Board index » Visual Studio » Using variable names to change labels

Using variable names to change labels

Visual Studio337
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


-
 

Re:Using variable names to change labels

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.



"Jarry" <HarryandJames@gmail.com>wrote in message

Quote
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







-

Re:Using variable names to change labels



Marina Levit [MVP] wrote:



Quote
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...



-

Re:Using variable names to change labels

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).



"Jarry" <HarryandJames@gmail.com>wrote in message

Quote


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...







-