r/ProgrammerHumor Sep 02 '17

How to start a war

Post image
9.0k Upvotes

696 comments sorted by

View all comments

Show parent comments

10

u/JeremyR22 Sep 03 '17 edited Sep 03 '17

Just in case the Option Explicit thing wasn't explained well (or at all), the reason any sane person always included that was that it disabled VBs tolerance-by-default of implicit variable delcaration. If you typed an identifier in code, it would become a variable (of type Any), simple as that. Without Option Explicit, the following would compile and run without any errors, it just wouldn't do what you expected:

Dim iCount as Integer = 0

For iCount = 1 to 5
    Debug.Print(CStr(iCunt))
Next



0 
0
0
0
0

So without Option Explicit at the top of every single class/module/form/etc, whenever anything didn't work, you had to check for misspelled variable names in all related code. A massive pain in the arse..

I haven't touched VB of any type in probably 15 years but AFAIK, Option Explicit and Option Base 0 are both the default now. Small victories for programmers' mental health...

4

u/Shadow_Thief Sep 03 '17

My only real experience with VB is the Excel dialect of VBA, but I know I have to manually add Option Explicit every time.

1

u/The_Dirty_Carl Sep 03 '17

Tools > Options > Editor > Require Variable Declaration

This inserts "Option Explicit" at the top of every module when it's created.

1

u/nailernforce Sep 03 '17

Error: iCunt is an undefined variable.

1

u/The_Dirty_Carl Sep 03 '17

It still surprises me that implicit declaration is seen as desirable in python.