Object names as variables...

B2Ben

Well-known member
Joined
Aug 17, 2006
Messages
52
Programming Experience
Beginner
This may be a simple thing, but the solution escapes me as a relatively inexperienced Dot-Netter...

I have a form with 24 pictureboxes, named numerically:
"Mod0"
"Mod1"
"Mod2"
...etc...
"Mod22"
"Mod23"

I want to change properties of these pictureboxes by supplying a function with the number of the item. For instance...

function HideMod(ModNum as integer)
PictureBoxName = ("Mod" & ModNum)

'I know this next line is wrong, but can you see what I want to do?
PictureBoxName.visible = false
'I want to avoid 24 different if/then statements, and just modify each one by number/variable

end function


I hope that made sense... if anyone can help me figure this out, it would be a huge help. Thanks :)
 
hi

i dont know if this is the best way to do it, but you could create an object array, and then loop through the form.controls and if the current control is a picturebox, store it in the array.

that way, when you supply an integer it doubles up as the index of the array.

i would test it myself, but i dont have a coding environment at the moment.

hope it helps mate

regards
adam
 
Lots of beginners ask that sort of question but it is not the correct way to do things. It is possible using reflection but reflection is to be avoided except where it is needed and it is certainly not needed here. If you want to refer to an object by an index then you put that object in an array or collection. The correct way to do what you want would be something like this:
VB.NET:
Private pictureBoxes As PictureBox()

Private Sub Form1_Load(...) Handles MyBase.Load
    Me.pictureBoxes = New PictureBox() {Me.Mod0, Me.Mod1, ..., Me.Mod23}
End Sub

Private Sub HideMod(ByVal index As Integer)
    Me.pictureBoxes(index).Hide()
End Sub
 
ah i didnt know that you could do a collection like that with a control itself.
well, learn something new every day. and when you see it done like it seems so obvious, and you smack yourself in the head for not realising it hahah

cheers mate

regards
adam
 
Just FYI, .NET 2.0 allows you to retrieve a control by name too. It's not fool-proof as it gets more complex with nested controls and controls created at run time, but you can do this in VB 2005:
VB.NET:
Private Sub HideMod(ByVal index As Integer)
    Me.Controls("Mod" & index).Hide()
End Sub
That's because the ControlCollection.Item property is overloaded in .NET 2.0, so you can index it by ordinal (number) or key (name). In .NET 1.x the ordinal is all that's available.
 
well as long as you keep track of the controls (ie. ones in a panel for instance) wouldnt the method you just said be quicker than having to create an array of controls?

rather than having to create the array, instanciate it, and fill it, just one line of code gets execute. it sounds quicker to me, is that right or not?


cheers
adam
 
It would actually likely be slower. Creating the array is quite a quick process, plus it allows you to include controls from various containers. Retrieving a control by name involves looping through all the control names and comparing them to the one specified, then when a match is found uisng its index to get the actual control by that index. Just because it's one line in your code doesn't indicate anything about what's going on behind the scenes.
 
ok then, that makes sense. cheers for your input. i have had a similar problem to this one in the past. but now ill be able to do it no problem.

once again, thanks

regards
adam
 
Back
Top