Re: Will undefined varialbes test as False?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Top Spin (ToppSpin_at_hotmail.com)
Date: 10/04/04


Date: Mon, 04 Oct 2004 05:11:04 -0700

On Mon, 4 Oct 2004 03:53:16 -0400, "Rick Rothstein"
<rickNOSPAMnews@NOSPAMcomcast.net> wrote:

>> Can I be assured that an undefined variable will test
>> a False in an If statement?
>
>I would recommend that you never use "undefined" (the correct word here
>should be undeclared) variables in your program.

Yes, yes, yes. Always declare all variables. Of course. I guess I
should have provided more background. I was trying to be brief.

This is a situation were I can't declare the variable or not easily.
It's related to another question I posted recently about routines that
might be called both by the program and by me manually from the
Immediate window. These are debug routines that test a global switch
to determine whether to execute or not. The problem is that when I
call them from the immediate window, unless it is after a break, those
global variables are undeclared. I just want to set them up so that a
undeclared variable is treated the same as if debug is enabled.

So, if undeclared variables test as false, I will define "negative"
switches:

  Public const DebugIsOff as boolean = (True or False)

and then the routines will test

  If DebugIsOff then Exit Sub

If the routine is called from the program, the switch will have
whatever setting is had, but if called from the Immediate window, the
switch will be undeclared, the test will fail, and the routine will
execute.

If undeclared variables always test as True, I will do the opposite.

Geez. I wish you guys would just answer the question instead of always
lecturing on protocols -- or at least lecture and then answer the
question. ;-)

>If you don't declare a
>variable that you use in your program, VB will declare it for you
>(automatically and behind the scenes). The only problem with letting VB
>do this is that it uses the slowest variable type it has... a Variant...
>which also happens to require the most memory of any of the variable
>types to hold it. It is just a plain bad idea to do this. And there can
>be unexpected side effects to using Variants also. For example,
>
> a = Text1.Text
> b = Text2.Text
> c = a + b
>
>If your user types 1 in Text1 and 2 in Text2, what do you think will be
>assigned to the variable c? Try adding this statement to see if you were
>right.
>
> Debug.Print c
>
>VB has a built-in mechanism that can help you track undeclared
>variables. You can either type this statement
>
> Option Explicit
>
>at the beginning of every module you use in your program or you can let
>VB do it for you automatically. Click Tools/Options in VB menubar and
>then place a check in the CheckBox labeled "Require Variable
>Declaration". While you are there, you might also want to uncheck the
>one labeled "Auto Syntax Check". If that latter one is checked, you will
>be challenged immediately by VB to fix any errors that appear in your
>code as you make them. If you uncheck it, VB won't interrupt you to fix
>the error, but it will highlight the error line in red so that you know
>something is wrong with it that needs fixing.
>
>Rick - MVP

-- 
Running MS VB 6.0 Pro (SP5) on Win2K-SR2
For email, use Usenet-20031220 at spamex.com


Relevant Pages

  • Re: Will undefined varialbes test as False?
    ... >whether a function is being called from the Immediate Window or not. ... >with undeclared/undefined variables. ... Then I can set it on or off as I like and routines called from running ... These are debug routines that test a global switch ...
    (microsoft.public.vb.general.discussion)
  • Re: Will undefined varialbes test as False?
    ... whether a function is being called from the Immediate Window or not. ... with undeclared/undefined variables. ... These are debug routines that test a global switch ...
    (microsoft.public.vb.general.discussion)
  • Re: Public Variables
    ... If you wanted to really follow the OOP paradigm, ... > Are you stating that I should ALWAYS declare ALL variables as private? ... > My object is not to learn programming style as I only wrote this one ... >>> The program consists of 2 forms, and about 12 routines I wrote. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Public Variables
    ... Are you stating that I should ALWAYS declare ALL variables as private? ... when every other proceedure needs to know the new value. ... >> The program consists of 2 forms, and about 12 routines I wrote. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: reusing routines
    ... > delare it in my uses clause I should be able to address those routines ... one at the start of the interface section and one at the implementation ... If both Main and Library declare a type TIndex, ... stricter, visibility rules. ...
    (alt.comp.lang.borland-delphi)