Prevent variable use without a declaration
Blitz3D Forums/Blitz3D Beginners Area/Prevent variable use without a declaration
| ||
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. |
| ||
Do a search on this forum for similar requests. I think someone came up with a way using types. |
| ||
I have yet to find an example that illustrates mandatory variable declaration. Thanks anyway. |
| ||
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. |
| ||
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. |
| ||
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!! |
| ||
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 :) |