Results 1 to 13 of 13

Thread: Why do we need CStr?

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2013
    Posts
    9

    Why do we need CStr?

    i was given a practice code and when i executed the code everything went well. now when i deleted the CStr from a label it was assigned to, i noticed that the program did not do anything different, everything went well as it should have, unless i didnt check somewhere i was supposed to?

    for example in my code.....
    Code:
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ListBox1.Items.Add("COMP 2110")
            ListBox1.Items.Add("COMP 2120")
            ListBox1.Items.Add("COMP 2300")
            ListBox1.Items.Add("COMP 2315")
            ListBox1.Items.Add("COMP 2400")
            ListBox1.Items.Add("COMP 2501")
            ListBox1.Items.Add("GEIC 1000")
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            
            Dim num As Integer
            Dim Count As Integer
            Dim file_courses As IO.StreamWriter = IO.File.CreateText("Courses.dat")
            
    
            Label1.Text = CStr(ListBox1.Items.Count)//when i deleted this CStr nothing different happened? what was supposed to happen?
    
            For num = 0 To (ListBox1.Items.Count)
                file_courses.Write(ListBox1.Items(Count))
    
            Next
    
            file_courses.Close()
        End Sub
    End Class
    so again i ask why do we need or what is the importance of CStr?

  2. #2
    Member crispytoast's Avatar
    Join Date
    Oct 2013
    Posts
    45

    Re: Why do we need CStr?

    Cstr is a conversion function to convert other variable types to a string. In this case, a value for the number of listbox items is returned as an integer. A label can display an integer just fine in this case, but converting it to a string is good practice.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Why do we need CStr?

    The reason that it made no difference is that you have Option Strict Off, which is unfortuantely the default in VB.NET. In that case, the compiler will perform casts and conversions implicitly for you if it can. This does make things easier in a sense because you don't have to worry what data type things are a lot of the time. The fact that you're asking this question shows why that's an issue though: you don't know what data type things are. That is not an issue when they are compatible but is if they aren't.

    In this case, you're assigning a property value of type Integer to a property of type String. A String property can only hold a String, so that Integer must be converted. With Option Strict Off, that conversion will occur implicitly. With Option Strict On, most implicit conversions are not allowed so that would fail. The CStr is an explicit conversion to type String so you get a String object to assign to that String property.

    It's always a good idea to turn Option Strict On and leave it On except in the rare occasions that you need it Off. It feels like more work to begin with but it will help you write better code that is more efficient and less error-prone.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Why do we need CStr?

    Quote Originally Posted by crispytoast View Post
    A label can display an integer just fine in this case, but converting it to a string is good practice.
    No it can't. A Label can only display a String. The Integer is converted one way or another, either implicitly or explicitly.

  5. #5
    Member crispytoast's Avatar
    Join Date
    Oct 2013
    Posts
    45

    Re: Why do we need CStr?

    Quote Originally Posted by jmcilhinney View Post
    No it can't. A Label can only display a String. The Integer is converted one way or another, either implicitly or explicitly.
    A label can't display an integer value?
    Dim intExample As Integer = 5
    Label1.Text = intExample

    Really? Because I get a label with an output of 5.

    Ultimately, the text property is always a string and the conversion is just done behind the scenes, right?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Why do we need CStr?

    Quote Originally Posted by crispytoast View Post
    A label can't display an integer value?
    Dim intExample As Integer = 5
    Label1.Text = intExample

    Really? Because I get a label with an output of 5.
    Yes, really. As I have already said, that code will work with Option Strict Off because the Integer is implicitly converted to a String, but it IS converted to a String because that's all the Text property can hold. Try turning Option Strict On and see what happens. It will tell you that an implicit conversion is not allowed, proving that an implicit conversion IS taking place.

    Once you've turned Option Strict On, leave it On. If you turn it on in the IDE options then it will be On by default for all future projects.

  7. #7
    Member crispytoast's Avatar
    Join Date
    Oct 2013
    Posts
    45

    Re: Why do we need CStr?

    Quote Originally Posted by jmcilhinney View Post
    Yes, really. As I have already said, that code will work with Option Strict Off because the Integer is implicitly converted to a String, but it IS converted to a String because that's all the Text property can hold. Try turning Option Strict On and see what happens. It will tell you that an implicit conversion is not allowed, proving that an implicit conversion IS taking place.

    Once you've turned Option Strict On, leave it On. If you turn it on in the IDE options then it will be On by default for all future projects.
    Yeah, I have always had Option Strict / Explicit On out of habit. It's horrible practice to keep it off. I didn't know it was implicitly converting it behind the scenes though for labels, huh, cool! Can messageboxes take a raw integer?

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Why do we need CStr?

    Yes, it is implicitly converting, and implicit conversions tend to be slower than explicit conversions. If you are putting an integer into a string, then the integer has to become a string. There are times when Option Strict won't even complain about that. I'd say that it's a widening conversion like assinging an integer to a double, but whether an integer to a string is strictly widening or not....I'd have to look it up. Widening conversions don't cause trouble with Option Strict, so you don't have to explicitly convert an integer to a double.
    My usual boring signature: Nothing

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Why do we need CStr?

    Quote Originally Posted by crispytoast View Post
    Yeah, I have always had Option Strict / Explicit On out of habit. It's horrible practice to keep it off.
    If that code worked for you then you must have Option Strict Off because Option Strict On will disallow that conversion.
    Quote Originally Posted by crispytoast View Post
    I didn't know it was implicitly converting it behind the scenes though for labels, huh, cool! Can messageboxes take a raw integer?
    It's not a case of any particular type accepting or not accepting any other type. This has nothing to do with the Label. It's simply a matter of whether a conversion exists between the type being assigned and the type being assigned to and whether it's narrowing or widening. Even with Option Strict On you can perform widening conversions implicitly, e.g. assign an Integer value to a Long variable. In this case, an Integer value is being assigned to a string property and that's all that matters. There is a conversion between those types so it is allowed with Option Strict Off but it is a narrowing conversion so it's not allowed with Option Strict On. That applies anywhere that a String is expected, e.g. an argument passed to MessageBox.Show.

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Why do we need CStr?

    Why is integer to string a narrowing conversion? All integers can be converted to strings, but not all strings can be converted to integers, so integer to string should be widening.....though I'd say that it's really neither, since it's changing a fish to a fowl.
    My usual boring signature: Nothing

  11. #11
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Why do we need CStr?

    Quote Originally Posted by crispytoast View Post
    A label can't display an integer value?
    Dim intExample As Integer = 5
    Label1.Text = intExample

    Really? Because I get a label with an output of 5.

    Ultimately, the text property is always a string and the conversion is just done behind the scenes, right?
    5 will not be an integer, it will be a string. I can't see how you normally have strict on to say that.

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Why do we need CStr?

    Quote Originally Posted by Shaggy Hiker View Post
    Why is integer to string a narrowing conversion? All integers can be converted to strings, but not all strings can be converted to integers, so integer to string should be widening.....though I'd say that it's really neither, since it's changing a fish to a fowl.
    This MSDN topic:

    http://msdn.microsoft.com/EN-US/libr...d=hv.2%29.aspx

    says:
    Narrowing Conversions

    The standard narrowing conversions include the following:

    The reverse directions of the widening conversions in the preceding table (except that every type widens to itself)

    Conversions in either direction between Boolean and any numeric type

    Conversions from any numeric type to any enumerated type (Enum)

    Conversions in either direction between String and any numeric type, Boolean, or Date

    Conversions from a data type or object type to a type derived from it

  13. #13
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Why do we need CStr?

    Good enough then.
    My usual boring signature: Nothing

Tags for this Thread

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