Home All Groups Group Topic Archive Search About

Using variable names to change labels

Author
9 Nov 2006 7:26 PM
Jarry
This may sound really obvious, but is there a way to change the text of
different certain label depending on an integer? For example, here is
the long hand (using the default label1, label2 etc) :
Dim myInteger as integer = 15
Select Case myInteger
Case 1
label1.text = "This was changed"
Case 2
label2.text = "This was changed"
Case 3 ...
End Select
There must be a better way - I was thinking something along the lines
of the eval() statement in javascript etc? Or something completly
different?

Thanks

Author
9 Nov 2006 7:32 PM
Marina Levit [MVP]
You would have to find the control by name. I don't know if this is a web
form or a winforms, but you can typically do it either way.

A more reliable solution is to put them in a label array, and index into the
array to get the appropriate one.

Show quoteHide quote
"Jarry" <HarryandJa***@gmail.com> wrote in message
news:1163100362.315640.48890@m73g2000cwd.googlegroups.com...
> This may sound really obvious, but is there a way to change the text of
> different certain label depending on an integer? For example, here is
> the long hand (using the default label1, label2 etc) :
> Dim myInteger as integer = 15
> Select Case myInteger
> Case 1
> label1.text = "This was changed"
> Case 2
> label2.text = "This was changed"
> Case 3 ...
> End Select
> There must be a better way - I was thinking something along the lines
> of the eval() statement in javascript etc? Or something completly
> different?
>
> Thanks
>
Author
9 Nov 2006 7:45 PM
Jarry
Marina Levit [MVP] wrote:

> You would have to find the control by name. I don't know if this is a web
> form or a winforms, but you can typically do it either way.
>
> A more reliable solution is to put them in a label array, and index into the
> array to get the appropriate one.
>

I considered that, and, now you mantion it, it does seem to be the best
option. Are there any more? Also, is it possible to define variables
using another variable:

For x = 1 to 20
Dim (myVar + x) as string
Next x

for instance? I have a huge array, and it might help...
Author
9 Nov 2006 7:56 PM
Marina Levit [MVP]
No, you cannot do anything like what you have there.  The compiler can't
make sense of that.

I would say either array, or use the methods available to find the label by
name on the page (i'm assuming asp.net from the way you've been talking).

Show quoteHide quote
"Jarry" <HarryandJa***@gmail.com> wrote in message
news:1163101550.619757.276920@f16g2000cwb.googlegroups.com...
>
> Marina Levit [MVP] wrote:
>
>> You would have to find the control by name. I don't know if this is a web
>> form or a winforms, but you can typically do it either way.
>>
>> A more reliable solution is to put them in a label array, and index into
>> the
>> array to get the appropriate one.
>>
>
> I considered that, and, now you mantion it, it does seem to be the best
> option. Are there any more? Also, is it possible to define variables
> using another variable:
>
> For x = 1 to 20
> Dim (myVar + x) as string
> Next x
>
> for instance? I have a huge array, and it might help...
>