kogan31 Posted October 7, 2003 Posted October 7, 2003 (edited) 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. Edited October 8, 2003 by kogan31 Quote
whosyodaddy Posted October 7, 2003 Posted October 7, 2003 why is the CS before the tr in this code: sr = CStr(Label1.Text) (of course you don't have Label1.Text in your code) Quote
kogan31 Posted October 7, 2003 Author Posted October 7, 2003 err.. sorry i'm 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 Quote
wyrd Posted October 7, 2003 Posted October 7, 2003 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) Quote Gamer extraordinaire. Programmer wannabe.
kogan31 Posted October 7, 2003 Author Posted October 7, 2003 (edited) 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 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 Edited November 26, 2005 by PlausiblyDamp Quote
wyrd Posted October 7, 2003 Posted October 7, 2003 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 don't have an integer value in your Label. Make sure it's an integer value, and you won't get that error. Or you can look up how to use Try/Catch (are you learning from a book or just winging it?) Quote Gamer extraordinaire. Programmer wannabe.
kogan31 Posted October 7, 2003 Author Posted October 7, 2003 yeah i'm 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? Quote
Leaders dynamic_sysop Posted October 7, 2003 Leaders Posted October 7, 2003 try changing the CStr section to something along these lines ( assuming you only have numerics in the label's text ) Dim sr As Integer = Integer.Parse(Label1.Text) Quote
kogan31 Posted October 7, 2003 Author Posted October 7, 2003 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 Quote
Administrators PlausiblyDamp Posted October 7, 2003 Administrators Posted October 7, 2003 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
kogan31 Posted October 7, 2003 Author Posted October 7, 2003 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 Quote
Leaders dynamic_sysop Posted October 7, 2003 Leaders Posted October 7, 2003 Try If Not Label1.Text = Nothing Then '/// incase it's 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 Quote
kogan31 Posted October 7, 2003 Author Posted October 7, 2003 (edited) code doesnt work.. Edited October 8, 2003 by kogan31 Quote
pendragon Posted October 7, 2003 Posted October 7, 2003 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 Quote
kogan31 Posted October 7, 2003 Author Posted October 7, 2003 the error came out straight when i start to play the program.. dun even get to see my form.. lol Quote
Leaders dynamic_sysop Posted October 7, 2003 Leaders Posted October 7, 2003 have you tried asigning a value to _LastValue to start with? ie: Private _LastValue As Int32 = 0 also instead of having "10" and "0" in the load event, have you tried removing the ""? , ie: Label1.Text = 10 Label4.Text = 0 Quote
pendragon Posted October 7, 2003 Posted October 7, 2003 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 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.