Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Less coding from Select..Case

Status
Not open for further replies.

hamking01

Programmer
May 30, 2004
238
US
I have a multi dimensional array of textboxes, arrTxtBx(37, 3), and data populated in a datatable, dtResult. For each row in datatable if 1st column is DbNull it'll return "0" for the 1st set of 3 textboxes, if 1st column in not Dbnull it'll return columns 3, 4, 5 as text for the 3 textboxes.

I've gotten the above to work with Select...Case statement. However, I would need to code 38 cases for the 38 elements in the textbox arrays, when the code within the case is exactly the same.

I'm sure this could be coded without the 38 case statement, but just can't seem to see it right now. Could anyone help on this. Thanks

Here's the code:
Dim arrTxtBx(37, 3) As TextBox

Sub DispResult()
Dim arrTxtBx(,) As TextBox =
{{txtHrFoundry, txtTpFoundry, txtSlFoundry}, _
{txtHrMold, txtTpMold, txtSlMold}, _
{txtHrMach, txtTpMach, txtSlMach}, _
{txtHrMachQC, txtTpMachQC, txtSlMachQC}, _
{txtHrMachQC, txtTpMachQC, txtSlMachQC}, _
{txtHrDie, txtTpDie, txtSlDie}, _
{txtHrCrib, txtTpCrib, txtSlCrib}, _
{txtHrShot, txtTpShot, txtSlShot}, _
{txtHrPaint, txtTpPaint, txtSlPaint}, _
{txtHrLeak, txtTpLeak, txtSlLeak}, _
{txtHrFinal, txtTpFinal, txtSlFinal}, _
{txtHrMat, txtTpMat, txtSlMat}, _
{txtHrMain, txtTpMain, txtSlMain}, _
{txtHrQual, txtTpQual, txtSlQual}, _
{txtHrProc, txtTpProc, txtSlProc}, _
{txtHrJan, txtTpJan, txtSlJan}, _
{txtHrMfgEng, txtTpMfgEng, txtSlMfgEng}, _
{txtHrFac, txtTpFac, txtSlFac}, _
{txtHrHr, txtTpHr, txtSlHr}, _
{txtHrSaf, txtTpSaf, txtSlSaf}, _
{txtHrSec, txtTpSec, txtSlSec}, _
{txtHrPurch, txtTpPurch, txtSlPurch}, _
{txtHrRecv, txtTpRecv, txtSlRecv}, _
{txtHrOEM, txtTpOEM, txtSlOEM}, _
{txtHrAcct, txtTpAcct, txtSlAcct}, _
{txtHrIS, txtTpIS, txtSlIS}, _
{txtHrAssHS, txtTpAssHS, txtSlAssHS}, _
{txtHrFinHS, txtTpFinHS, txtSlFinHS}, _
{txtHrForgHS, txtTpForgHS, txtSlForgHS}, _
{txtHrCustCHI, txtTpCustCHI, txtSlCustCHI}, _
{txtHrCoppCHR, txtTpCoppCHR, txtSlCoppCHR}, _
{txtHrPlatCHR, txtTpPlatCHR, txtSlPlatCHR}, _
{txtHrSolCHR, txtTpSolCHR, txtSlSolCHR}, _
{txtHrBrightCHR, txtTpBrightCHR, txtSlBrightCHR}, _
{txtHrLabCHR, txtTpLabCHR, txtSlLabCHR}, _
{txtHrLabCHR, txtTpLabCHR, txtSlLabCHR}, _
{txtHrWasteCHR, txtTpWasteCHR, txtSlWasteCHR}, _
{txtHrPolishCHR, txtTpPolishCHR, txtSlPolishCHR}, _
{txtHrFlowVN, txtTpFlowVN, txtSlFlowVN}}

Dim strMonth As String = Request.QueryString("Month")
Dim strYear As String = Request.QueryString("Year")
Dim strPlant As String = Request.QueryString("Plant")
Dim dtResult As DataTable = dtFunctionData(strPlant, _
strYear, strMonth)
Dim i As Integer
For i = 0 To dtResult.Rows.Count - 1
Select Case CInt(dtResult.Rows(i)(1).ToString)
Case 1
If dtResult.Rows(i)(0) Is DBNull.Value Then
arrTxtBx(i, 0).Text = "0"
arrTxtBx(i, 1).Text = "0"
arrTxtBx(i, 2).Text = "0"
Else
arrTxtBx(i, 0).Text = dtResult.Rows(i)(2).ToString
arrTxtBx(i, 1).Text = dtResult.Rows(i)(3).ToString
arrTxtBx(i, 2).Text = dtResult.Rows(i)(4).ToString
End If
Case 2
If dtResult.Rows(i)(0) Is DBNull.Value Then
arrTxtBx(i, 0).Text = "0"
arrTxtBx(i, 1).Text = "0"
arrTxtBx(i, 2).Text = "0"
Else
arrTxtBx(i, 0).Text = dtResult.Rows(i)(2).ToString
arrTxtBx(i, 1).Text = dtResult.Rows(i)(3).ToString
arrTxtBx(i, 2).Text = dtResult.Rows(i)(4).ToString
End If
Case ....38
End Select
Next
End Sub
 
If the code for each case is exactly the same, then you do not need a select at all. This should do:

For i = 0 To dtResult.Rows.Count - 1
If dtResult.Rows(i)(0) Is DBNull.Value Then
arrTxtBx(i, 0).Text = "0"
arrTxtBx(i, 1).Text = "0"
arrTxtBx(i, 2).Text = "0"
Else
arrTxtBx(i, 0).Text = dtResult.Rows(i)(2).ToString
arrTxtBx(i, 1).Text = dtResult.Rows(i)(3).ToString
arrTxtBx(i, 2).Text = dtResult.Rows(i)(4).ToString
End If
Next



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
Sorry, wasn't specific enough.

If Select Case CInt(dtResult.Rows(i)(1).ToString)
Case 1
Then need to fill 1st set of textboxes:
{txtHrFoundry, txtTpFoundry, txtSlFoundry}
Case 3
Then fill 2nd set of textboxes:
{txtHrMold, txtTpMold, txtSlMold}


The case # may not be in the same order as the textboxes. For example, Case 50, could be for the fourth set of textboxes.
 
If I've understood you correctly, each Case corresponds to a set of textboxes. For example, Case=1 corresponds to the Foundry textboxes, Case=3 corresponds to the Mold textboxes, Case=50 corresponds to the Mach textboxes, Case=18 corresponds to the MachQC textboxes, ..., etc.

Maybe if you had a key/value table, like below
Code:
 1 - "Foundry"
 3 - "Mold"
50 - "Mach"
18 - "MachQC"

you could query the Value column for the Key= CInt(dtResult.Rows(i)(1).ToString) and then you could check to see if arrTxtBx(i,0).Name.ToString() contains the appropriate string value. If it does, you know you have the right group of textboxes and can go ahead and populate them. You'd have to take care to check that the last X characters of the textbox's name are identical to your Value, where X=Value.Length, otherwise you might end up populating the MachQC textboxes when you wanted to populate the Mach textboxes.
I can't provide any example code at the moment because I'm at home and lacking the necessary equipment, but in pseudocode (or kind of semicode!):

Code:
For i = 0 To dtResult.Rows.Count - 1
  Dim theKey as Int
  Dim theValue as String
  Dim theTextBoxName as String
  theKey = CInt(dtResult.Rows(i)(1).ToString)
  theValue = [i]{get the value corresponding to the key}[/i]
  theTextBoxName = arrTxtBx(i,0).Name.ToString()
  If theTextBoxName.Substring(theTextBoxName.Length - theValue.Length, theValue.Length) = theValue  
      If dtResult.Rows(i)(0) Is DBNull.Value Then
         arrTxtBx(i, 0).Text = "0"
         arrTxtBx(i, 1).Text = "0"
         arrTxtBx(i, 2).Text = "0"
      Else
         arrTxtBx(i, 0).Text = dtResult.Rows(i)(2).ToString
         arrTxtBx(i, 1).Text = dtResult.Rows(i)(3).ToString
         arrTxtBx(i, 2).Text = dtResult.Rows(i)(4).ToString
      End If
  End If
Next

I have no idea if this would work, or indeed if I've understood what you're trying to do, but hopefully it might give you a new idea. Hope it helps.

I'm the Chairman of the Bored
 
LFCfan,

That's exactly what I'm trying to do. I didn't try it out, but looking at the code it should work. I actually figured out a solution using a slightly different method. Instead of a key/value table I added another hidden textbox to each array of textboxes. It would then match the value of this textbox to the value in stored procedure column and populate textboxes accordingly.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top