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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable reference in a loop

Status
Not open for further replies.

hitechboy78737

Programmer
Joined
Nov 8, 2004
Messages
82
Location
US
I have an object that has 3 properties: .Q0, .Q1, .Q2

Each property can hold a value between -1 and 3

Each property will set one of 4 checkboxes on a report (not a form) depending on the value.

I want to reference the property in a loop using the property's common name "Q" and an count integer "i"- like .Qi .

This of course won't work.. but how can I make something like this work...

The code:
Code:
For i = 0 to 2
if .Qi = 0 then
 ckbx_Ai_0.text = "x"
elseif .Qi = 1
 ckbx_Ai_1.text = "x"
elseif .Qi = 2
 ckbx_Ai_2.text = "x"
elseif .Qi = 3
 ckbx_Ai_3.text = "x"
next

This loop will save me bucoos of typing if I can make it work.

Thanks for your help!
Kevin Howell

 
You actually can do this by using the name -- take a look at the System.Reflection namespace. Fair warning, though, it'll be slow.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
For everyone's edification... this is how I solved this problem...

Code:
    Private Sub buildEvalReportAnswers(ByVal fieldSubString As String, ByVal answers As String, ByRef C1Rpt As C1Report)
        Dim arrAnswers As Array = answers.Split(",")
        Dim strAnswer As String
        Dim i As Integer = 0
        For Each strAnswer In arrAnswers
            strAnswer = arrAnswers(i)
            Select Case strAnswer
                Case 0
                    C1Rpt.Fields(fieldSubString & i & "_0").Text = "x"
                Case 1
                    C1Rpt.Fields(fieldSubString & i & "_1").Text = "x"
                Case 2
                    C1Rpt.Fields(fieldSubString & i & "_2").Text = "x"
                Case 3
                    C1Rpt.Fields(fieldSubString & i & "_3").Text = "x"
            End Select
            i = i + 1
        Next
    End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top