473,387 Members | 1,420 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Testing data types for nothing

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?
Nov 22 '05 #1
4 3047
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
Nov 22 '05 #2
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
Nov 22 '05 #3
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" <an*******@discussions.microsoft.com> wrote in message
news:A1**********************************@microsof t.com...
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?
Nov 22 '05 #4
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" <an*******@discussions.microsoft.com> wrote in message
news:A1**********************************@microsof t.com...
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?
Nov 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Nathon Dalton | last post by:
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...
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
0
by: Nathon Dalton | last post by:
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...
44
by: John A. Bailo | last post by:
Dr. Dobbs has a /glowing/ article on Ruby on Rails this month. What do you guys think? Can it replace .net, php and java? And be the Open Source OOP web solution that is not bound to Sun or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.