Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Dim variable as boolean

5,521 views
Skip to first unread message

Phil S.

unread,
Dec 18, 2005, 12:41:11 PM12/18/05
to
Trying to compose a long VBscript for login. (for long reason, I need to
look at what network drives are already connected, then replace, add, or do
nothing depending on AD info.)

I need, or I want, to create several flag variables; as in Boolean variable.
(then I can have decisions based on 5 or 6 True/False flag conditions) All I
get is errors when I try to crate and use Boolean variables. so.

Every thing I have read states that I just {dim Tflag} and later just {Tflag
= True}

Currently the logic functions correctly by my using the {dim Tflag} and
using the sting {Tflag = "True"} or {Tflag = "False"} but looking thru the
script is UGLY do it that way and I cannot go into IT / IS staff meetings
with sloppy code like that.

Oh, I am using Option Explicit, and any {dim Tflag as Boolean}dies. One
place up at MSDN suggest I use the dim binTflag standard, but should that
have an effect?

Thanks for the help

Phil

James Whitlow

unread,
Dec 18, 2005, 1:02:15 PM12/18/05
to
"Phil S." <nospam-m-phil-NoSpam@one two three m-a-p-s.net> wrote in message
news:ubC95o$AGHA...@tk2msftngp13.phx.gbl...

Phil, take a look at this page and see if it answers some or all of your
questions:
http://www.csidata.com/custserv/onlinehelp/VBSdocs/vbs6.htm

You could use the 'CBool' function to set the subtype of the variable to
boolean, but assigning either True or False will also set the subtype to
boolean, so you accomplish the same thing, so I guess it comes down to
coding style & preference.


Phil S.

unread,
Dec 18, 2005, 1:11:39 PM12/18/05
to
James:

Thanks for the reply.
That is just my problem, if I just assign the variable to True or False, I
get an error message.
I have read the link you provided many times, with no help.
I have tried everything I know, and only (just) with VBscript,
application VB, and straight VB I can use the Dim <> as Boolean
but not with VBscript.

Phil

"James Whitlow" <jwhi...@letter.com> wrote in message
news:uHlxq0$AGHA...@TK2MSFTNGP09.phx.gbl...

McKirahan

unread,
Dec 18, 2005, 1:32:48 PM12/18/05
to
"Phil S." <nospam-m-phil-NoSpam@one two three m-a-p-s.net> wrote in message
news:etSY85$AGHA...@TK2MSFTNGP15.phx.gbl...

> James:
>
> Thanks for the reply.
> That is just my problem, if I just assign the variable to True or False, I
> get an error message.
> I have read the link you provided many times, with no help.
> I have tried everything I know, and only (just) with VBscript,
> application VB, and straight VB I can use the Dim <> as Boolean
> but not with VBscript.

[snip]

"I get an error message" -- could you be more specific?!


What happens for you with the following:

Save it as "booVar.vbs" and run it.

Option Explicit
Dim booVAR
booVAR = True
If booVAR Then WScript.Echo "+"


HSalim[MVP]

unread,
Dec 18, 2005, 3:15:01 PM12/18/05
to
Phil,
That is because, in VBScript, variables are always of one fundamental data
type, Variant.

You may use VarType to check for subtypes for example (copied from help
files)

Dim MyCheck
MyCheck = VarType(300) ' Returns 2.
MyCheck = VarType(#10/19/62#) ' Returns 7.
MyCheck = VarType("VBScript") ' Returns 8.

But you cannot use vatype to cast a variable as a subtype.

HS.


"Phil S." <nospam-m-phil-NoSpam@one two three m-a-p-s.net> wrote in message

news:etSY85$AGHA...@TK2MSFTNGP15.phx.gbl...
: James:

: >
: >
:


Michael Harris (MVP)

unread,
Dec 18, 2005, 8:49:09 PM12/18/05
to
James Whitlow wrote:
> "Phil S." <nospam-m-phil-NoSpam@one two three m-a-p-s.net> wrote in
> message news:ubC95o$AGHA...@tk2msftngp13.phx.gbl...
>> Trying to compose a long VBscript for login. (for long reason, I
>> need to look at what network drives are already connected, then
>> replace, add, or do nothing depending on AD info.)
>>
>> I need, or I want, to create several flag variables; as in Boolean
>> variable. (then I can have decisions based on 5 or 6 True/False flag
>> conditions) All I get is errors when I try to crate and use Boolean
>> variables. so.
>>
>> Every thing I have read states that I just {dim Tflag} and later
>> just {Tflag = True}
>>
>> Currently the logic functions correctly by my using the {dim Tflag}
>> and using the sting {Tflag = "True"} or {Tflag = "False"} but
>> looking thru the script is UGLY do it that way and I cannot go into
>> IT / IS staff meetings with sloppy code like that.
>>
>> Oh, I am using Option Explicit, and any {dim Tflag as Boolean}dies.
>> One place up at MSDN suggest I use the dim binTflag standard, but
>> should that have an effect?


True/False can be represented several ways and If and CBool() will
understand it as a Boolean...

for each foo in array(true, vbTrue, "true", -1, 1, 99)

wscript.echo "1a)",cstr(foo),vartype(foo),cstr(cbool(foo))
if foo then wscript.echo "1b) is true" else wscript.echo "1b) is false"

next

for each foo in array(false, vbFalse, "false", 0)

wscript.echo "2a)",cstr(foo),vartype(foo),cstr(cbool(foo))
if foo then wscript.echo "2a) is true" else wscript.echo "2a) is false"

next

Output:

---------- cscript ----------
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

1a) True 11 True
1b) is true
1a) -1 2 True
1b) is true
1a) true 8 True
1b) is true
1a) -1 2 True
1b) is true
1a) 1 2 True
1b) is true
1a) 99 2 True
1b) is true
2a) False 11 False
2a) is false
2a) 0 2 False
2a) is false
2a) false 8 False
2a) is false
2a) 0 2 False
2a) is false

Output completed (0 sec consumed) - Normal Termination


--
Michael Harris
Microsoft MVP Scripting


John

unread,
Dec 19, 2005, 12:06:07 PM12/19/05
to
Phil:

See if following code works for your requirements:

Option Explicit

Dim T

T = vbTrue

If T Then
MsgBox "T is true"
Else
MsgBox "T is False"
End If

T = vbFalse

If T Then
MsgBox "T is true"
Else
MsgBox "T is False"
End If

Thank you,
John

"Phil S." <nospam-m-phil-NoSpam@one two three m-a-p-s.net> wrote in message
news:ubC95o$AGHA...@tk2msftngp13.phx.gbl...

Phil S.

unread,
Dec 19, 2005, 6:04:08 PM12/19/05
to
Thanks everyone for you suggestions and help.

Turns out it was something different.
Since you all said it should work,
I tried my orginal script on a different computer,
it ran OK. The variables accepted the boolean True/False.

I re-installed MS Windows Script V.5.6 on my desk computer,
and my original script started to work there also.
Just as you all said it should.

Phil


Al Dunbar

unread,
Dec 18, 2005, 3:25:25 PM12/18/05
to

"Phil S." <nospam-m-phil-NoSpam@one two three m-a-p-s.net> wrote in message
news:ubC95o$AGHA...@tk2msftngp13.phx.gbl...
> Trying to compose a long VBscript for login. (for long reason, I need to
> look at what network drives are already connected, then replace, add, or
> do
> nothing depending on AD info.)
>
> I need, or I want, to create several flag variables; as in Boolean
> variable.
> (then I can have decisions based on 5 or 6 True/False flag conditions) All
> I
> get is errors when I try to crate and use Boolean variables. so.
>
> Every thing I have read states that I just {dim Tflag} and later just
> {Tflag
> = True}

The dim is only required if you use option explicit, but it is nonetheless a
good thing to do. Setting the variable to a value like {True} is also a good
idea, mind you, that is {true}, not {"true"}.

> Currently the logic functions correctly by my using the {dim Tflag} and
> using the sting {Tflag = "True"} or {Tflag = "False"} but looking thru the
> script is UGLY do it that way and I cannot go into IT / IS staff meetings
> with sloppy code like that.

If by "string" you mean as the boolean expression in an IF statement, the
expression is already assumed boolean by nature, so need not contain an
explicitly boolean expression. If you are doing this kind of thing:

if ( Tflag = "True" ) then
' do something
end if

Then Tflag is not a boolean but a string variable and you are comparing it
to other strings. This may certainly work, but it is, as you have
recognized, and uglification, to say nothing of problematic, as you might
inadvertently use "true" in place of "True".

Better to treat Tflag as truly boolean, i.e.:

if ( whatever ) then
Tflag = true
else
Tflag = false
end if

if ( Tflag ) then
' do something
end if

You would be well to name such flag variables to better document their
(boolean) meaning, for example, isOKtoConnect.

Using variables as boolean this way avoids both complexity and ambiguity,
and also avoids a common error when one assumes that a non-zero integer
ALWAYS behaves as if it has the boolean value TRUE. Consider this trivial
example in which we try to determine if both "a" and "b" appear in a string
(INSTR is often used as a boolean function - IF(INSTR(strpath,":"))...):

strtest = "abcdefg"
hasA = instr(strtest,"a")
hasB = instr(strtest,"b")
if ( hasA ) then
wscript.echo "string {" & strtest & "} contains the letter ""a"""
end if
if ( hasB ) then
wscript.echo "string {" & strtest & "} contains the letter ""b"""
end if
if ( hasA and hasB ) then
wscript.echo "string {" & strtest & "} contains the letters ""a""
and ""b"""
else
wscript.echo "string {" & strtest & "} does NOT contain both the
letters ""a"" and ""b"""
end if

If you run the above, it will tell you that the string contains "a" and it
contains "b", but it does NOT contain both "a" and "b". The reason is that,
while the values returned of 1 and 2 yield a true result when used on their
own. When combined with the boolean AND, however, their integer nature
causes a bitwise AND to be performed: 0x01 and 0x10 then yeild a zero, or a
false value.

> Oh, I am using Option Explicit, and any {dim Tflag as Boolean}dies.

As well it should; the "as" clause is not valid VBScript. All variables are
variants, taking on various types as required of them at the time.

> One
> place up at MSDN suggest I use the dim binTflag standard, but should that
> have an effect?

IMHO, the only effect of an alternate naming convention is that the variable
names are different. Neither VBScript nor WSH (nor any other possible hosts)
will take any significance from that "bin" prefix. That is strictly for your
own use.

/Al


0 new messages