|
-
Jul 18th, 2022, 03:44 AM
#1
Thread Starter
PowerPoster
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.
-
Jul 18th, 2022, 04:33 AM
#2
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:
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.
-
Jul 18th, 2022, 11:36 AM
#3
Thread Starter
PowerPoster
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.
-
Jul 18th, 2022, 12:11 PM
#4
Re: unexpected behavior with ctype and nullable byte (byte?) variable
 Originally Posted by BruceG
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.
-
Jul 18th, 2022, 03:12 PM
#5
Re: unexpected behavior with ctype and nullable byte (byte?) variable
 Originally Posted by PlausiblyDamp
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
-
Jul 18th, 2022, 08:53 PM
#6
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#.
-
Jul 19th, 2022, 12:00 AM
#7
Thread Starter
PowerPoster
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|