The Presents of Null values/Nothing/"" or not

G

Guest

I'm trying to check for the presents of null values or null value not being
present. My sample code below:


If EmailAddresses Is Nothing Then

SendMailMessage(EmailAddresses, AlertTitle,
ViolationBody)
End If


The above code works when checking for the presents of a null
value/Nothing/"".
However, I want to check for null not being present ie.



If EmailAddresses not Nothing Then

SendMailMessage(EmailAddresses, AlertTitle,
ViolationBody)
End If

The above code will not compile.

Basically I want to know when nulls are present and not present.

Thanks
 
G

Guest

I tried your example below and the results are the same. It executes the
send when the value of EmailAddresses is a null value (string) or not null
value which means it contains an email address.

Could I be missing something here. If there is no email address then no
need to send mail.

Thanks
 
I

Imran Koradia

You didn't mention EmailAddresses was a string in your first post (or
probably I half read it :)). I assumed it was an object (which is why you
were testing for nothing). To test whether a string is empty ("") or not,
you should use this:

If EmailAddresses.Length > 0 Then
' Its a non-empty string
SendMailMessage(EmailAddresses, AlertTitle, ViolationBody)
End If


hope that helps..
Imran.
 
H

Herfried K. Wagner [MVP]

Larry Bird said:
Basically I want to know when nulls are present and not present.

Depends on how you define 'null' for strings...

\\\
If _
Not EmailAddresses Is Nothing AndAlso _
EmailAddresses.Length > 0 _
Then
...
End If
///
 
C

Cor Ligthert

Larry,

Because everybody does something than me as well

Dim EmailAddress as string is
EmailAdress Is Nothing

Dim EmailAddress as string = "" is
EmailAdress.lenght = 0

Both work with
If EmailAdress = Nothing

I hope this helps?

Cor
 
S

Scott M.

What you've got is overkill and not needed to check a string for null, just
check for "".
 
G

Guest

That worked great thank for the help


Imran Koradia said:
You didn't mention EmailAddresses was a string in your first post (or
probably I half read it :)). I assumed it was an object (which is why you
were testing for nothing). To test whether a string is empty ("") or not,
you should use this:

If EmailAddresses.Length > 0 Then
' Its a non-empty string
SendMailMessage(EmailAddresses, AlertTitle, ViolationBody)
End If


hope that helps..
Imran.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top