Get Object/Other Data Type Ref from String Expression

TheGrange

Member
Joined
Jan 2, 2009
Messages
7
Programming Experience
Beginner
Hello everybody, I have just joined this forum :D

I have a question to which I have not been able to find a good answer despite much googling.

The problem is to get an object reference, or other data type reference from a string. For example, suppose I have ten picturebox controls sequentially named PicBox1 to Picbox10 and I want to change their properties during runtime, but I want to do this using a string equal to their name, and I don't know at design time what that string value will be. Something like:

Dim BoxName As string
BoxName = InputBox("Please enter name of box")

'Then I want to do something like:

BoxName.Image = .......

The idea being that the user can type in the name of one of the pictureboxes, and that box's image will change to whatever I have specified in the ...... part of the above code.

I cannot find a way of doing this that doesn't cause an error. I get a "cannot convert string to picturebox" or something along those lines. What I want is the picture box whose name is the string.

I hope this makes sense, thanks in advance to anyone providing help.
 
Controls have a Name property and that property value is used the key when adding controls to a container's Controls collection. As such, you can get a control by name like so:
VB.NET:
Dim ctl As Control = Me.Controls(controlName)
Note that you are getting the control by its Name property, NOT by the name of the variable used to refer to it. The variable name and the Name property are the same by default when you add controls in the designer but they can be changed and controls added in code have neither unless you explicitly code it.

Having said all that, is it really appropriate that your users need to know the name of a control that you use in code? I would think that it would be far more appropriate for the user to identify a PictureBox by number or by a friendly name that would be inappropriate as a variable name.
 
TheGrange said:
The idea being that the user can type in the name of one of the pictureboxes
You should provide a selection of available names/numbers that the user can choose from, for example with ListBox or Combobox. User should not need to type a string since the options here is given, be they fixed or dynamic.
 
Thats great thankyou. Actually you are right the example I suggested is bizarre its not what I am really trying to do but it was a much simpler explanation than the real thing which would have been very confusing to read on a forum, or for me to explain for that matter! Thanx very much though me.controls(name) has done the trick.

Do you know if there is anyway to do a similar thing but with other variables e.g. booleans, so you dim several booleans and then want to set one (whose expression is equal to a string value generated at runtime) to true or false using code? So just for simplicity, suppose the user was inputting the name as above, and you want the boolean equal to that character string to be true, for example.

thank you very much for your help
 
You would use a If block or a Select Case block.
VB.NET:
If somestring="my keyword" then bool = True
All built-in primitive types also has a parsing method that reads the string representation of the value to data type value:
VB.NET:
Dim b As Boolean = Boolean.Parse("true")
Also note there are several controls that provide checkbox functionality to give user a yes/no, on/off, true/false choice.
 
Do you know if there is anyway to do a similar thing but with other variables e.g. booleans, so you dim several booleans and then want to set one (whose expression is equal to a string value generated at runtime) to true or false using code? So just for simplicity, suppose the user was inputting the name as above, and you want the boolean equal to that character string to be true, for example.
I think I specifically said in my post that you are NOT using variable names but the Name property of the controls, which other types do NOT have. That said, you could always create a Dictionary(Of String, Bollean) or the like to store and identify your values by names.

I'm afraid I don't buy that an explanation of what you're trying to do would be too confusing. If you can't explain it to other developers then how can you possibly explain how to use your app to non-technical users? If you care to make the effort then you'll be able to explain it, even if it requires a screen shot or two for clarification. If we know what you're actually trying to achieve, then we can probably give the best advice for achieving it.
 
Back
Top