Comment On Thank Goodness there's "Option Strict Off"

Without Option Strict Off, the compiler wouldn't even let you try to stuff three different return values into a boolean value, as Jeff Atwood's colleague likes to do ... [expand full text]
« PrevPage 1 | Page 2Next »

Re: Thank Goodness there's "Option Strict Off"

2004-12-09 12:33 • by Stan Rogers
No. Freakin'. Way.

Re: Thank Goodness there's "Option Strict Off"

2004-12-09 12:47 • by
my god... would the non-zero values actually be preserved in that case, (e.g. does the boolean actually store a short integer) or does it get converted into True?


Re: Thank Goodness there's "Option Strict Off"

2004-12-09 12:51 • by Jeff S
implicitly the 2 and the 1 both get converted to "True", I believe.

I LOVE option explicit -- it is the best thing to hit VB since sliced bread.

I'd be curious to see how many VB.NET developers use it ... it still frightens me that so many poeople don't even use Option Explicit !!  (in both .NET and regular VB6 and even VBScript)

Re: Thank Goodness there's "Option Strict Off"

2004-12-09 12:53 • by
I don't know why you non-VBers like to mock VB programmers so much.  Just deal with your jealousy.  Our booleans go up to 2 and our amplifiers go up to 11.  Sure, I know what you're thinking, "if he used SELECT CASE he could go even higher..."  That's where you're WRONG bucko!  Nested If..Then statements are the only way to take our ultimate programming powers to the next level - boolean = 3!! 

Re: Thank Goodness there's "Option Strict Off"

2004-12-09 12:53 • by Jeff S
26447 in reply to 26445
oops -- I meant "Option Strict" in the first instance... sorry for the confusion.


(i really wish we could have a 5 minute window to edit posts!!)

Re: Thank Goodness there's "Option Strict Off"

2004-12-09 13:01 • by
What is the return value if the user is not in Admin, Training, or Supervisor?

Re: Thank Goodness there's "Option Strict Off"

2004-12-09 13:03 • by
And of course there's the Win32 call TrackPopupMenu:
BOOL TrackPopupMenu(
    HMENU hMenu,
    UINT uFlags,
    int x,
    int y,
    int nReserved,
    HWND hWnd,
    HWND prcRect
);
"If you specify TPM_RETURNCMD in the uFlags parameter, the return value is the menu-item identifier of the item that the user selected. If the user cancels the menu without making a selection, or if an error occurs, then the return value is zero.

If you do not specify TPM_RETURNCMD in the uFlags parameter, the return value is nonzero if the function succeeds and zero if it fails. To get extended error information, call GetLastError."


Re: Thank Goodness there's "Option Strict Off"

2004-12-09 13:04 • by
Let me just say.... I am a C# developer who went into a VB.NET firm to consult and make recommendations...... I turned Option Explicit ON - and VS 2003 said there were "too many errors to display"  ;)   On 1 WINDOWS FORM.... NOT EVEN COUNTING THE OTHER 30+ forms. (Yes 30+ forms of repetative code...) No modules, and no user controls. ;)

needless to say... my recommendation was to REWRITE everything using ACTUAL .NET rather then VB6 with VB.NET functions and methods ;)

Chris

Re: Thank Goodness there's "Option Strict Off"

2004-12-09 13:04 • by Stan Rogers
26451 in reply to 26447
You'll just have to learn to be publicly humiliated and apologetic like the rest of us, Jeff. Isn't it incredible how many run-time mysteries become compile-time wavy underlines with circles and arrows and flashing lights and sound effects when you use Option Explicit/Option Strict?

Re: Thank Goodness there's "Option Strict Off"

2004-12-09 13:06 • by
26452 in reply to 26450

OOPs i meant STRICT too! see what you did??  Damn VB.

-Chris

Re: Thank Goodness there's "Option Strict Off"

2004-12-09 13:09 • by
Well, you see, there's false, true, and REALLY true.

Re: Thank Goodness there's "Option Strict Off"

2004-12-09 13:13 • by

i bet all you C# guys out there are jealous of us VB guys with our Option Strict Off possibilities. Forget binary, man... with Option Strict Off, we can code in Trinary!

[:D]

the worst thing happening here is that when he doesn't return any value, it actually gets converted to "False" automatically... i hate that... if i return "Nothing" as a boolean, then I want it to be NOTHING, not False...

Re: Thank Goodness there's "Option Strict Off"

2004-12-09 13:20 • by Stan Rogers
26456 in reply to 26455
If you return "Nothing" as a Boolean, it's a String. That'll start a whole new thread.....

Re: Thank Goodness there's "Option Strict Off"

2004-12-09 13:28 • by RJ
This is a stunning day in the history of computer science.  A decompiler can actually generate better code than the original developer.  Compile that fragment and Lutz Roeder's Reflector will generate this:
Friend Function get_CheckUserRole() As Boolean
Dim flag1 As Boolean
Dim user1 As SystemUser = SystemUser.CurrentUser
If user1.AssertRole(RoleTypes.Admin) Then
Return False
End If
If user1.AssertRole(RoleTypes.Training) Then
Return True
End If
If user1.AssertRole(RoleTypes.Supervisor) Then
Return True
End If
Return flag1
End Function

Truly awe inspiring.

Re: Thank Goodness there's "Option Strict Off"

2004-12-09 13:39 • by
26460 in reply to 26448
:
What is the return value if the user is not in Admin, Training, or Supervisor?

False, so a user without role has full rights (False = 0 = Admin)

Re: Thank Goodness there's "Option Strict Off"

2004-12-09 13:50 • by
26461 in reply to 26460
Holy crap, you're right.  Until you said that I didn't realize how WTF this code really was.

bj

Re: Thank Goodness there's "Option Strict Off"

2004-12-09 14:01 • by DJDanny
Alex Papadimoulis:

Without Option Strict Off, the compiler wouldn't even let you try to stuff three different return values into a boolean value, as Jeff Atwood's colleague likes to do ...

Friend [snip.....]




End Property


The WTF here has got to be the word 'Friend'. I mean, do VB 'programmers' need to be mollycoddled that much?!

Next you'll tell me that static variables/methods are called something like Shared in VB, so you can share them with your Friends and all be happy!

Re: Thank Goodness there's "Option Strict Off"

2004-12-09 14:04 • by
VB's "Option Strict" is like perl's "use strict" - it should be on by default.

Re: Thank Goodness there's "Option Strict Off"

2004-12-09 14:09 • by Alex Papadimoulis
26464 in reply to 26462
DJDanny:
The WTF here has got to be the word 'Friend'. I mean, do VB 'programmers' need to be mollycoddled that much?!

Next you'll tell me that static variables/methods are called something like Shared in VB, so you can share them with your Friends and all be happy!


It isn't the VB team who invented friend -- it's a C++ oddity. Although my Computer Science background gets offended, I find the modifiers MustInherit, Overloads, Overrides, and NotOverridable much easier to read and see what's going on than the "semi-colon" eqivilants of abstract, <none>, <none>, and final ...

Re: Thank Goodness there's &quot;Option Strict Off&quot;

2004-12-09 14:23 • by
Actually all of you are missing the real culprit here.  If only the CLR defined a Trilean data type, all would be just fine.

Geesh.

Bruce Johnson
http://www.objectsharp.com/blogs/bruce   

Re: Thank Goodness there's &quot;Option Strict Off&quot;

2004-12-09 14:29 • by DJDanny
26468 in reply to 26464
Alex Papadimoulis:


It isn't the VB team who invented friend -- it's a C++ oddity. Although my Computer Science background gets offended, I find the modifiers MustInherit, Overloads, Overrides, and NotOverridable much easier to read and see what's going on than the "semi-colon" eqivilants of abstract, <none>, <none>, and final ...


 I'll have to agree with you on this one. As much as I hate VB for its 'access for all' policy (anybody familiar with the new policy of English Universities will know EXACTLY what I'm talking about), C++ almost has an 'access for none' policy.

Although it's an undoubtedly very powerful and fast language, it is over-complicated, and those that use it revel in its ability to perform such wonderous feats as multiple-inheritance, among other miracles.

Java and C# are a wonderful halfway house, in that they are fairly simple to pick up, and code is actually human readable (unlike a lot of C++), but they are also not overly-simplified like VB so that there are less 'novices' (for want of a better word) using them to program things beyond their abilities, such as the guy who was using the WebClient class and clearly didn't have the first idea about HTML or HTTP (see one of my posts on a thread from a couple of days ago for the reference).

Re: Thank Goodness there's &quot;Option Strict Off&quot;

2004-12-09 14:31 • by DJDanny
26469 in reply to 26466
:
Actually all of you are missing the real culprit here.  If only the CLR defined a Trilean data type, all would be just fine.


 


Boolean/bool is tristate anyway, surely? true, false and null

Re: Thank Goodness there's &quot;Option Strict Off&quot;

2004-12-09 14:33 • by Jacob K
Anyone ever heard of the "quantum bool" (or something similar to that name)? It's some research project in which there can be a bool that's half on. Maybe he's just preparing for it? We could learn from him...












... The project does exist, but that rest is sarcasm if you haven't yet figured that out.

Re: Thank Goodness there's &quot;Option Strict Off&quot;

2004-12-09 14:36 • by David Crowell
Hmmm.... Wouldn't this be easier, and fix the case of not belonging to a role causing the same result as an admin:

 

[code language="c#"]
internal bool CheckUserRole
{
 get
 {
  return ! SystemUser.CurrentUser.AssertRole(RoleTypes.Admin);
 }
}
[/code]

Yeah, I know, it's C# now, it would be easy to do in VB also.
 

Re: Thank Goodness there's &quot;Option Strict Off&quot;

2004-12-09 14:40 • by David Crowell
26474 in reply to 26470

Anyone ever heard of the "quantum bool" (or something similar to that name)? It's some research project in which there can be a bool that's half on. Maybe he's just preparing for it? We could learn from him...


Or the upcoming "bool?" in .NET 2.0 (nullable).  That would be tri-state.

Re: Thank Goodness there's &quot;Option Strict Off&quot;

2004-12-09 14:45 • by JoeNotCharles
26475 in reply to 26462
DJDanny:
The WTF here has got to be the word 'Friend'. I mean, do VB 'programmers' need to be mollycoddled that much?!


Reminds me of my favourite g++ warning:


vmobj.h:1757: warning: `class CVmObjTable' only defines a private destructor and has no friends

Poor, poor, suicidal class CVmObjTable. I wish I could give it a hug.


(From http://groups-beta.google.com/group/rec.arts.int-fiction/browse_thread/thread/e59f834f272a596/d60167720a76102b - dear lord, the new Google Groups interface is bad. Google! What happened to you?)

Re: Thank Goodness there's &quot;Option Strict Off&quot;

2004-12-09 14:46 • by alexb
26476 in reply to 26457
RJ:
This is a stunning day in the history of computer science.  A decompiler can actually generate better code than the original developer.


Compilers have been able to generate "better" code than humans for ages now.

As far as booleans go, Microsoft has been pretty consistent in their implementations throughout their programming environments (databases or otherwise):

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vaoriDataTypesVBA.asp

"When numeric data types are converted to Boolean values, 0 becomes False and all other values become True. "

Maybe the programmer was just using 1 and 2 for his/her own reference within the scope of that function. Or it could be one of those temporarily invisible bugs just waiting to bite you one fine day.

Re: Thank Goodness there's &quot;Option Strict Off&quot;

2004-12-09 14:54 • by
26477 in reply to 26463
:
VB's "Option Strict" is like perl's "use strict" - it should be on by default.


It was on by default in the first Betas, but too many people complained that their code fell over. Unfortunately, Microsoft decided to change the default rather than sit those people down and explain patiently that if their code fell over it was a crock of s*** and they should stop calling themselves programmers.

I guess it goes to show there are some times when you shouldn't listen to the users.

Re: Thank Goodness there's &quot;Option Strict Off&quot;

2004-12-09 15:00 • by
Reminds me of a developer I used to work with who swore left and right that he'd come across a negative bit. That one still makes me chuckle.

Re: Thank Goodness there's &quot;Option Strict Off&quot;

2004-12-09 17:33 • by mjwills
26489 in reply to 26469
DJDanny:


Boolean/bool is tristate anyway, surely? true, false and null


Apparently you don't program in .NET. That is not the case.

Re: Thank Goodness there's &quot;Option Strict Off&quot;

2004-12-09 17:36 • by DJDanny
26491 in reply to 26489
mjwills:


Apparently you don't program in .NET. That is not the case.


ok, fair enough, then. true, false and unassigned. I make that a tristate!

Re: Thank Goodness there's &quot;Option Strict Off&quot;

2004-12-09 17:40 • by
26492 in reply to 26489
mjwills:
 DJDanny wrote:


Boolean/bool is tristate anyway, surely? true, false and null


Apparently you don't program in .NET. That is not the case.


I was under the impression that in VB.NET, if you do not return a value from a method, the compiler creates some MSIL which simply returns a variant - which is different from null. The C# compiler, however, does not allow for such nonsense.

Re: Thank Goodness there's &quot;Option Strict Off&quot;

2004-12-09 18:31 • by andyandy
26494 in reply to 26492
"The C# compiler, however, does not allow for such "
In C# v2.0 there are nullable types, so you can write "bool?" and have true, false and null :)

Another funny bool error

2004-12-09 23:59 • by
I was writing an interface layer for VBA once, and found an interesting bug.   I was trying to make sure that I could set the value true and false, as well as query it to get correct values.  My VBA for this was something along the lines of:

Boolean temp = iforget.getState()
print temp
temp = Not( temp )
iforget.setState( temp )

( I haven't touched VB since I coded the project, so that's probably not correct.  But you get the idea.. )

Here's the intersting part.  If the state was initially false, it'd be set to true.  If the state was initially true, it'd be set to true.

In VBA, the bool value for true is 0xFFFF and the value for false is 0x0000.  The Not function does a bitwise flip - so 0xFFFF <-> 0x0000.  However, any non-zero value evaluates to true.

In C++, the bool value for true is 0x0001.  And that's what I was returning to VBA.  Unfortunately, the complement of 0x0001 is 0xFFFE, which also evaluates to true in VBA, and in C++.  I fixed my code, but there were plenty of other places in the app where it wasn't fixed.

Re: Thank Goodness there's &quot;Option Strict Off&quot;

2004-12-10 03:24 • by foxyshadis
26497 in reply to 26475
JoeNotCharles:
 DJDanny wrote:
The WTF here has got to be the word 'Friend'. I mean, do VB 'programmers' need to be mollycoddled that much?!


Reminds me of my favourite g++ warning:


vmobj.h:1757: warning: `class CVmObjTable' only defines a private destructor and has no friends

Poor, poor, suicidal class CVmObjTable. I wish I could give it a hug.


(From http://groups-beta.google.com/group/rec.arts.int-fiction/browse_thread/thread/e59f834f272a596/d60167720a76102b - dear lord, the new Google Groups interface is bad. Google! What happened to you?)

Re: Thank Goodness there's &quot;Option Strict Off&quot;

2004-12-10 03:33 • by DJDanny
26498 in reply to 26492
:


I was under the impression that in VB.NET, if you do not return a value from a method, the compiler creates some MSIL which simply returns a variant - which is different from null. The C# compiler, however, does not allow for such nonsense.


VB goes one step further, and doesn't even have methods. It has 'Functions' and 'Subs'. Surely that has to be another WTF! Either way, it's rubbish.

Re: Thank Goodness there's &quot;Option Strict Off&quot;

2004-12-10 03:57 • by azz
26499 in reply to 26473
Hey David, ur solution suck & is a bigger WTF than the original post!

Re: Thank Goodness there's &quot;Option Strict Off&quot;

2004-12-10 05:38 • by Tom
I've read through hundreds of lines of VB code before and have never seen a statement that begins with 'Dim'.  Anyone know what that does?

Re: Thank Goodness there's &quot;Option Strict Off&quot;

2004-12-10 05:57 • by DJDanny
26501 in reply to 26500
Tom:
I've read through hundreds of lines of VB code before and have never seen a statement that begins with 'Dim'.  Anyone know what that does?


Yes, it's a completely superfluous directive, of which there are many in VB ('as' and 'then' being just a couple more) which exist in VB and nowhere else which give VB 'programmers' a little helping hand because, let's face it, they need all the help they can get.

Re: Thank Goodness there's &quot;Option Strict Off&quot;

2004-12-10 05:57 • by
I .Net bool cant be null.  So its still only 2 state :-)

Re: Thank Goodness there's &quot;Option Strict Off&quot;

2004-12-10 07:41 • by DJDanny
26503 in reply to 26502
:
I .Net bool cant be null.  So its still only 2 state :-)


But it can be unassigned. So that's a third state.

Re: Thank Goodness there's &quot;Option Strict Off&quot;

2004-12-10 08:29 • by alexb
26505 in reply to 26503
DJDanny:
  wrote:
I .Net bool cant be null.  So its still only 2 state :-)


But it can be unassigned. So that's a third state.

Re: Thank Goodness there's &quot;Option Strict Off&quot;

2004-12-10 08:30 • by alexb
26506 in reply to 26505
Try it for yourself:

bool x;
bool y = x; // compiler error: "Use of unassigned local variable x"

Re: Thank Goodness there's &quot;Option Strict Off&quot;

2004-12-10 08:57 • by Mike R
26507 in reply to 26499
azz:
Hey David, ur solution suck & is a bigger WTF than the original post!


err spelling suck and err post is a bigger wtf than anything here.

Re: Thank Goodness there's &quot;Option Strict Off&quot;

2004-12-10 09:00 • by Mike R
26508 in reply to 26500
Tom:
I've read through hundreds of lines of VB code before and have never seen a statement that begins with 'Dim'.  Anyone know what that does?


The reason you don't see it is because everytime VB encounters it, it darkens the screen (hence the name Dim) . The strange thing is, option explicit seems to require it, which is why such a thing is not used.

(And in case anyone isn't aware this statement was completely toung-in-cheek)

Re: Thank Goodness there's &quot;Option Strict Off&quot;

2004-12-10 09:13 • by

How about:
True,
False, and
When I dream, I have a pony?

Dim statement

2004-12-10 09:37 • by
26510 in reply to 26500

It's a relic from GWBASIC where only arrays needed to be declared before using (hence, the DIMension statement). When QBasic came out, there were a few programs from MS to convert legacy GWBasic code. When VB came out, it was easy to bring QB logic. With VB.NET ... you get the idea.
no
I say, keep Dim as a synonym for the Declare. I think that'd be great ....  Declare FirstName As String ....  MS, go put it in the specs! Please [:^)]

Re: Thank Goodness there's &quot;Option Strict Off&quot;

2004-12-10 09:47 • by Jeff S
26511 in reply to 26498
DJDanny:
  wrote:


I was under the impression that in VB.NET, if you do not return a value from a method, the compiler creates some MSIL which simply returns a variant - which is different from null. The C# compiler, however, does not allow for such nonsense.


VB goes one step further, and doesn't even have methods. It has 'Functions' and 'Subs'. Surely that has to be another WTF! Either way, it's rubbish.

Re: Thank Goodness there's &quot;Option Strict Off&quot;

2004-12-10 09:48 • by Jeff S
26512 in reply to 26498
DJDanny:
  wrote:


I was under the impression that in VB.NET, if you do not return a value from a method, the compiler creates some MSIL which simply returns a variant - which is different from null. The C# compiler, however, does not allow for such nonsense.


VB goes one step further, and doesn't even have methods. It has 'Functions' and 'Subs'. Surely that has to be another WTF! Either way, it's rubbish.

Re: Thank Goodness there's &quot;Option Strict Off&quot;

2004-12-10 09:50 • by Jeff S
26513 in reply to 26498
DJDanny:
  wrote:


I was under the impression that in VB.NET, if you do not return a value from a method, the compiler creates some MSIL which simply returns a variant - which is different from null. The C# compiler, however, does not allow for such nonsense.


VB goes one step further, and doesn't even have methods. It has 'Functions' and 'Subs'. Surely that has to be another WTF! Either way, it's rubbish.
« PrevPage 1 | Page 2Next »

Add Comment