Solved

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

Posted on 2004-09-15
18
227 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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
 
LVL 96

Accepted Solution

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

foreignExch & "".Length > 0

Bob
0
 
LVL 23

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

724 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