By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,815 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,815 IT Pros & Developers. It's quick & easy.

How do you convert a string into an object

P: 4
So, I'm creating an array of labels. I have labels set up as:

labal1
label2
label3
etc...

I've declared the array as such:
Expand|Select|Wrap|Line Numbers
  1. Dim labelArray(5) As Label
Now, instead of assigning the labels one by one to the array (labelArray(0) = label1, labelArray(1) = label2, etc...), I was hoping to use a loop (there will be more than 5 labels). I have something like this, which obviously doesn't quite work:
Expand|Select|Wrap|Line Numbers
  1. For i = 0 To 5
  2.       labelArray(i) = "label" & i
  3. Next

Is there a function that I can use to convert the labelName string to an object, or have vb.net see that as an object instead of a string?
Apr 2 '12 #1
Share this Question
Share on Google+
3 Replies


Frinavale
Expert Mod 5K+
P: 9,731
Labels are objects.
They have properties....

The have a Text Property that is a string and is used to display text in the Label. They also have a Name Property that you can use as an identifier for the Label so that you can access it by name/id in code.

Since you are dynamically populating the labels, it's unlikely that you'll use the Name property but you are quite likely to use the Text property...since you want to display text in the label.

What I'm trying to say is that you don't want to convert the Label into a String. You want to use the Label's Text Property to set and get the string displayed.

Like this:
Expand|Select|Wrap|Line Numbers
  1. For i = 0 To labelArray.Length-1
  2.   labelArray(i) = New Label
  3.   labelArray(i).Text = "label" & i
  4. Next
-Frinny
Apr 3 '12 #2

P: 4
Thanks for the reply.

I'm not trying to set the text for the labels. That was done in the property settings for the labels themselves when I created my form. The text is set as "Seat 1", "Seat 2", etc...

I have a million labels on this form.

lblLabel1
lblLabel2
lblLabel3
lblLabel4
etc...

I created an array so that I could use loops to manage changes to the labels. But, I don't want to have to use code like...

Expand|Select|Wrap|Line Numbers
  1. labelArray(0) = lblLabel1
  2. labelArray(1) = lblLabel2
  3. labelArray(2) = lblLabel3
  4. labelArray(3) = lblLabel4
  5. labelArray(4) = lblLabel5
  6.  
When you have a million labels, this is kind of much. So, I wanted to use a loop and concatenate the counter to the end of say "lblLabel" and do it that way, so that I don't have 10 million lines of code for this.
Apr 3 '12 #3

Frinavale
Expert Mod 5K+
P: 9,731
I'm sorry I didn't understand what you were trying to accomplish yesterday. Thanks for re-explaining the problem.

I didn't realize that you had put your labels on the form already...I thought that you were trying to dynamically create them and place them on the form.

Anyways, I'm not very familiar with Win Form projects but you should be able to loop through all of the child controls and use the Type Of or GetType methods to determine what the control is.

For example, if all of your controls were in a Panel, you could loop through the Panel.Controls property, check if the control is a Label, and if it is, add it to your list of labels.

There's probably a way to do this for the whole window/form too but I'm not sure of the properties (it's probably a Controls property too).

You could even use LINQ to select all of the labels from the Controls property if you want.

-Frinny
Apr 4 '12 #4

Post your reply

Sign in to post your reply or Sign up for a free account.