Jump to content

Testing for Null/empty variable


Recommended Posts

How is everyone?

Stuck on detecting if a variable is blank.

If $Variablename has no value, has not been defined, then do ....

In the head it's:

If $Variable = Null Then

Ideas?

Thanks.

don't think i'm a freeloader. your help has been effective! here's the script contributed - thank you creator and thank you members and thank you authors of autoit!Call Conference Dial-in Script updated from time to time.

Link to comment
Share on other sites

To check if a variable has been declared use the If IsDeclared("variablename") function. If the variable you're looking for is called $variable, use "variable" in the function. To see if the variable contains nothing in it, use If $Variable = "".

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Do you want a detecting function like this ?

Global $_A = '', $_B = 0, $_C = ' ', $_D = 0.5

ConsoleWrite ( "Is $_A Null : " & _IsVarIsNull ( $_A ) & @Crlf )
ConsoleWrite ( "Is $_B Null : " & _IsVarIsNull ( $_B ) & @Crlf )
ConsoleWrite ( "Is $_C Null : " & _IsVarIsNull ( $_C ) & @Crlf )
ConsoleWrite ( "Is $_D Null : " & _IsVarIsNull ( $_D ) & @Crlf )
Exit

Func _IsVarIsNull ( $_Var )
    If Not $_Var Then 
        Return True 
    Else
        Return False
    EndIf
EndFunc
Edited by wakillon

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Link to comment
Share on other sites

Do you want a detecting function like this ?

Global $_A = '', $_B = 0, $_C = ' ', $_D = 0.5

ConsoleWrite ( "Is $_A Null : " & _IsVarIsNull ( $_A ) & @Crlf )

ConsoleWrite ( "Is $_B Null : " & _IsVarIsNull ( $_B ) & @Crlf )

ConsoleWrite ( "Is $_C Null : " & _IsVarIsNull ( $_C ) & @Crlf )

ConsoleWrite ( "Is $_D Null : " & _IsVarIsNull ( $_D ) & @Crlf )

Exit

Func _IsVarIsNull ( $_Var )

If Not $_Var Then

Return True

Else

Return False

EndIf

EndFunc

In AutoIt much better solution than:

If Not somevar Then
    something
Else
    somethingelse
EndIf

is:

If somevar Then
    somethingelse
Else
    something
EndIf

@JohnOne, VarGetType is irrelevant. That function shows type (vt) of the variant holding the value of the variable.

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

But is it safe to say that all autoit variables start their life as a string type?

EDIT:

Sorry scriptstopper, we are all being terribly rude.

I'm fine, although I'm having a bit of trouble with my colon, nothing to worry about though, thanks for asking.

You?

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

;)

We're allowed a little bit of a giggle I hope.

Maybe, maybe not.

To answer that question of yours from the above - probably not. It depends if the variable is initialized.

This is String type (VT_BSTR):

Local $Var

This is Int32 (VT_I4)

Local $Var = 4

If internally AutoIt would make VT_BSTR and then change its type to VT_I4 and then assign the value, it would make one step more than it should because it always starts with VT_EMPTY as that's the result of variant initialization.

So, logically it would be to start with VT_EMPTY (initialization) and then depending on the script code changing type to what's needed.

Edited by trancexx

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

What is confusing me, is what autoit classes the variable type as.

I know you said vargettype was irrelevant but i would have expected that function to to be the judge of that very question.

Local $var is seen as a string before it is initialzed, and then converted when assigned a value be that an object or a int.

I cant speak about autoits internal enviroment, but in the scripting enviroment, it must start its life as string since there is no null.

Im pretty crap at explaining myself, sorry.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

  • 8 years later...

Just found out where this might apply!

The _sql library that we use (from Chris Lambert) apparently brings in NULLS as "Default". I was trying to detect the NULL values using

If $varName = "" Then
...
EndIf

Which was not working, so now I do

If IsKeyword($varName) OR $varName = "" Then
...
EndIf

Which works as it should. 

 

Just thought I'd throw this note in here for anyone else using the _sql library and trying to detect empty (NULL) values.

 

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...