Link to home
Start Free TrialLog in
Avatar of cotton9
cotton9

asked on

NEWBIE - Simple Array of Labels

Is there a way to simplify this by looping?  

Dim arrWord() As String = {"laud", "boat", "pool", "nag", "limb"}
intNumber = 1

        lblWord1.Text = intNumber & ". " & arrWord(intNumber - 1)
        intNumber = intNumber + 1
        lblWord2.Text = intNumber & ". " & arrWord(intNumber - 1)
        intNumber = intNumber + 1
        lblWord3.Text = intNumber & ". " & arrWord(intNumber - 1)
        intNumber = intNumber + 1
        lblWord4.Text = intNumber & ". " & arrWord(intNumber - 1)
        intNumber = intNumber + 1

        ' Etc. . .

I can't figure out how to refer to lblWordX in a loop.


SOLUTION
Avatar of Patrick Matthews
Patrick Matthews
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cotton9
cotton9

ASKER

matthewspatrick,

I've almost got it, but I get the following error:

Cast from string "lblWord1" to type 'Integer' is not valid.



Is that because it's looking for and integer in Controls(i)?



Note: The other benefit of control arrays is you avoid the limit of 254 named controls on a single form:
http://support.microsoft.com/default.aspx?scid=KB;EN-US;Q229756&
Avatar of cotton9

ASKER

AzraSound,

That sounds like a good way to go.  I wish I knew that before I manually created and renamed 50 labels.  Any ideas on that error?
Avatar of cotton9

ASKER

Ummmm. . . .

After searching endlessly for the index property, I found out that VB.NET controls do not use the index property.  Will either of these solutions work for .NET?

Avatar of cotton9

ASKER

Oops.  Sorry guys.  I didn't realize there was a VB.NET section.
AzraSound - .NET does NOT support control arrays - your advice applies to VB 6 but not to VB.NET.

cotton9, it is okay to ask general VB related questions (.NET as well as VB 6) here, though it is generally better to ask the VB.NET specific questions in the VB.NET section, found at

https://www.experts-exchange.com/Programming/.NET/VB.NET/

AW
Yes, I know, though I had no idea he was working in .NET until now.
AzraSound>> This line, from the original question, was the giveaway, that he was in VB.NET:

Dim arrWord() As String = {"laud", "boat", "pool", "nag", "limb"}


I dare you to try that in VB 6.

Just wanted to point out how we picked up on the .NET nature of the question.

AW
You don't need to dare me to do anything, and please don't question my intelligence.  I glossed over the question like I do all questions and got the gist of it from the title and a cursory glance at the code.  Secondly, I assume questions posted here will be in the VB pre-.NET category, so I already go in with that mindset.  I'm very proud you picked up on the nature of the question so quickly.  Here's a lollipop.