הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Monday, July 25, 2005 5:29 AM
**How do I use a value of a string as a public variable name/object that I can then change....
**
dim var1 as integer
dim var2 as integer
dim var3 as integer...ect...
Dim vroot as string = "var"
Dim i as integer = 1
Do
var + i.tostring
'this returns "var1"
'now how do i use this var1 returned value to act like a variable????
i=i+1
loop until i = 15
Also how would i do this with objects....say i had a series of labels named label1, labe2, label3...ect... to call these through a "label + i.tostring + .text" script also......
Catch my drift....is this possible?
Thanks!
All replies (12)
Wednesday, September 7, 2005 11:57 PM ✅Answered
As papdi writes above, if the "variables" that you are playing with happen to be controls that are placed on a form or user control, then they are already part of a collection (the controls collection of the form or user control)
You can access the controls by name from within the parent either by name or by index:
Me.Controls(<control name>) |
If this we are talking about normal variables, then I'd be boring and strongly suggest using a collection. If you could give us some more information about what you are trying to accomplish, we may be able to come up with a solution that is dynamic enough for your needs
Best regards,
Johan Stenberg
Monday, July 25, 2005 5:41 AM
Hi,
I don't think that this is possible. But in your scenario, you could store all of your objects in a Hashtable or anykind of Collection and make its objectnames as keys so you could just accesit like this ht["label" + i.ToString()]...
cheers,
Paul June A. Domag
Monday, July 25, 2005 8:48 AM
I don't understand the first question. What do you mean 'act like a variable'?
To the second question:
Me.Controls("Label" & i).Text = "bla bla"
Monday, July 25, 2005 2:19 PM
I cant get the me.controls method to work... It gives an error...I've tried many times but the syntax is off....Does anyone know the correct syntax for this?
Monday, July 25, 2005 4:34 PM
What do you mean "the syntax is off"? What's the exact exception the compiler returns?
Monday, July 25, 2005 10:35 PM
It dosn't accept it as the name of an object....
"Object reference not set to an instance of an object."
Tuesday, July 26, 2005 6:33 AM
It seams that you will have to read some things about Collections. You should a Collection to deal with this. You can't get the value of var1 by typing var + i.tostring. It does not work this way. Search for .net collection tutorials on google.
Wednesday, September 7, 2005 11:01 PM
i was curious if you got the "collections" to work for you? I'm looking for the same type of solution you were and most places are stating the use of collections.....I would think there would be something more dynamic than creating a collection...
Regis
Monday, January 23, 2006 10:28 AM
Hi,
I have read all above, my question is (because I think I have same proble) How to use a string for control name in Me.controls (<control name<)
bst rgds,
Wednesday, April 12, 2006 4:51 PM
If you get an answer that works for this I would appreciate hearing from you. I have tried the HashTable that was mentioned and every other collection type I can think of.
If anyone has an answer please let me know.
Thanks in advance,
Rich
Wednesday, April 12, 2006 5:39 PM
For the issue with variable names, here is some code from VB.Net 2003. Note that the collection is actually a collection of objects, so you can't assume (for example) that the variable stored is an integer, a string, etc.
Dim MyVariables As New Collection
With MyVariables
.Add("1", "Var1")
.Add("2", "Var2")
.Add("3", "Var3")
End With
'
Const strRoot As String = "Var"
Dim intIdx As Integer
For intIdx = 1 To 3
MsgBox(MyVariables(strRoot & intIdx))
Next
For the issue with controls, in VB.Net 2003 I was getting an error trying to access the control collection by control name. Instead, this would work (assuming you have three labels named Label1, Label2, and Label3):
Const strControl As String = "Label"
Dim ctl As Control
For intIdx = 1 To 3
For Each ctl In Me.Controls
If ctl.Name = strControl & intIdx Then
MsgBox(ctl.Text)
End If
Next
Next
Saturday, June 13, 2009 12:18 AM
What about in a console application?