Mike_R Posted December 6, 2004 Posted December 6, 2004 I was stunned to find that the following was a problem in VB.Net: Dim bytes() As Byte = {1, 2, 3, 4, 5} Dim i as Integer For i = 0 To bytes.Length - 1 bytes(i) += 1 ' <-- Compile Time Error Next The result is a squiggly blue line under the "+=" part and the error message read:Option Strict On disallows implicit conversions from 'Integer' to 'Byte' This was puzzling, so just to make sure that I wasn't going insane, I tried changing the offending line to:bytes(i) = bytes(i) + 1 The result was the same error message and the squiggly line this time was under "bytes(i) +1". However, the following is just fine in C# however:byte[] bytes = {1,2,3,4,5}; int i = 0; for(i=0; i<bytes.Length; i++) { bytes[i] +=1; } Does anyone have an idea of what might be going on in the VB.Net example? It seems kind of ludicrous to have to make use of the Convert class to convert a Byte to istelf?? Thanks in advance... Quote Posting Guidelines Avatar by Lebb
Mike_R Posted December 6, 2004 Author Posted December 6, 2004 (edited) Ok, my bad... I think the problem is with the '1' and not with the 'Bytes(i)' aspect. That is, '1' is being interpreted as an Integer, by default. Unfortunately, I don't think there is a "shortcut" conversion symbol for the Byte data type. That is, for example, I could do the following for Chars, or double, respectivelyDim myChar As Char = "a"c Dim myDouble As Double = 1R It would make sense that Byte could make use of 'b' or 'B', but, alas, this does not work. :( I wonder how C# gets around this 'Option Strict' issue... I guess the + operator is simply overloaded to handle Byte + Integer. Then I wonder why VB does not overload their operator as well? It really seems that VB is taking this "Option Strict" a tad to far? Oh well, a bummer for VB I guess... Edited December 6, 2004 by Mike_R Quote Posting Guidelines Avatar by Lebb
Jaco Posted December 7, 2004 Posted December 7, 2004 It's actually worse than that - if you declare a variable of type "System.UInt32" (or UInteger in VB 2005) you can't set this to a value without some extra coding: Dim x As System.UInt32 = 0 'no way ! x = 1 'no way ! Option Strict is seriously flawed in how it's fanatically applied by the compiler. Quote
Mike_R Posted December 7, 2004 Author Posted December 7, 2004 Yeah, anything smaller than an Integer is narrowing and so this error kicks in. :( Bytes and Shorts come to mind. Integer, Long, Single, Double, Decimal should all be fine. I guess UInt32 is a no-go because the native Integer evaluation on the right-hand side (RHS) could be negative value and so this is also narrowing. It's a bit odd, and I'm starting to get used to it. I wish there were more shortcut conversion symbols like "c" for Char and "R" for Double. A "b" for Byte would seem nice at a minimum. The truth is, codes for all values that are narrower than Int32 would seem more important than those that are wider, for the latter can rely on implicit conversion... Hmmm.... I also wonder why C# does not have a problem with Byte++ or Byte += 1, whereas these choke in VB.Net, tripping over Option Strict. I, again, suppose that the '+' operator is overloaded, but then I wish they had also overloaded VB.Net's '+' operator in the same fashion. Oh well. Quote Posting Guidelines Avatar by Lebb
Mike_R Posted December 7, 2004 Author Posted December 7, 2004 Ok, more nonsense... Note that the following is 1005 fine in C#: char c = 'a'; int i = c; MessageBox.Show(i.ToString()); // Returns "97" But the following chokes in VB.Net:Dim c As Char = "a"c Dim i As Integer = c ' <-- Compile Time Error MessageBox.Show(i.ToString()) The correction needed to use:Dim i As Integer = Convert.ToInt32(c) Pretty ungainly though. I wonder why they made VB "stricter" than C#? Oh well... Quote Posting Guidelines Avatar by Lebb
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.