please correct my coding to work please(newbie)

kogan31

Member
Joined
Oct 6, 2003
Messages
11
hi guys..
i wanna do a coding whereby whenever a number is removed from label1(label1 has codings to generate numbers),label4 will indicate a +1 point(for ex. if 5 numbers is removed from label1,label4 will show 5)

(i already did a coding to remove the numbers..)


and also this error message on sr = CStr(Label1.Text) is shown

An unhandled exception of type System.InvalidCastException occurred in microsoft.visualbasic.dll

Additional information: Cast from string "" to type Integer is not valid.
 
Last edited by a moderator:
why is the CS before the tr in this code:

sr = CStr(Label1.Text) (of course you dont have Label1.Text in your code)
 
err..
sorry im a newbie and i dun quite understand u
CStr is a code use to convert string... am i wrong?
if so wats the code i should write?
could u show them to me?

thanks alot
 
CStr converts it to a string, yes, but you want to convert it to an integer. So you should be using CInt (because sr is an integer)
 
errmm..
ok..
but there is still an error

An unhandled exception of type System.InvalidCastException occurred in microsoft.visualbasic.dll

Additional information: Cast from string "" to type Integer is not valid.

i think there is something wrong somewhere in my coding
Code:
Dim sa As String = Label1.Text
        Dim sr As Integer
        sr = CInt(Label1.Text)
        If sa.Length = sr - 1 Then
            Label4.Text = sr + 1
        End If
any idea wats wrong?
please advice

thank you so much
 
Last edited by a moderator:
Well, you seem a little new to try and explain what Try/Catch error handling is (at least for me to explain it, as I suck at explaining things). But the error is because you dont have an integer value in your Label. Make sure its an integer value, and you wont get that error. Or you can look up how to use Try/Catch (are you learning from a book or just winging it?)
 
yeah
im new to vb..
yeah there is an integer value in the label but its only when i activate the generating of the numbers den the numbers(integers) starts..
so is the error from there?
 
hmm...

still cant work for me..
ok..
can anyone do me a favor?
could u write a rough coding so that if i remove a number from label1 a point will be added in label4..

thanks alot
 
Is Label1 displaying anything at all? If not you will get the error because an empty string canot be converted to an integer.
Use the Try / Catch suggestions already given.
 
nope label1 is not displaying anything at all until i start the timer and numbers will be generating out randomly..
as a newbie..
i do not know how to use the try/catch..
hehe
hope u guys can help me out?
gonna hand in this project in like 3 hours time..
lol
 
Code:
Try
    If Not Label1.Text = Nothing Then /// incase its empty
        Dim sr As Integer = Integer.Parse(Label1.Text)
        /// do your code stuff here.
    End If
Catch Ex As Exception
    MessageBox.Show(Ex.Message)
End Try
 
I tried your code in a simple program and it worked ok, the only way I got an error was when lable1.text was not a whole number (Input string was not in correct format) What does label1.text show when you get the error
 
have you tried asigning a value to _LastValue to start with? ie:
Code:
Private _LastValue As Int32 = 0
also instead of having "10" and "0" in the load event, have you tried removing the ""? , ie:
Code:
Label1.Text = 10
Label4.Text = 0
 
Where have you got the code to start the random generation of numbers?

If i call a sub that changes label1.text in sub main I get an error, only get the error you have if i have a very large number in label1.text
 
Back
Top