Re: Will undefined varialbes test as False?
From: Top Spin (ToppSpin_at_hotmail.com)
Date: 10/04/04
- Next message: Stuart Crow: "Re: Error handling in dll ?"
- Previous message: Bonj: "Re: Multithreading problem :("
- In reply to: Rick Rothstein: "Re: Will undefined varialbes test as False?"
- Next in thread: Bob Butler: "Re: Will undefined varialbes test as False?"
- Reply: Bob Butler: "Re: Will undefined varialbes test as False?"
- Reply: Tony Proctor: "Re: Will undefined varialbes test as False?"
- Messages sorted by: [ date ] [ thread ]
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
- Next message: Stuart Crow: "Re: Error handling in dll ?"
- Previous message: Bonj: "Re: Multithreading problem :("
- In reply to: Rick Rothstein: "Re: Will undefined varialbes test as False?"
- Next in thread: Bob Butler: "Re: Will undefined varialbes test as False?"
- Reply: Bob Butler: "Re: Will undefined varialbes test as False?"
- Reply: Tony Proctor: "Re: Will undefined varialbes test as False?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|