473,473 Members | 1,576 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Check if an object is null

Hello,

Inside a class I have 4 propertiy types:

1. String

2. Boolean

3. Mail.MailPriority

4. Mail.MailAddressCollection

I need to check if each property is null, i.e., if it wasn't defined
any value for them.

How can I do it?

I tried NULL but it got an error to use System.DBNull but I got error
again.

Thanks,

Miguel

Oct 9 '06 #1
4 9263
Try DBNull.value.
--
Ameet Phadnis
Sr. Technical Consultant
e Tek Global Inc.
ASP Alliance Author Page: http://aspalliance.com/author.aspx?uId=44260
"shapper" wrote:
Hello,

Inside a class I have 4 propertiy types:

1. String

2. Boolean

3. Mail.MailPriority

4. Mail.MailAddressCollection

I need to check if each property is null, i.e., if it wasn't defined
any value for them.

How can I do it?

I tried NULL but it got an error to use System.DBNull but I got error
again.

Thanks,

Miguel

Oct 9 '06 #2
You would use the System.DBNull.Value as a null test if you're pulling data
from a datareader or other database source. Also with a string it could be
initialised but empty, which you may also want to handle differently by
testing if it's a String.Empty.
--
Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006
"shapper" <md*****@gmail.comwrote in message
news:11*********************@e3g2000cwe.googlegrou ps.com...
Hello,

Inside a class I have 4 propertiy types:

1. String

2. Boolean

3. Mail.MailPriority

4. Mail.MailAddressCollection

I need to check if each property is null, i.e., if it wasn't defined
any value for them.

How can I do it?

I tried NULL but it got an error to use System.DBNull but I got error
again.

Thanks,

Miguel

Oct 9 '06 #3
In ASP.NEt 1.1 or in 2.0 without a nullable type, you would use DbNull.Value

if (x == DbNull.Value)
{
//is null
}

In 2.0, you have nullable types. I assume you are not using them however, or
are using VB.NET. In VB.NET 2.0, you use

Dim nullable1 As Nullable(Of DateTime) = New Nullable(Of DateTime)
If Not nullable1.HasValue Then
End If
--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com

*************************************************
Think outside of the box!
*************************************************
"shapper" <md*****@gmail.comwrote in message
news:11*********************@e3g2000cwe.googlegrou ps.com...
Hello,

Inside a class I have 4 propertiy types:

1. String

2. Boolean

3. Mail.MailPriority

4. Mail.MailAddressCollection

I need to check if each property is null, i.e., if it wasn't defined
any value for them.

How can I do it?

I tried NULL but it got an error to use System.DBNull but I got error
again.

Thanks,

Miguel

Oct 9 '06 #4
I am using:

If MyObject.Equals(DBNull.Value) Then
....

Do you think this work?

At least I didn't have any problem in my code.

Thanks,
Miguel

Cowboy (Gregory A. Beamer) wrote:
In ASP.NEt 1.1 or in 2.0 without a nullable type, you would use DbNull.Value

if (x == DbNull.Value)
{
//is null
}

In 2.0, you have nullable types. I assume you are not using them however, or
are using VB.NET. In VB.NET 2.0, you use

Dim nullable1 As Nullable(Of DateTime) = New Nullable(Of DateTime)
If Not nullable1.HasValue Then
End If
--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com

*************************************************
Think outside of the box!
*************************************************
"shapper" <md*****@gmail.comwrote in message
news:11*********************@e3g2000cwe.googlegrou ps.com...
Hello,

Inside a class I have 4 propertiy types:

1. String

2. Boolean

3. Mail.MailPriority

4. Mail.MailAddressCollection

I need to check if each property is null, i.e., if it wasn't defined
any value for them.

How can I do it?

I tried NULL but it got an error to use System.DBNull but I got error
again.

Thanks,

Miguel
Oct 10 '06 #5

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

Similar topics

5
by: LL | last post by:
Hi, Where is the best place to check the session's object, if it's null, then redirect the user to login page? put into every page's load event? Thanks.
2
by: aaa | last post by:
I know in VB we had IsObject ut how do I check an object to see if it exists in sharp (without bombing the if)?
9
by: Edmund Dengler | last post by:
Greetings! Just trying some tests out, and wanted to know about some optimizations. If I do a CHECK constraint on a table, is this used to optimize a SELECT or does Postgresql rely mostly on...
25
by: pamelafluente | last post by:
Hi Guys, I have the following HTML code which is doing a GET to a page, say MyUrl.aspx : <body> <form name="form1" method="get" action="MyUrl.aspx" id="form1"> <input type="hidden"...
4
by: Karen Hill | last post by:
How does one check if an object's reference is null? I'd like to check that rather than catch a nullreference exception. thanks.
15
by: Tarun Mistry | last post by:
Hi guys, what is the best/correct way to check for a NULL object? I.e. myClass test; if(test == null) {}
8
by: Earl | last post by:
What's the best way to check for null on an ExecuteScalar? The following would fire the command twice: if (cmd.ExecuteScalar() != null) { intContactID = Convert.ToInt32(cmd.ExecuteScalar()); }
30
by: Jess | last post by:
Hello, I tried a program as follows: include<iostream> using namespace std; class A{ public:
14
by: Mark | last post by:
Hi, I would like to check if my object has been deleted or not, but my program keeps crashing. Here's the simplified code (in infinite loop) delete tetromino; //if(tetromino==NULL)...
4
by: rosh72851 | last post by:
Header file #ifndef STRING_HPP #define STRING_HPP #include <iostream> using namespace std; /* mystring class */
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
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.