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

Reference an index of a variable name

Status
Not open for further replies.

Theseekers

Technical User
Jul 22, 2004
118
US
Hi Tekers,

I am passing 4 controls to a function to do some tests. In the function I reference them as InputVar1, InputVar2..etc.

In stead of address them individually, I am looping them in a for and next loop. My problem is that I don't know how to format the variable name so that I can use in the loop. For instance, I need to check to see if the control is a text box or a dropdown list so normally I would use this to test it:

If typeof (Inputvar1) is textbox then
...do thing

However, in the loop I would like to substitute the index 1 with the loop control variable something like this:

If typeof (inputvar & lcv) then...

I have tried these methods so far but none is working.

if typeof ("InputVar" & lcv)

if typeof (input + lcv)

dim s as tring = "InputVar"
if typeof s & lcv then ...

can someone lend me a hand on how to concatenate the name InputVar along with the loop index so I can address them programatically..

TIA
 
Try:
Code:
dim s as string
s = "InputVar" + CStr(lcv)
if typeof(s) then
  ....
Jim
 
Hi JBen,

Thanks for your suggestion. However, it does not work.

When I put view s in debug mode it bears a string type as "InputVar1"; thus, it failed the test for the typeof.

Any other suggestions that I might try out?

Thanks
 
Try using FindControl to get a reference to the relevant control


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Try using c8msm's suggestion.. something like

Code:
dim s as String
dim obj as Object
s = "InputVar" + CStr(lcv)
obj = Me.FindControl(s)

If typeof(obj) = ... Then
...
End If
 
Hi C8 and JBen,

OK, I must have done something wrong here. Your suggestions are valid, but I cannot get it to see the control. I am posting my code segment here and hope that someone can spot my wrong doing.

In the calling code segment I call the function as:
Code:
  CheckKeyFields(me.ymrs1, me.ymrs17)

FYI - ymrs1 is a dropdown box, and ymrs17 is a textbox. I also cut down # of parameter for the post.

In the function CheckKeysFields I declared it as:

Code:
 Function CheckKeyFields(ByVal InputVar1 As Object, ByVal InputVar2 As Object) As Boolean
    Dim LCV As Integer
    Dim S As String
    Dim Obj As Object

    For LCV = 1 To 2
      S = "InputVar" + CStr(LCV)
      Obj = Me.FindControl(S)
      If TypeOf (FindControl("InputVar" & LCV)) Is TextBox Then
         Response.Write("textbox")
      Else
         Response.Write("dropdown box")
      End If
    Next

When i view the S and Obj in debug mode as it goes through the loop, I can see that S = "InputVar1" and Obj = Nothing respectively for I = 1 and the same for I = 2

With this, the if condition is always a dropdown box....

What have I done wrong??? Please help.

10Qs.....
 
Ooop my bad,

the statement if typeof should read as:

if TypeOf(Obj) Is TextBox then


Sorry for the typo...

 
Hello,

For Jben - I am running this on the page itself.

Anyway, after much and long search, I found out that I can not convert a string to an object so I ended up with putting those objects into an arrary and from there I can get to them individually by using the array index.

Here is my solution to this question. Suggestion or information is more than welcome.

Code:
        Dim TempArray(3) As Object

        TempArray(1) = InputVar1
        TempArray(2) = InputVar2
        TempArray(3) = InputVar3


        For LCV = 1 To 3
            If TypeOf TempArray(LCV) Is TextBox Then
                If (Len(CType(TempArray(LCV), TextBox).Text) & "") = 0 Then
                    blah blah blah..
                endif
            endif
        next

HTH.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top