473,326 Members | 2,196 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,326 software developers and data experts.

test reference type for null?

Hi,

When I test my user defined class type passed parameter variable for null
it throws an exception. If I use class "string" the null test doesn't
through an exception. What could I be doing in my class that causes the
exception?

Thanks,
Mark

Thanks,
Mark


Nov 15 '05 #1
3 1604
"Mark Oliver" <ma*********@comcast.net> wrote in
news:AqXlb.11313$e01.21529@attbi_s02:
Hi,

When I test my user defined class type passed parameter variable
for null
it throws an exception. If I use class "string" the null test
doesn't through an exception. What could I be doing in my class that
causes the exception?

Thanks,
Mark

Thanks,
Mark



the only chance that your custom class can throw an exception on
myobj==null is when you've overriden the equals method (or the == operator)

maybe you should check your class implementation

--
best regards

Peter Koen
-----------------------------------
MCAD, CAI/R, CAI/S, CASE/RS, CAT/RS
http://www.kema.at
Nov 15 '05 #2
Mark Oliver <ma*********@comcast.net> wrote:
When I test my user defined class type passed parameter variable for null
it throws an exception. If I use class "string" the null test doesn't
through an exception. What could I be doing in my class that causes the
exception?


Chances are you've overridden == in some way that assumes that both
arguments are non-null (e.g. calling firstArgument.Equals
(secondArgument)). If you make the first few lines of your operator ==
override:

if ((Object)first==(Object)second)
return true;

if ((Object)first==null || (Object)second==null)
return false;

and then do whatever else you were doing, you should be fine.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
Thanks, That was the problem.
Mark

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Mark Oliver <ma*********@comcast.net> wrote:
When I test my user defined class type passed parameter variable for null it throws an exception. If I use class "string" the null test doesn't
through an exception. What could I be doing in my class that causes the exception?


Chances are you've overridden == in some way that assumes that both
arguments are non-null (e.g. calling firstArgument.Equals
(secondArgument)). If you make the first few lines of your operator ==
override:

if ((Object)first==(Object)second)
return true;

if ((Object)first==null || (Object)second==null)
return false;

and then do whatever else you were doing, you should be fine.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #4

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

Similar topics

110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
9
by: Steve Sargent | last post by:
Hi: I'm trying to debug the following code, and it keeps looping on the if statement: public static bool operator == (OnlineMemberNode first, OnlineMemberNode second) { if(first == null) {
5
by: Boniek | last post by:
Hi I define a public property in a new form and I can see this property in table of Properties in Visual. How I can hide this property to see only in code ? Thank's Boniek
5
by: Little | last post by:
I have this program and I need to work on the test portion, which tests if a Val is in the list. It returns false no matter what could you look at the part and see what might need to be done to fix...
12
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
3
by: Giorgio Parmeggiani | last post by:
HI I have a method that receive a generic parameter and also return a generic: public T GetById(ID id) I've want to return null if the received parameter is null, so i have write this code:...
2
by: Netkiller | last post by:
#!/usr/bin/python # -*- coding: utf-8 -*- """ Project: Network News Transport Protocol Server Program Description: 基于数据库的新闻组,实现BBS前端使用NNTP协议来访问贴子...
4
by: Matt | last post by:
Hello all, I have just discovered (the long way) that using a RegExp object with the 'global' flag set produces inconsistent results when its test() method is executed. I realize that 'global'...
2
by: hcaptech | last post by:
This is my Test.can you help me ? 1.Which of the following statement about C# varialble is incorrect ? A.A variable is a computer memory location identified by a unique name B.A variable's name...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shllpp 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.