Jump to content

Issues with IsDeclared ... is this a bug or am I doing it wrong LOL?


Go to solution Solved by Danp2,

Recommended Posts

A little brain teaser for you.

I'm trying to write a simple debug function where I just shoot values and they're processed in a specific way to the console (Note: I'm trying to find out where a boolean variable turns into a text variable containing the text "True" instead of the value 'True').

BTW, "_CW" is shorthand for "ConsoleWrite" (how lazy can you be LOL)

So in order to make the output cleaner I tried different techniques to *not* display something when the function receives nothing.

Consider this bit of code

Global $bValuePos = True
Global $bValueNeg = False

_CW($bValuePos, $bValueNeg)

Exit

Func _CW($i1 = "", $i2 = "", $i3 = "", $i4 = "", $i5 = "", $i6 = "", $i7 = "", $i8 = "")
    If IsDeclared($i1) Then ConsoleWrite($i1 & @TAB & VarGetType($i1) & @CRLF)
    If IsDeclared($i2) Then ConsoleWrite($i2 & @TAB & VarGetType($i2) & @CRLF)
    If IsDeclared($i3) Then ConsoleWrite($i3 & @TAB & VarGetType($i3) & @CRLF)
    If IsDeclared($i4) Then ConsoleWrite($i4 & @TAB & VarGetType($i4) & @CRLF)
    If IsDeclared($i5) Then ConsoleWrite($i5 & @TAB & VarGetType($i5) & @CRLF)
    If IsDeclared($i6) Then ConsoleWrite($i6 & @TAB & VarGetType($i6) & @CRLF)
    If IsDeclared($i7) Then ConsoleWrite($i7 & @TAB & VarGetType($i7) & @CRLF)
    If IsDeclared($i8) Then ConsoleWrite($i8 & @TAB & VarGetType($i8) & @CRLF)
EndFunc

For those who didn't know, you can assign default values to all required variables. In our case, they're all going to be equal to "" unless a value is passed to the function. 

Logically, from within that function $i1 SHOULD be declared if a parameter is passed to it, right?

Well check it out; ConsoleWrite(IsDeclared($i1)) shoots me a zero from within that function.

Since it IS Local, we should be getting -1, right? A zero means it's NOT declared, which is obviously not true.

So is there something funky here or am I doing it wrong ehehehe

Also if you run that code, nothing comes out, no matter what you send in as parameters. It's time to change things up. 


Questioning, continued:

So I tried many ways of going about the same thing, hoping to "crack the code". 

Consider the following line: 

If $i1 <> "" Then ConsoleWrite($i1 & @TAB & VarGetType($i1) & @CRLF)
If $i2 <> "" Then ConsoleWrite($i2 & @TAB & VarGetType($i2) & @CRLF)

In theory, if you call the function and give nothing to display ... you expect to see nothing, right? And if you do, then you expect to see ConsoleWrite shoot out something?
Well, If you pass a boolean and it's False then you HAVE PASSED A VALUE ... but the line fails to display. It will work fine however if the value is True. There's something wrong, we're expecting that line to execute if the value is NOT EMPTY. 

Then I tried inversing the logic :

If Not $i1 = "" Then ConsoleWrite($i1 & @TAB & VarGetType($i1) & @CRLF)
If Not $i2 = "" Then ConsoleWrite($i2 & @TAB & VarGetType($i2) & @CRLF)

Same issue. As soon as the value is boolean false, the line fails to display.,. when I should get the value followed by the variable type. 

I'm expecting this output 

True     Bool
False     Bool

but I'm getting this: 

True     Bool

Something's wrong. 

 

A possible solution:

After much searching and mucking around, I found this technique to make sure booleans display fine when they're False:

Func _CW($i1 = Null, $i2 = Null, $i3 = Null, $i4 = Null, $i5 = Null, $i6 = Null, $i7 = Null, $i8 = Null)
    If Not ($i1 = Null) Then ConsoleWrite($i1 & @TAB & VarGetType($i1) & @CRLF)
    If Not ($i2 = Null) Then ConsoleWrite($i2 & @TAB & VarGetType($i2) & @CRLF)
    If Not ($i3 = Null) Then ConsoleWrite($i3 & @TAB & VarGetType($i3) & @CRLF)
    If Not ($i4 = Null) Then ConsoleWrite($i4 & @TAB & VarGetType($i4) & @CRLF)
    If Not ($i5 = Null) Then ConsoleWrite($i5 & @TAB & VarGetType($i5) & @CRLF)
    If Not ($i6 = Null) Then ConsoleWrite($i6 & @TAB & VarGetType($i6) & @CRLF)
    If Not ($i7 = Null) Then ConsoleWrite($i7 & @TAB & VarGetType($i7) & @CRLF)
    If Not ($i8 = Null) Then ConsoleWrite($i8 & @TAB & VarGetType($i8) & @CRLF)
EndFunc   ;==>_CW

Two things. 
1. Notice how I replaced "" by Null;
    Apparently "" confuses AutoIT when you're comparing it to boolean False. I'm guessing AutoIT assumes "" = False?

2. I've also put the whole comparison between parenthesis.
    My guess is "Not" is only processing the first part of the equation --> it does "Not $i1" and then checks to see if  that is equal to Null. We want to do "Not" on the results of $i1 = Null instead. This was a tricky one. 

But I still think there's a bug in the AutoIT code however

I'm glad this is cleared up but still think IsDeclared should tell me when a variable is local if it is part of the variables ... declared as the function's parameters. 


Hope you learned something and hope you find the code found here is useful!

Louis

P.S.: I'm using version 3.3.14.5 of AutoIt3

Edited by obiwanceleri
Added AutoIT version

Help a newbie, comment your code!

Link to comment
Share on other sites

@Danp2

Wow putting the variable name between parenthesis does work! (And that was in the example given in de documentation!!!). 
... but in our case variables that aren't passed are flagged as being declared anyways. Which kinda defeats the idea.

Per example, if you add this line to the script ...

If IsDeclared("i8") Then ConsoleWrite("BINGO i8" & @TAB & IsDeclared("i8") & @CRLF)

... and call _CW() without an eighth parameter, you will get this answer: 

BINGO i8    -1

Since no parameter was passed and there's no "Local $i8" statement, I was expecting IsDeclared() to give back a zero and the line to be blank. 

Oh well. 

The good news is I got exactly what I needed to detect booleans being transformed into strings, which was the initial issue that sent me into this foray. 

For the future, I Think I will simply create numerical variables equal to 0 or 1 instead. Booleans are great but in some borderline cases it makes the code go funky.

@Danp2 I'm giving you the win on this one ;)

Thanks!

Help a newbie, comment your code!

Link to comment
Share on other sites

32 minutes ago, obiwanceleri said:

Booleans are great but in some borderline cases it makes the code go funky.

Do you have something to backup such a vague assertion?

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

@obiwanceleriI think you are trying to use the wrong function to perform this task. Have you tried declaring the function like this?

Func _CW($i1 = Default, $i2 = Default, $i3 = Default, $i4 = Default, $i5 = Default, $i6 = Default, $i7 = Default, $i8 = Default)

Then you could check with something like

If $i1 <> Default Then ConsoleWrite($i1 & @TAB & VarGetType($i1) & @CRLF)

 

Link to comment
Share on other sites

4 hours ago, jchd said:

Do you have something to backup such a vague assertion?

I've tried to use Booleans many times because it makes sense, right? On and off?

But so far I've had to do a lot poking around to make it work. I'm thinking at this point the variable is interpreted as string instead of a boolean value in some cases. Might be my bad too.

The short of the story is that when I migrated from a boolean 'true' and 'false' to an integer '1' and '0', all my issues stopped.

For your information, I was using it as part of a checkmark a user can toggle on or off. 

I'd love to share the code but the interface and comments are in french and I couldn't be bothered to translate everything LOL

Hope this clears it up a bit?

Help a newbie, comment your code!

Link to comment
Share on other sites

1 hour ago, Danp2 said:

@obiwanceleriI think you are trying to use the wrong function to perform this task. Have you tried declaring the function like this?

Func _CW($i1 = Default, $i2 = Default, $i3 = Default, $i4 = Default, $i5 = Default, $i6 = Default, $i7 = Default, $i8 = Default)

Then you could check with something like

If $i1 <> Default Then ConsoleWrite($i1 & @TAB & VarGetType($i1) & @CRLF)

 

If you believe this is future-proof / better code then "Default" it shall be!

Thanks!

Help a newbie, comment your code!

Link to comment
Share on other sites

6 hours ago, obiwanceleri said:

I'm thinking at this point the variable is interpreted as string instead of a boolean value in some cases.

Thayt's still very vague. Post short runable code to demonstrate this.

6 hours ago, obiwanceleri said:

the interface and comments are in french

I'm French too.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Answered.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...