Option _Explicit Keyword(s) of day XXX:
#20
Here is one more example of a difficulty I have run into with Option _Explicit, and it has to do with Local variables v's Global or Dim v's Dim Shared I know the fix is just Dim Shared every variable but keeping with the theme of the use of a "throw away variable" as in a counter situation, it also pops up with Locally declared variables. With Option _Explicit you pretty well need to give up on a locally declared variable.

Code: (Select All)
'Option _Explicit

Dim Shared x
Dim x1

'Main Module
x = 1
x1 = 100
Print "Main Module values"
Print " Value of x = "; x
Print "Value of x 1 = "; x1
Print "Value of x2 = "; x2
Print "Value of x3 = "; x3
Print

Subx2
Subx3





Sub Subx2
    Dim x2
    x2 = 200
    Print
    Print "Subx2 Module values"
    Print " Value of x = "; x
    Print "Value of x 1 = "; x1
    Print "Value of x2 = "; x2
    Print "Value of x3 = "; x3
    Print
End Sub



Sub Subx3
    Dim x3
    x3 = 300
    Print
    Print "Subx3 Module values"
    Print " Value of x = "; x
    Print "Value of x 1 = "; x1
    Print "Value of x2 = "; x2
    Print "Value of x3 = "; x3
    Print
End Sub
Without Option _Explicit all the variable values work as expected.

For me personally, I think all my objections to Option _Explicit are simply a resistance to change which is not a good trait in a modern day programmer/coder. The thing is that Option _Explicit does an excellent job in catching misspelled variables which are very much an issue I have and a time waster to track down. So how difficult is it really for me to declare all variables as Dim Shared along with adding Option _Explicit at the beginning of all my code. It's a lot less time than trying to track down that one misspelled variable. Thus I morn the loss of the throw away local variable. Excellent excuse for another beer.
Reply


Messages In This Thread
RE: Option _Explicit Keyword(s) of day XXX: - by Dimster - 06-09-2023, 02:31 PM



Users browsing this thread: 2 Guest(s)