Prevent variable use without a declaration

Blitz3D Forums/Blitz3D Beginners Area/Prevent variable use without a declaration

Spikenspan(Posted 2003) [#1]
Hi all,

Is there a way to prevent the use of a variable without first declaring it? There is a feature like this in visual basic.

If the ide doesn't support this check, is there a plugin that could apply this verification on "check for errors" pushbutton click?

This would really be helpfull to spot variable scope errors (global vs local to a function).

Thanks.

p.s. I use the visualblitz ide.


WolRon(Posted 2003) [#2]
Do a search on this forum for similar requests. I think someone came up with a way using types.


Spikenspan(Posted 2003) [#3]
I have yet to find an example that illustrates mandatory variable declaration.

Thanks anyway.


LineOf7s(Posted 2003) [#4]
If you're keen to search for it, the phrase you're looking for is 'option explicit'. It comes up quite a bit.

Or did, until a clever chap by the name of Michael Reitzenstein came up with the Unofficial Blitzbasic Preprocessor , which amongst other things includes the ability to make Blitz make you explicitly declare variables.

Be aware that as it's written in C# you'll need the Microsoft .NET runtimes installed to use it.


soja(Posted 2003) [#5]
What wolron is reffering to is to put all your variables as fields of a custom type, like this:
Type variables
   Field var1, var2, var3
End Type
v.variables = New variables


But then, everytime you use a variable, you must precede it with v\. The advantage is that if you mispell it or don't declare it or something, it will give you an error: "Type field not found".

Personally, I prefer not to use it, but hey, some people do.


Spikenspan(Posted 2003) [#6]
LineOf7s: The "check for errors" works well and does alert any reference of undeclared variables, but the actual execution crashes, which is quite unfortunate.

soja: I guess I'm gonna go with your solution.

Thanks!!


Ross C(Posted 2003) [#7]
What i sometimes do to prevent me using similar variable names is name them like

tmp_loop=1
gfx_cube=CreateCube()
snd_bleep=LoadSound()

and so forth :)