[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 234
  • Last Modified:

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

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
kwh3856
Asked:
kwh3856
  • 4
  • 3
  • 3
  • +7
3 Solutions
 
kwh3856Author Commented:
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
 
arif_eqbalCommented:
How is the variable foreignExch being populated if it is being set through a database field then try using IsDBNULL(foreignExch)
0
 
tzxie2000Commented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Excalibur_SoftwareCommented:
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
 
armoghanCommented:
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
 
planoczCommented:
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
 
DotNetLover_BaanCommented:
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
 
Bob LearnedCommented:
Another trick:

foreignExch.Length > 0

Bob
0
 
DotNetLover_BaanCommented:
Are you sure Bob ? .. because if foreignExch is NULL, foreignExch.Length is gonna give an error.
-Baan
0
 
Bob LearnedCommented:
Sorry:

foreignExch & "".Length > 0

Bob
0
 
Snarf0001Commented:
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
 
Excalibur_SoftwareCommented:
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
 
DotNetLover_BaanCommented:
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
 
Excalibur_SoftwareCommented:
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
 
kwh3856Author Commented:
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
 
sj_hicksCommented:
I find it easiest to test the length of the sting using len().  i.e.

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



0
 
Excalibur_SoftwareCommented:
If you have a true null and you try to test for the length you will get a error.
0
 
kwh3856Author Commented:
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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 4
  • 3
  • 3
  • +7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now