I'm getting increasingly frustrated with C# and its exceptions on null
values. Rather than try to deal with it on a hit-or-miss basis as
exceptions pop up, I thought I should try to learn exactly how C# deals
with null. Of course, there's nothing obvious in the docs like "Dealing
with Null Values" and a search on "null" yielded 500 results, most of
which don't apply. Can anybody point me in the right direction? Or offer
some general guidelines? Or even the correct keyword to search the docs?
sandman
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it! 20 4013
Max Sandman <sa*********@yahoo.com> wrote: I'm getting increasingly frustrated with C# and its exceptions on null values. Rather than try to deal with it on a hit-or-miss basis as exceptions pop up, I thought I should try to learn exactly how C# deals with null. Of course, there's nothing obvious in the docs like "Dealing with Null Values" and a search on "null" yielded 500 results, most of which don't apply. Can anybody point me in the right direction? Or offer some general guidelines? Or even the correct keyword to search the docs?
Never try to dereference null - that's when you'll get an exception.
Read the documentation for methods you call - they should (but probably
won't always, unfortunately) specify whether or not null is a valid
value for the parameters you pass in, and whether or not the method may
return null.
Basically, make sure you know at all times which of your variables may
be null, and what it means for them to be null. Then don't dereference
those null values :)
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Hi Max,
Well I think that you will not find what you are looking for, now the
question to ask is why you are getting so much null values in your program,
are you sure you initialize all your variables correctly? are you checking
for null before using a object, it's non uncommon method returning null when
a value is not found like this example:
DataRow row = dataset.Tables["table1"].Rows.Find( pk );
//I have to check if row is a valid row or null
if ( row != null ) { .... }
Other than that, I do not what to advise you.
Hope this help,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Max Sandman" <sa*********@yahoo.com> wrote in message
news:uE**************@TK2MSFTNGP09.phx.gbl... I'm getting increasingly frustrated with C# and its exceptions on null values. Rather than try to deal with it on a hit-or-miss basis as exceptions pop up, I thought I should try to learn exactly how C# deals with null. Of course, there's nothing obvious in the docs like "Dealing with Null Values" and a search on "null" yielded 500 results, most of which don't apply. Can anybody point me in the right direction? Or offer some general guidelines? Or even the correct keyword to search the docs?
sandman
*** Sent via Developersdex http://www.developersdex.com *** Don't just participate in USENET...get rewarded for it!
with respect to Nulls...what are your frustrations?
examples? -----Original Message----- I'm getting increasingly frustrated with C# and its
exceptions on nullvalues. Rather than try to deal with it on a hit-or-
miss basis asexceptions pop up, I thought I should try to learn
exactly how C# dealswith null. Of course, there's nothing obvious in the
docs like "Dealingwith Null Values" and a search on "null" yielded 500
results, most ofwhich don't apply. Can anybody point me in the right
direction? Or offersome general guidelines? Or even the correct keyword to
search the docs? sandman
*** Sent via Developersdex http://www.developersdex.com
***Don't just participate in USENET...get rewarded for it! .
there's a null object pattern too, which is sometimes appropriate to save
explicit tests for null http://c2.com/cgi/wiki?NullObject
"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ... Max Sandman <sa*********@yahoo.com> wrote: I'm getting increasingly frustrated with C# and its exceptions on null values. Rather than try to deal with it on a hit-or-miss basis as exceptions pop up, I thought I should try to learn exactly how C# deals with null. Of course, there's nothing obvious in the docs like "Dealing with Null Values" and a search on "null" yielded 500 results, most of which don't apply. Can anybody point me in the right direction? Or offer some general guidelines? Or even the correct keyword to search the docs?
Never try to dereference null - that's when you'll get an exception.
Read the documentation for methods you call - they should (but probably won't always, unfortunately) specify whether or not null is a valid value for the parameters you pass in, and whether or not the method may return null.
Basically, make sure you know at all times which of your variables may be null, and what it means for them to be null. Then don't dereference those null values :)
-- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet/ If replying to the group, please do not mail me too
"Max Sandman" <sa*********@yahoo.com> wrote in message
news:uE**************@TK2MSFTNGP09.phx.gbl... I'm getting increasingly frustrated with C# and its exceptions on null values. Rather than try to deal with it on a hit-or-miss basis as exceptions pop up, I thought I should try to learn exactly how C# deals with null. Of course, there's nothing obvious in the docs like "Dealing with Null Values" and a search on "null" yielded 500 results, most of which don't apply. Can anybody point me in the right direction? Or offer some general guidelines? Or even the correct keyword to search the docs?
Hi Max,
Off the top of my head, here are a few coding practices that I use to avoid
these types of problems:
1. Initialize variables so you always know they are in a valid state. If
you explicitly initialize them to null, you know that they start off in an
invalid state (anticipating that the algorithm should bring them to a valid
state) and you should check them before you use them.
2. Check for null before making library calls to 3rd pary libraries where
null is invalid.
3. Use Debug.Assert for pre and post-condition checking to make sure your
algorithms start and end in valid states. It's really easy to use for
pre-conditions, i.e. making sure input parameters are not null.
4. Get NUnit, http://nunit.org/, (or another unit testing tool that you
would prefer) and test positive and negative conditions. I use this all the
time and do regular regression tests that keep me out of trouble.
Joe
-- http://www.csharp-station.com
Joe <jo*@nospam.com> wrote: 3. Use Debug.Assert for pre and post-condition checking to make sure your algorithms start and end in valid states. It's really easy to use for pre-conditions, i.e. making sure input parameters are not null.
I would suggest not using Debug.Assert for that kind of thing - I'd use
a straight exception:
if (someParameter==null)
throw new ArgumentNullException ("someParameter");
At least for non-private methods - it would make sense to use
assertions for private methods.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ... Joe <jo*@nospam.com> wrote:
3. Use Debug.Assert for pre and post-condition checking to make sure
your algorithms start and end in valid states. It's really easy to use for pre-conditions, i.e. making sure input parameters are not null.
I would suggest not using Debug.Assert for that kind of thing - I'd use a straight exception:
if (someParameter==null) throw new ArgumentNullException ("someParameter");
At least for non-private methods - it would make sense to use assertions for private methods.
Actually, I do both. Maybe I'm afraid someone else on the team will eat the
exception and I want to make sure they don't overlook it. :)
Joe
-- http://www.csharp-station.com
I'm finding null to be a bit frustrating too. What am I missing?
Consider the following example...
object o = null;
System.String x = null;
o = null;
y y1=null;
if(o!=null)
{
Console.WriteLine(o.ToString());
}
y is defined as follows:
class y
{
}
My frustration is that o is *never* null. I watch it in the debugger and
even the assignment makes it not null. Why?
System.String starts and null and happily stays that way.
What is going on? ab***********@hotmail.com <ab***********@hotmail.com> wrote: I'm finding null to be a bit frustrating too. What am I missing?
Consider the following example...
object o = null; System.String x = null; o = null; y y1=null; if(o!=null) { Console.WriteLine(o.ToString()); }
y is defined as follows:
class y { }
My frustration is that o is *never* null. I watch it in the debugger and even the assignment makes it not null. Why?
I'm sure it's just the debugger being strange - o certainly *is* null.
Here's a program to demonstrate that, using bits of your code:
using System;
public class Test
{
static void Main()
{
object o = null;
System.String x = null;
o = null;
if(o!=null)
{
Console.WriteLine(o.ToString());
}
else
{
Console.WriteLine ("o was null");
}
}
}
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Hi Abel,
In my machine it shows the three as null , both in the watch window and the
quickwatch.
Cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
<ab***********@hotmail.com> wrote in message
news:eL**************@TK2MSFTNGP12.phx.gbl... I'm finding null to be a bit frustrating too. What am I missing?
Consider the following example...
object o = null;
System.String x = null;
o = null;
y y1=null;
if(o!=null)
{
Console.WriteLine(o.ToString());
}
y is defined as follows:
class y
{
} My frustration is that o is *never* null. I watch it in the debugger and even the assignment makes it not null. Why?
System.String starts and null and happily stays that way.
What is going on?
Thanks everybody. Lots of good suggestions. I've got NUnit and I'll
start looking at it.
Most of my problems come from one of the following: uninitialized values
that, theoretically, a properly functioning app would prevent from
occurring; reading from a database where all columns haven't had data
entered - I'm still a little concerned about that one. If the user
doesn't enter anything, then the column contains null but if the user
clears a value and saves it, it's usually something defined, like 0 or
"". Do I just need to make sure that my database is initialized to valid
values? Since data will either be imported or keyed in directly, I have
control over how it goes in.
sandman
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Interesting. Then I guess the question is which version of the framework are
you using?
The String is shown as null for me, while as you can see from a previous
posting, others see <undefined value>
"Ignacio Machin" <ignacio.machin AT dot.state.fl.us> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl... Hi Abel,
In my machine it shows the three as null , both in the watch window and
the quickwatch.
Cheers,
-- Ignacio Machin, ignacio.machin AT dot.state.fl.us Florida Department Of Transportation
<ab***********@hotmail.com> wrote in message news:eL**************@TK2MSFTNGP12.phx.gbl... I'm finding null to be a bit frustrating too. What am I missing?
Consider the following example...
object o = null;
System.String x = null;
o = null;
y y1=null;
if(o!=null)
{
Console.WriteLine(o.ToString());
}
y is defined as follows:
class y
{
} My frustration is that o is *never* null. I watch it in the debugger and even the assignment makes it not null. Why?
System.String starts and null and happily stays that way.
What is going on?
You're right, the value *is* null, since looking at the registers in the
disassembly confirmed this. I'm just confused as to how the debugger figures
to display <undefined value>. I'm using 1.1 of the Framework and it seems my
VS is up to date.
"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ... ab***********@hotmail.com <ab***********@hotmail.com> wrote: I'm finding null to be a bit frustrating too. What am I missing?
Consider the following example...
object o = null; System.String x = null; o = null; y y1=null; if(o!=null) { Console.WriteLine(o.ToString()); }
y is defined as follows:
class y { }
My frustration is that o is *never* null. I watch it in the debugger and even the assignment makes it not null. Why?
I'm sure it's just the debugger being strange - o certainly *is* null. Here's a program to demonstrate that, using bits of your code:
using System;
public class Test { static void Main() { object o = null;
System.String x = null;
o = null;
if(o!=null) { Console.WriteLine(o.ToString()); } else { Console.WriteLine ("o was null"); } } }
-- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet/ If replying to the group, please do not mail me too
HI Abel,
I'm using VS.2002 and v1.0.3705
Cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
<ab***********@hotmail.com> wrote in message
news:OI**************@TK2MSFTNGP09.phx.gbl... Interesting. Then I guess the question is which version of the framework
are you using?
The String is shown as null for me, while as you can see from a previous posting, others see <undefined value>
"Ignacio Machin" <ignacio.machin AT dot.state.fl.us> wrote in message news:%2****************@TK2MSFTNGP12.phx.gbl... Hi Abel,
In my machine it shows the three as null , both in the watch window and the quickwatch.
Cheers,
-- Ignacio Machin, ignacio.machin AT dot.state.fl.us Florida Department Of Transportation
<ab***********@hotmail.com> wrote in message news:eL**************@TK2MSFTNGP12.phx.gbl... I'm finding null to be a bit frustrating too. What am I missing?
Consider the following example...
object o = null;
System.String x = null;
o = null;
y y1=null;
if(o!=null)
{
Console.WriteLine(o.ToString());
}
y is defined as follows:
class y
{
} My frustration is that o is *never* null. I watch it in the debugger
and even the assignment makes it not null. Why?
System.String starts and null and happily stays that way.
What is going on?
Thank heavens! Had we been the same, I would have been *most* concerned. I'm
using VS2003 and the 1.1 Framework. I'm not on the beta, but I'm curious how
that would have fared.
Hmmm... curiouser and curiouser. Has anyone else seen this using either
framework?
"Ignacio Machin" <ignacio.machin AT dot.state.fl.us> wrote in message
news:u5**************@TK2MSFTNGP12.phx.gbl... HI Abel,
I'm using VS.2002 and v1.0.3705
Cheers,
-- Ignacio Machin, ignacio.machin AT dot.state.fl.us Florida Department Of Transportation
<ab***********@hotmail.com> wrote in message news:OI**************@TK2MSFTNGP09.phx.gbl... Interesting. Then I guess the question is which version of the framework are you using?
The String is shown as null for me, while as you can see from a previous posting, others see <undefined value>
"Ignacio Machin" <ignacio.machin AT dot.state.fl.us> wrote in message news:%2****************@TK2MSFTNGP12.phx.gbl... Hi Abel,
In my machine it shows the three as null , both in the watch window
and the quickwatch.
Cheers,
-- Ignacio Machin, ignacio.machin AT dot.state.fl.us Florida Department Of Transportation
<ab***********@hotmail.com> wrote in message news:eL**************@TK2MSFTNGP12.phx.gbl... > I'm finding null to be a bit frustrating too. What am I missing? > > Consider the following example... > > object o = null; > > System.String x = null; > > o = null; > > y y1=null; > > if(o!=null) > > { > > Console.WriteLine(o.ToString()); > > } > > > > > > y is defined as follows: > > class y > > { > > } > > > > My frustration is that o is *never* null. I watch it in the debugger and > even the assignment makes it not null. Why? > > System.String starts and null and happily stays that way. > > What is going on? > > > > >
Hi Abel,
I'm installing now the 2003 version, they finally bought me a licence :) so
I will let you know if I get the same results :)
Cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
<ab***********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl... Thank heavens! Had we been the same, I would have been *most* concerned.
I'm using VS2003 and the 1.1 Framework. I'm not on the beta, but I'm curious
how that would have fared.
Hmmm... curiouser and curiouser. Has anyone else seen this using either framework? "Ignacio Machin" <ignacio.machin AT dot.state.fl.us> wrote in message news:u5**************@TK2MSFTNGP12.phx.gbl... HI Abel,
I'm using VS.2002 and v1.0.3705
Cheers,
-- Ignacio Machin, ignacio.machin AT dot.state.fl.us Florida Department Of Transportation
<ab***********@hotmail.com> wrote in message news:OI**************@TK2MSFTNGP09.phx.gbl... Interesting. Then I guess the question is which version of the
framework are you using?
The String is shown as null for me, while as you can see from a
previous posting, others see <undefined value>
"Ignacio Machin" <ignacio.machin AT dot.state.fl.us> wrote in message news:%2****************@TK2MSFTNGP12.phx.gbl... > Hi Abel, > > In my machine it shows the three as null , both in the watch window and the > quickwatch. > > Cheers, > > -- > Ignacio Machin, > ignacio.machin AT dot.state.fl.us > Florida Department Of Transportation > > <ab***********@hotmail.com> wrote in message > news:eL**************@TK2MSFTNGP12.phx.gbl... > > I'm finding null to be a bit frustrating too. What am I missing? > > > > Consider the following example... > > > > object o = null; > > > > System.String x = null; > > > > o = null; > > > > y y1=null; > > > > if(o!=null) > > > > { > > > > Console.WriteLine(o.ToString()); > > > > } > > > > > > > > > > > > y is defined as follows: > > > > class y > > > > { > > > > } > > > > > > > > My frustration is that o is *never* null. I watch it in the
debugger and > > even the assignment makes it not null. Why? > > > > System.String starts and null and happily stays that way. > > > > What is going on? > > > > > > > > > > > >
<ab***********@hotmail.com> wrote: You're right, the value *is* null, since looking at the registers in the disassembly confirmed this. I'm just confused as to how the debugger figures to display <undefined value>. I'm using 1.1 of the Framework and it seems my VS is up to date.
It does the same for me. I'd rather the debugger displayed "null", for
sure.
--
harry
<ab***********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl... You're right, the value *is* null, since looking at the registers in the disassembly confirmed this. I'm just confused as to how the debugger
figures to display <undefined value>. I'm using 1.1 of the Framework and it seems
my VS is up to date.
That's the same thing I see. As far as I know, <undefined value> always
means null. Perhaps it is the cross language capabilities of VS.NET that
motivated the generic terminology. i.e. C# = null, VB.NET = nothing, some
other language = nil.
Joe
-- http://www.csharp-station.com
Agreed. I guess one of the really strange things for me is that the
System.String x (see example) is indeed shown as null in the debugger. The
two other objects -- object and a reference to a dirty little class I wrote,
are <undefined value>
It seems inconsistant.
"Joe" <jo*@nospam.com> wrote in message
news:Oi**************@tk2msftngp13.phx.gbl... That's the same thing I see. As far as I know, <undefined value> always means null. Perhaps it is the cross language capabilities of VS.NET that motivated the generic terminology. i.e. C# = null, VB.NET = nothing, some other language = nil.
Joe -- http://www.csharp-station.com
Sorry. That was me. My fingers got ahead of what I was supposed to be
entering :P This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Agoston Bejo |
last post by:
I want to enforce such a constraint on a column that would ensure that the
values be all unique, but this wouldn't apply to NULL values. (I.e. there
may be more than one NULL value in the column.)...
|
by: Eric Lilja |
last post by:
Hello, consider the following complete program:
#include <assert.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
static int...
|
by: Matt |
last post by:
I could use some help dealing with null blobs. I'm
returning a transaction from an Image BLOB field in SQL
Server 2000 using C#. If the transaction exists the value
is returned with out trouble,...
|
by: Stephen Cawood |
last post by:
I'm trying to use a C++ .lib from C# (I tried the Interop group will no
results).
I have a working wrapper DLL (I can get back simple things like int), but
I'm having issues dealing with an array...
|
by: BobRoyAce |
last post by:
Let's say I have a table called Users which has a field DeptID which is
an int. I also have a User class which has a method for getting the
data for a particular user (i.e. User with specified ID)...
|
by: ineedahelp |
last post by:
Can anyone tell me if this is correct code? I have to deal with an occasional NULL value in MarketPrice. I am cycling through a recordset created by an SQL
If IsNull(rst!MarketPrice) Then....
|
by: Christopher Benson-Manica |
last post by:
Some recent posts got me thinking about how one might have dealt with
simplistic malloc() implementations which might return NULL for a 64K
request but might accept two 32K requests or four 16K...
|
by: Deckarep |
last post by:
Hey everyone,
Is there a more elegant or cleaner way of accomplishing the following
null check?
List<stringmyString = null; //Purposely null list of strings to
show the example
XElement...
|
by: raylopez99 |
last post by:
I ran afoul of this Compiler error CS1612 recently, when trying to
modify a Point, which I had made have a property. It's pointless to
do this (initially it will compile, but you'll run into...
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
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,...
|
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,...
|
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: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
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,...
| |