473,396 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How do you convert a string into an object

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
3 6678
Frinavale
9,735 Expert Mod 8TB
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
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
9,735 Expert Mod 8TB
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

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

Similar topics

2
by: Nathan | last post by:
Is there a way to convert a string to a CipherMessage? I am calling a function that decrypts a CipherMessage and returns the value. The only problem is when I want to use an encrypted value stored...
9
by: Andy Sutorius | last post by:
Hi, I am receiving the error when compiling the project, "cannot implicitly convert type object to string". The error points to this line of code and underlines the dtrRecipient:...
3
by: klynn | last post by:
I defined a session variable as an array using Session = new string; Later, in my code, I need to set it and sort it. I tried Session = "some string"; My error is "Cannot apply indexing with to...
5
by: XML newbie: Urgent pls help! | last post by:
function to convert string to 1 dimensional array of long in VB.Net
3
by: Ursula | last post by:
Is it possible to convert a string in a file. The problem is this: I have an object string that is a file xml and I want to pass to Deserialize function, but Deserialize function expect an object...
2
by: james | last post by:
I wrote values to the registry in a "MultiString" registry type. I now want to retrieve these into a string array. I use this: String LoadedObject = SUBKEY.GetValue(KeyNames.ToString()) but...
15
by: Buddy Home | last post by:
Hello, I'm trying to speed up a piece of code that is causing performance issues with our product. The problem is we are using serialization to convert the object to a string, this is costing us...
4
by: Lloyd Dupont | last post by:
to store user preference in our application we have an 'hand written' XML file. (as opposed to XmlSerializer written one, which crash sometimes, on some user's computer, for some unknown reason.....
2
by: manjitsarma | last post by:
In the following code ,I am getting the errors public abstract class Theme { private string sFrmBackGroundImage; private Color sFrmTransperncyColor; public object...
3
by: mamul | last post by:
Hi please some one can help me. how to convert char * to string? i have take char *argv from command line and want to pass to a function as string object(string str) i want to first convert argv...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.