Solved

testing for "" in a if statement never matches up with the data when it is true

Posted on 2004-09-15
18
187 Views
Last Modified: 2011-08-18
I am trying to find any null data and covert it to a boolean variable.  The local variable for the data shows the contents as "".  When I test for the data "" in a if statement, the break point skips right by the if statement.  Any ideas????

Here is my code

If foreignExch > "" Then
                foreignExch = False
            End If
            If foreignExch = "0" Then
                foreignExch = False
            Else
                foreignExch = True
            End If
0
Comment
Question by:kwh3856
  • 4
  • 3
  • 3
  • +7
18 Comments
 

Author Comment

by:kwh3856
ID: 12071740
ooops  I was just testing with that code here is how it sits

If foreignExch = "" Then
                foreignExch = False
            End If
            If foreignExch = "0" Then
                foreignExch = False
            Else
                foreignExch = True
            End If
0
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 12071963
How is the variable foreignExch being populated if it is being set through a database field then try using IsDBNULL(foreignExch)
0
 
LVL 5

Expert Comment

by:tzxie2000
ID: 12072054
depend on the code before if what is foreignExch will be important

rem you can write
If isdbnull(foreignExch) Then
   foreignExch = False
End If
rem when you get a null value

rem you can write
If isNothing(foreignExch) Then
   foreignExch = False
End If
when you did not assign a value to foreignExch
0
 
LVL 4

Expert Comment

by:Excalibur_Software
ID: 12072077
a Null is not the same thing as a empty String ("")
You could do something like this


foreignExch = Trim(foreignExch &" ") 'This remove any null and sets the string to emtpy if there is a null
If foreignExch = -1 Then          'If you have a true value
                foreignExch = true
Else                                       ' if the data is not true then it must be false
  foreignExch = False
End If

I hope this help you if you have any question please let me know

Thank you for your time
0
 
LVL 18

Expert Comment

by:armoghan
ID: 12072950
if it a string then it will work
try this

Dim foreignExch As String
' or
' Dim foreignExch As String =""
        If foreignExch = Nothing Or foreignExch = "" Then
            foreignExch = False
        End If
        If foreignExch = "0" Then
            foreignExch = False
        Else
            foreignExch = True
        End If

0
 
LVL 27

Expert Comment

by:planocz
ID: 12073981
Here is some code that you might like to play with....

  Private Function CheckNull(ByVal sData As String) As Boolean
        Select Case sData
            Case "", String.Empty
                CheckNull = False
            Case Else
                CheckNull = True
        End Select
    End Function
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12074352
On the other day Gregory showd a nice trick... you might like it

            If foreignExch & "" = "" Then   'Null & "" = ""
                foreignExch = False
            End If
            If foreignExch = "0" Then
                foreignExch = False
            Else
                foreignExch = True
            End If
====
Why are you using two if conditions.

            If foreignExch & "" = "" Or foreignExch = "0" Then   '''' Because  Null & "" = ""
                foreignExch = False
            Else
                foreignExch = True
            End If

-Baan
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12076985
Another trick:

foreignExch.Length > 0

Bob
0
 
LVL 18

Assisted Solution

by:DotNetLover_Baan
DotNetLover_Baan earned 125 total points
ID: 12077502
Are you sure Bob ? .. because if foreignExch is NULL, foreignExch.Length is gonna give an error.
-Baan
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 96

Accepted Solution

by:
Bob Learned earned 250 total points
ID: 12077532
Sorry:

foreignExch & "".Length > 0

Bob
0
 
LVL 22

Expert Comment

by:Snarf0001
ID: 12078425
If you're going through a database, you might want to also validate against DBNull.Value.

Building on Baan's statement, what exactly is it you want the statement to do?  If it did find the empty value, you'd set it to false.  But in the next statement:

           If foreignExch = "0" Then
                foreignExch = False
            Else
                foreignExch = True
            End If

you just made it false, so it won't evaluate to the string "0", and will be reset to true by the else statment.
0
 
LVL 4

Assisted Solution

by:Excalibur_Software
Excalibur_Software earned 125 total points
ID: 12078764
kwh3856 a Null is not the same thing as a empty String .A null value is different from an empty string, a blank field, or zero. For instance, both EMPTY( ) and ISBLANK( ) return true (.T.) when a variable contains blank or an empty string. EMPTY( ) also returns true (.T.) for zero. ISNULL( ) returns false (.F.) for these values and both EMPTY( ) and ISBLANK( ) return false (.F.) for null values. A null mean that your database dose not know that is in that data field for some reason in most cases it is because nothing has been written to your field.
A "" is a empty field that mean either something has been written and is now not there or that the field was set to a zero string.
if you have a null field you could use the isnull in a function to see if your field is a true null

I hope this help you understand null's

Thank you for your time

0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12078772
ya... this is what you call a pure logical error(mistake). When working with flags, you have to be very careful. Specially, if you are looping through array of values. The most standard way is to set the value before the loop starts. and inside the the loop if the flag changes, exit the loop.
-Baan
0
 
LVL 4

Expert Comment

by:Excalibur_Software
ID: 12079150
One other thing I forgot to mention, in your code
If foreignExch > "" Then
                foreignExch = False
            End If
            If foreignExch = "0" Then
                foreignExch = False
            Else
                foreignExch = True
            End If

You are mixing up data types

example "" and "0" are strings
foreignExch is a boolean data type

That means that this statment can never be true
If foreignExch > "" Then because your datatypes are not the same

Thank you for your time
Al
0
 

Author Comment

by:kwh3856
ID: 12079407
Thanks for all the response.....I have been out for a day or so, so let me review all this and get a better understanding of it all.  I will let you know what ends up working.  Again, thank each of you for your response.  I will work on things tonight and let you know.


Thanks
Kenny
0
 
LVL 7

Expert Comment

by:sj_hicks
ID: 12081510
I find it easiest to test the length of the sting using len().  i.e.

If len(foreignExch) <= 0 Then foreignExch = False



0
 
LVL 4

Expert Comment

by:Excalibur_Software
ID: 12081645
If you have a true null and you try to test for the length you will get a error.
0
 

Author Comment

by:kwh3856
ID: 12092665
Many thanks to each of you for your help.  Sometimes I do no know what I would do if this web site did not exist.


Thanks
Kenny
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now