|
-
Aug 25th, 2015, 10:45 AM
#5
Re: Option Strict Problems
 Originally Posted by Inferrd
My apologies. I had always assumed & worked with Objects that boxed String types, or types that could be converted to Strings. Apparently it disallows any operand that is an Object Type, full stop. I presumably haven't noticed this behaviour before as I normally always cast the Object back to its underlying Type before working with it.
Live and learn 
Technically, the answer is "it only allows widening implicit conversions or explicit conversions". That's something we have to unravel, but it's useful to know because if you're just getting used to Option Strict it can seem like it chooses randomly.
An "implicit conversion" is when you try to convert a type without using any casts like CType() or CDbl(), or a conversion method like ToString(). These are implicit conversions:
Code:
Dim value As Integer = 14.7 ' Converts from Double to Integer
Dim otherWay As Double = 10 ' Integer to Double
Dim another As Byte = 8 ' This is actually Integer to Byte and sometimes frustrating!
Dim badIdea As Double = 12 - "14" ' Implicitly converts String to Integer, then performs subtraction
' returning an Integer, then implicitly converts to Double
You probably don't consider the first few to be "type conversions" at all! That's why implicit conversions are supported, if you had to express all of those as casts you'd waste a lot of time on code clutter. But not all of those conversions are legal with Option Strict. Why?
That's where "narrowing" conversions appear. It's useful to think of data types as having a "size", which is just "How many values can they represent?" Integers can represent 4 billion values, Bytes can hold 255. So Integer is "bigger" than Byte. More importantly, every Byte is also a value that Integer can represent, but Integer has many values such as -145 that Byte cannot.
So in terms of conversions, we say Integer is "wider" than Byte. Byte to Integer is a "widening" conversion, because we are converting to a type that can not only represent all Byte values, but has more. But Integer to Byte is a "narrowing" conversion, because there are Integers that cannot be represented as Byte.
So if Option Strict is on, we are forced to use explicit casts when we want to make narrowing conversions:
Code:
Dim value As Integer = CInt(14.7) ' Double is narrowed explicitly
Dim otherWay As Double = 10 ' But Integer is widened implicitly
Dim another As Byte = 8 ' This is a PITA in C# but VB seems to recognize it.
Dim badIdea As Double = 12 - Integer.Parse("14") ' We had to parse String to Integer before the subtraction
I'm sort of confused by that Byte conversion. Technically '8' is an Integer literal, and Intellisense confirms. Technically it's not safe to narrow Integer to Byte. It looks like the compiler's smart enough to do range checking and allow it. Feel blessed, C# makes this a pain.
So, the reason Object needed a string conversion is Object is technically the widest type of all: it can be anything! That means converting from Object to anything else is always considered "narrowing", even when you can prove that the only thing the Object could be is one particular type. Option Strict makes you be explicit with your dangerous casts, in the hopes that you think harder about them.
Now you know!
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
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
|