Label problem

Hi all,

I have the following labels

Result1, Result2, Result3 ..... Result10

I have a loop that does some calculate based on the labels and I encountered some error

eg
CurrentPlay is an integer

if cint(me.controls("Result" & CurrentPlay-2).text) > 2 then ....

Please help. Thanks!

LVL 9
leechoonhweeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Wayne Taylor (webtubbs)Commented:
What's the error?
0
Hawkvalley1Commented:
If the CurrentPlay - 2 is less than 1 you will get an error as there is no label named "Result0"
You should check this integer prior to calling this line:

If CurrentPlay > 2  AndAlso Not CurrentPlay > 12 Then
    If cint(me.controls("Result" &  CurrentPlay-2).text) > 2 then ....
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
You will also get an error if the current text in the label cannot be converted to an integer with CInt()...make sure it has a valid "input".
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

leechoonhweeAuthor Commented:
Currentplay had been checked before it enters the sub routine.

The error is raised on unable to convert string to integer which has nothing to do with the Cint but rather something to do with the syntax of

me.controls("Result" & CurrentPlay -2).text

It seems that there is something structually wrong with the syntax above.

I can however get around the prblem by using a label link instead as label links have index which I can directly reference with like

me.controls(Currentplay-2).text

However, it will be helpful to know where is the root of the problem so that it might help others too.

Thanks!

0
Mike TomlinsonMiddle School Assistant TeacherCommented:
You could make the subtraction occur first by placing it in parenthesis:

    Me.Controls("Result" & (CurrentPlay - 2)).Text

Also, if this is WinForms and .Net 2.0 (or above) then you can make it search for a matching control so it will be found even if the control is nested several levels deep:

    Dim matches() As Control = Me.Controls.Find("Result" & (CurrentPlay - 2), True)
    If matches.Length > 0 AndAlso TypeOf matches(0) Is Label Then
        Dim lbl As Label = CType(matches(0), Label)

        ' ... do something with "lbl" ...

    End If
0
JackOfPHCommented:
I think the problem is with the CINT

If the value of the label is am "empty string" it will raise the an exception.

Cannot convert string to int.

So, I think to avoid this problem you have to check whether the value of the label is convertible to Integer.

Try this code:
If Not Char.IsDigit(Me.Controls("Result" & CurrentPlay - 2).Text) Then
            MsgBox("The value of label is " & Me.Controls("Result" & CurrentPlay - 2).text & " Cannot convert to integer.)
            Exit Sub
        End If

        If CInt(Me.Controls("Result" & CurrentPlay - 2).Text) > 2 Then
            'Do your staff here...
        End If

Open in new window

0
Hawkvalley1Commented:
I use this type of naming convention and it works fine. The only difference I see is I cast the label type.

CInt(CType(Me.Controls("Result" & (CurrentPlay - 2).ToString(), Label).Text)
This is also Option Strict On correct.

Also you have not shown us the actual error as it was asked above and this will help us understand the true issue.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.