Testing data types for nothing

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need to know what the appropriate way to test for nothing in several different types of data types is. Here are some examples

For string I use this
If Not abc.text Is Nothing AndAlso Not abc.text = "" The
' Some Cod
End I

For datetime's I'm currently using something like this
If Not abc.ToShortDateString = "01/01/0001" The
' Some Cod
End I

For Integers, I use this
If not abc = 0 The
' Some Cod
End I

For object, I'm asuming that it'd be against nothing like this
If Not abc Is Nothing The
' Some Cod
End I

What about other data types? I know Boolean wouldn't matter, because when it's not initialized, it's False. What about things like Decimal, Single, Double, Long, Byte, Char, etc.? Also, are the ways I'm testing above the correct way to test?
 
Hi Nathon,
For string I use this.
If Not abc.text Is Nothing AndAlso Not abc.text = "" Then
' Some Code
End If

A string is a strange thing, both are correct however there is a difference.

The first test a string which has not really a reference to something, the
second is an empty string, however you can always use the = "" which is by
the most regulars in the language.vb group the advices method.
For datetime's I'm currently using something like this.
If Not abc.ToShortDateString = "01/01/0001" Then
' Some Code
End If
Date is in VB.net a value and therefore is
If abc = nothing very allowed.

For Integers, I use this.
If not abc = 0 Then
' Some Code
End If
A value type can only tested for 0, = nothing is the same
For object, I'm asuming that it'd be against nothing like this.
If Not abc Is Nothing Then
' Some Code
End If

There are basicly only two types Objects and Values.

From the values are the date and the string a little bit strange for the
rest you can always do the same as with the integer.

Cor
 
If it's a reference type, you can test for nothing (vb.net) or null (c#).
Most of the types you mention are value types so they won't be null but they
do have default values and you can test against those. Bool defaults to
false for instance. Decimal defaults to 0. Char defaults to string.Empty
(as an aside, it's better to test for string.Empty with strings as well
instead of testing for something = "")
For datetime you can test against MinValue (01/01/0001 12:00:00 AM)

Most everything else is going to be a reference type, so it's pretty easy to
learn the value types and test against them.


HTH,

Bill
Nathon Dalton said:
I need to know what the appropriate way to test for nothing in several
different types of data types is. Here are some examples.
For string I use this.
If Not abc.text Is Nothing AndAlso Not abc.text = "" Then
' Some Code
End If

For datetime's I'm currently using something like this.
If Not abc.ToShortDateString = "01/01/0001" Then
' Some Code
End If

For Integers, I use this.
If not abc = 0 Then
' Some Code
End If

For object, I'm asuming that it'd be against nothing like this.
If Not abc Is Nothing Then
' Some Code
End If

What about other data types? I know Boolean wouldn't matter, because when
it's not initialized, it's False. What about things like Decimal, Single,
Double, Long, Byte, Char, etc.? Also, are the ways I'm testing above the
correct way to test?
 
Back
Top