Results 1 to 7 of 7

Thread: unexpected behavior with ctype and nullable byte (byte?) variable

  1. #1

    Thread Starter
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657

    unexpected behavior with ctype and nullable byte (byte?) variable

    Hello - I had an issue that I have now corrected, but I'd like to understand why the original way I had this behaved as it did.
    Original way:
    Code:
        Dim b As Byte?
        Dim s As String = " "     ' s will either contain a blank space or a digit between 0 and 3
        b = CType(if(s = " ", Nothing, s), Byte?)
    When the above code executed, I expected the variable b to be null (Nothing), however, it contained zero (0).

    The fix:
    Code:
        Dim b As Byte?
        Dim s As String = " "     ' s will either contain a blank space or a digit between 0 and 3
        b = If(s = " ", Nothing, CType(s, Byte?))
    When the above code executed, b was Nothing as one would expect.

    The question is why did the first example return 0 rather than Nothing?
    "It's cold gin time again ..."

    Check out my website here.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: unexpected behavior with ctype and nullable byte (byte?) variable

    The If operator that you're using is effectively generic, in that the two potential return values must be the same type or one must be a type derived from the other. In your first code snippet, you have this:
    vb.net Code:
    1. if(s = " ", Nothing, s)
    so that If operator is returning a String, i.e. either a null reference of type String or the value of s. The CType operator is then converting that String reference to a Byte? and it appears that the result of using CType to convert a null String reference to a nullable numeric type is always zero. I tried it with Integer? as well and got the same result. In the second case, because the last argument to If is type Byte?, the second argument is inferred to be that type too, so that Nothing is going directly to type Byte?, rather than to String first and then to Byte?. It seems to be a bit of an idiosyncratic behaviour but it is what it is.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657

    Re: unexpected behavior with ctype and nullable byte (byte?) variable

    Thanks, jmcilhinney. Your response further confirms and clarifies what has been observed here.
    Easier to understand is the behavior of the If function ("two potential return values must be the same type ...").
    The CType situation is, as you say, more idiosyncratic: "it appears that the result of using CType to convert a null String reference to a nullable numeric type is always zero". Prior to posting on here, I did try to find an "official statement" on this behavior but was unable to do so.
    "It's cold gin time again ..."

    Check out my website here.

  4. #4
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,958

    Re: unexpected behavior with ctype and nullable byte (byte?) variable

    Quote Originally Posted by BruceG View Post
    Thanks, jmcilhinney. Your response further confirms and clarifies what has been observed here.
    Easier to understand is the behavior of the If function ("two potential return values must be the same type ...").
    The CType situation is, as you say, more idiosyncratic: "it appears that the result of using CType to convert a null String reference to a nullable numeric type is always zero". Prior to posting on here, I did try to find an "official statement" on this behavior but was unable to do so.
    I have always tended to avoid CType, instead I prefer the Parse / TryParse methods provided by a lot of the built in types. Also the Convert class might save a more expected behaviour.

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: unexpected behavior with ctype and nullable byte (byte?) variable

    Quote Originally Posted by PlausiblyDamp View Post
    I have always tended to avoid CType, instead I prefer the Parse / TryParse methods provided by a lot of the built in types. Also the Convert class might save a more expected behaviour.
    Agreed.

    Code:
                ' s will either contain a blank space or a digit between 0 and 3
                If s.Length = 1 AndAlso Byte.TryParse(s.Trim, Nothing) Then
                    b = Byte.Parse(s)
                End If
    Tested with

    Code:
            For Each s As String In {" ", "0", "1", "2", "3"}
                ' s will either contain a blank space or a digit between 0 and 3
                If s.Length = 1 AndAlso Byte.TryParse(s.Trim, Nothing) Then
                    b = Byte.Parse(s)
                End If
            Next
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

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

    Re: unexpected behavior with ctype and nullable byte (byte?) variable

    I would tend to agree with my fellows above about avoiding CType. In fact , I never use CType directly. I do use CStr, CInt, etc, to perform a cast, as it's more succinct than using DirectCast for those supported types, but I never use them or CType to perform a conversion. I think that that's partly because I have long tended to use C# as much or more than VB and there's no CType in C#, so I tend to write the same code in VB as I would in C#.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657

    Re: unexpected behavior with ctype and nullable byte (byte?) variable

    Thanks for all your input gentlemen, good advice all around.
    "It's cold gin time again ..."

    Check out my website here.

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