ide explicit

Cor

Active Member
Licensed User
Longtime User
compiler gives no error

only find fault when running program

B4X:
sub test
dim sectionname as string

sectioname="1"   <--- gives no fault when compiling
end sub

grCor
 
Last edited:

Cor

Active Member
Licensed User
Longtime User
I get no error when compiling, only when running program it does not work as supposed to be.


because i set sectioname="1" but there is no sectioname
only sectionname ' with a n

only i did make a typo myself at the IDE

for short:
Option explicit
Forces explicit declaration of all variables in a file.

that's where the problem is

I hope you understand

thanks

grCor
 

klaus

Expert
Licensed User
Longtime User
There are 3 type mismatches:
1) sun test > Sub test
2) Dim sectionname as string sectioname="1" the variable names are not the same
3) sectioname="1" <--- gives no fault when compiling should be
sectioname="1" ' <--- gives no fault when compiling

2) doesn't through an error in the IDE, nor does it when running the program in the emulator, after correcting 1) and 3).

But this code throughs an error in the IDE!
B4X:
[FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]Sub [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]test[/SIZE][/FONT]
[/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] sectionname [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#008b8b][FONT=Courier New][SIZE=2][COLOR=#008b8b][FONT=Courier New][SIZE=2][COLOR=#008b8b]String[/COLOR][/SIZE][/FONT]
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]sectioname=[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000]"1"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000]' <--- gives no fault when compiling[/COLOR][/SIZE][/FONT]
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]Lable1.Text=sectioname[/SIZE][/FONT]
[/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]End Sub[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]

Best regards.
 

Cor

Active Member
Licensed User
Longtime User
2) Dim sectionname as string sectioname="1" the variable names are not the sam

that's just the problem

I want Option explicit

so all variables must be defined

Now I can easily make mistakes, no compiler but fails when i run my program
because sectionname with an n was not defined

Just look at this: Option explicit
and you will know what i mean, i hope so

grCor
 

Cor

Active Member
Licensed User
Longtime User
The compiler throws an error if you try to use a variable that was not declared or not assigned a value. It doesn't throw an error if you assign a value to a new undeclared variable. This is not an error and there is no must to declare variables (though it is recommended).


>It doesn't throw an error if you assign a value to a new undeclared variable.
Because i can easily make a mistake now

That's why Option explicit is needed

grCor
 

JesseW

Active Member
Licensed User
Longtime User
I agree with Cor that the possibility exists for a programmer to get their variable names wrong from time to time, which can lead to one pulling their hair out trying to chase it down. VB6 had an Option Explicit statement that told the runtime and compiler to throw an error FOR ANY ATTEMPTED USE on an un-dimmed variable. I used it all the time, for just such an occasion.

I think its a good idea, because it doesn't force a new way of doing things for those that don't need or want its functionality, or break any existing code, and is extremely beneficial to those that do.

Jesse
 
Top