Results 1 to 10 of 10

Thread: Label1.text = 0 error

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Posts
    18

    Label1.text = 0 error

    When i type "Label1.text = 0" or use anything that can hold text, and try to have the text as an integer, just like in the example, i get an error. "Option Strict On disallows implicit conversions from 'Integer' to 'String' ", it also gives me an example of what it thinks i should be using, "Label1.text = Cstr(0)". I'm very confused by this as i've used integers in textboxs and labels many many times, and when i copy and paste from my old scripts it also comes up the errors, yet my old scripts still work, and are error free.

    Does anyone know what the problem could be?

    Thanks.

  2. #2
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Label1.text = 0 error

    There isn't a problem.

    It is just as the error says: you have Option Strict set to On, so you cannot implicitly convert 0 (an integer) to "0" (a string).

    The reason you could do it before in you 'script' (VBScript?) is because it would implicitly convert a numeric value to a string. There are many reasons why this seems convenient, but there are just as many reasons - and then some - why it is bad.

    You should get into the habit of understanding your data types and should convert the types as required: having the compiler 'guess' will introduce innumerable and hard-to-find errors into your code. There are no 'shortcuts': just because you don't write the code to perform the conversion explicitly, the computer will perform it anyway, behind the scenes.

    So, in this case, the IDE is recommending the CStr() function to perform the conversion. There is also the .ToString() method on pretty much any object which you should start getting familiar with, as well as the overloads of that method allowing you to format a numeric value, for example, to the correct display format.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  3. #3
    Hyperactive Member DavesChillaxin's Avatar
    Join Date
    Mar 2011
    Location
    WNY
    Posts
    451

    Re: Label1.text = 0 error

    This may be because somewhere in your project, you have Option Strict set to ON. Which I would recommend in the first place so I wouldn't go changing it.

    MSDN
    "Because Option Strict On provides strong typing, prevents unintended type conversions with data loss, disallows late binding, and improves performance, its use is strongly recommended."

    Having said I would leave it. To fix your problem you need to explicitly covert the integer to a string data type. There are many ways of accomplishing this. The way I use most is the .ToString() method available through out the .net framework.

    For example:

    Code:
    Label1.Text = 0.ToString()
    Or
    Code:
    Dim int As Int32 = 0
    Label1.Text = int.ToString()
    Or use you could use the Convert class (System.Convert)
    MSDN: http://msdn.microsoft.com/en-us/libr...m.convert.aspx

    Here you would call the method that accepts an object to be converted into a string data type.

    Code:
    Label1.Text = System.Convert.ToString(0)
    Please rate if my post was helpful!
    Per favore e grazie!




    Code Bank:
    Advanced Algebra Class *Update | True Gradient Label Control *Dev | A Smarter TextBox *Update | Register Global HotKey *Update
    Media Library Beta *Dev | Mouse Tracker (Available in VB.net and C#.net) *New | On-Screen Numpad (VB.net) *New

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Posts
    18

    Re: Label1.text = 0 error

    So in the past i would of had...

    Label1.text = 3
    Label2.text = 2
    Label3.text = label1.text + label2.text

    If i want the result to be 5, but your saying i should have ".tostring()" at the end of them both?

  5. #5
    Hyperactive Member
    Join Date
    Jan 2012
    Location
    Florida
    Posts
    285

    Re: Label1.text = 0 error

    To do this you would have to write it like this:

    vb.net Code:
    1. Label1.Text = "0"
    2. Label2.Text = "1"
    3. Label1.Text = Cstr(Cint(Label1.Text + Label2.Text))
    4. 'Possibly Label1.Text = Cstr(Cint(Label1.Text) + Cint(Label2.Text))

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Posts
    18

    Re: Label1.text = 0 error

    Realy? Wow. I think i prefered the old method.

    Thank you all for the very useful and informative replies.

  7. #7
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,289

    Re: Label1.text = 0 error

    Quote Originally Posted by sico View Post
    So in the past i would of had...

    Label1.text = 3
    Label2.text = 2
    Label3.text = label1.text + label2.text

    If i want the result to be 5, but your saying i should have ".tostring()" at the end of them both?
    You can't perform math calculations on text. If you could, what would be the result of "apple" + "orange" = ???. For that reason, if your text is a representation of a number, you have to convert it to a number first before performing any calculation on it.
    The fact that you can write, with option strict OFF:
    Code:
    Label1.text = 3
    Label2.text = 2
    Label3.text = label1.text + label2.text
    simply because the IDE implicitly converts the strings to numbers for you. However, implicit conversions might yield undesired results. Using the same example code above, Label3 will show 32 as the result instead of 5. Why? Because in the 1st 2 lines the IDE implicitly converts the integers 3 and 2 to string since it detects that the label.text property expects a string, therefore the integer 3 and 2 are converted to strings and assigned to the labels. However, when you get to the 3rd line, label1.text and label2.text are already string, and since label3.text expects a string, the IDE won't bother to convert the those strings "3" and "2" back to integers. The + operator when used with strings becomes a concatenation operator, therefore it concatenates the 2 strings "3" and "2" which yields "32" and assign that to Label3.Text.
    As a rule of thumb, always turn option strict ON, which prevent the IDE from performing implicit type conversions. And since the IDE is asked not to do type conversions on its own, you will have to tell it how to convert one type to another >>> Less chance of getting runtime errors like the one above.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  8. #8
    Hyperactive Member DavesChillaxin's Avatar
    Join Date
    Mar 2011
    Location
    WNY
    Posts
    451

    Re: Label1.text = 0 error

    It seems more complicated, but in practice it makes sense. Its just knowing what object types you're working with, and being sure everything you're tossing back and forth are compatible with one another. It allows for more efficient code, easier readability, and peace of mind knowing you're code will always do what you're expecting it to do.

    I would also recommend reading up on DirectCast() and TryCast(). These can also be very helpful too when handling a lot of type conversions. Also I've come to understand from my research that DirectCast(), when used correctly is the fastest most efficient way of converting types. Although I could be mistaken...can't always believe everything you read on the internet
    Please rate if my post was helpful!
    Per favore e grazie!




    Code Bank:
    Advanced Algebra Class *Update | True Gradient Label Control *Dev | A Smarter TextBox *Update | Register Global HotKey *Update
    Media Library Beta *Dev | Mouse Tracker (Available in VB.net and C#.net) *New | On-Screen Numpad (VB.net) *New

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Label1.text = 0 error

    The "old method" may be simpler, but there are all kinds of problems (some of them major) that you haven't thought about. Some of them are:
    • for numbers over 1000, do you want to display a comma? (eg: "1,000") If so, do you want it to always be a comma, or do you want it Region aware? (some parts of the world use a dot instead, and a comma for the decimal point)
    • Do you want positive numbers to have a space in front of them (so that they 'line up' with negative numbers)?
    • Do you want to display a decimal point and zeros after it?
    That is just a small sample of the things to be concerned about... and we've seen many program failures over the years because people made assumptions about these things, or didn't think about them at all.

    Doing the opposite kind of conversion (such as your Label3.text = label1.text + label2.text ) is dramatically more complicated, and leads to many more types of errors (and much more frequently). Ignoring the massive ambiguity (if the labels contain "1" and "3", do you want "4" or "13"?), just think about what 'should' happen when you have values like " 0", "a", "1.5", and "(1)".


    Having Strict on means that you at least get warned about where problems are likely to happen, and to some degree need to think about them - which means you have a good chance to deal with them while you are writing the code, rather than having to come back to an old project in a few months hunting thru the entire code because your user(s) are angry about your program failing (or worse, using the wrong values without warning).

    Outside of VBScript (where you often don't have an option), there is no valid reason to use what you describe as "the old method" rather than the proper methods - and that applies all the way back to the earliest versions of VB.... however, we have all been guilty of taking the lazy option from time to time!

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Posts
    18

    Re: Label1.text = 0 error

    I was somewhat aware of cstr() and cint(), but as not using them worked and always has, i've never realy used them. You all make very valid points as to why i should do things correctly, but up until now i've never needed to do it correctly, in the situations i've been in so far i've tended to use the label as a reference for me, to ensure what i'm doing is correct. So the decimals, commas in the thousands and looking pretty have never been issues. I can imagine my next project will require all three though now that you've mentioned them.

    Thanks again for the lessons.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width