PC Review


Reply
Thread Tools Rate Thread

Create Null Object

 
 
tshad
Guest
Posts: n/a
 
      28th Sep 2005
How do you create a null object if the Constructor finds an error and thus
would make the object invalid?

If I have a Class Role and during the constructer there is something wrong,
I want to do something like the following:

public Role()
{
connectionString = ConfigurationSettings.AppSettings["x"];
if (connectionString == "")
Don't create the object and pass back a null
}


 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      28th Sep 2005
tshad <(E-Mail Removed)> wrote:
> How do you create a null object if the Constructor finds an error and thus
> would make the object invalid?


There's no such things as a null object.

> If I have a Class Role and during the constructer there is something wrong,
> I want to do something like the following:
>
> public Role()
> {
> connectionString = ConfigurationSettings.AppSettings["x"];
> if (connectionString == "")
> Don't create the object and pass back a null
> }


It sounds like you need a factory method:

public static Role CreateRole()
{
string connectionString = ConfigurationSettings.AppSettings["x"];
if (connectionString=="")
{
return null;
}
return new Role(connectionString);
}

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
tshad
Guest
Posts: n/a
 
      29th Sep 2005
"Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> tshad <(E-Mail Removed)> wrote:
>> How do you create a null object if the Constructor finds an error and
>> thus
>> would make the object invalid?

>
> There's no such things as a null object.
>
>> If I have a Class Role and during the constructer there is something
>> wrong,
>> I want to do something like the following:
>>
>> public Role()
>> {
>> connectionString = ConfigurationSettings.AppSettings["x"];
>> if (connectionString == "")
>> Don't create the object and pass back a null
>> }

>
> It sounds like you need a factory method:


Could be.

I have never used this, so I don't know.

But from your example, it looks like what I need. I will need to look at
how it works to see.

What I want to use it for is to create a role or user object based on a
RoleID or UserID. If the roleID or userID doesn't exist, then I want to
send back a null and not create the object (for what would be the point).

Thanks,

Tom
>
> public static Role CreateRole()
> {
> string connectionString = ConfigurationSettings.AppSettings["x"];
> if (connectionString=="")
> {
> return null;
> }
> return new Role(connectionString);
> }
>
> --
> Jon Skeet - <(E-Mail Removed)>
> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too



 
Reply With Quote
 
Scott
Guest
Posts: n/a
 
      29th Sep 2005
As a reminder, as an addition to that, so that users of the class don't just
call the constructor and circumvent your CreateRole() static method, make
the constructo private, as in:

private Role() {}


Scott

"tshad" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> tshad <(E-Mail Removed)> wrote:
>>> How do you create a null object if the Constructor finds an error and
>>> thus
>>> would make the object invalid?

>>
>> There's no such things as a null object.
>>
>>> If I have a Class Role and during the constructer there is something
>>> wrong,
>>> I want to do something like the following:
>>>
>>> public Role()
>>> {
>>> connectionString = ConfigurationSettings.AppSettings["x"];
>>> if (connectionString == "")
>>> Don't create the object and pass back a null
>>> }

>>
>> It sounds like you need a factory method:

>
> Could be.
>
> I have never used this, so I don't know.
>
> But from your example, it looks like what I need. I will need to look at
> how it works to see.
>
> What I want to use it for is to create a role or user object based on a
> RoleID or UserID. If the roleID or userID doesn't exist, then I want to
> send back a null and not create the object (for what would be the point).
>
> Thanks,
>
> Tom
>>
>> public static Role CreateRole()
>> {
>> string connectionString = ConfigurationSettings.AppSettings["x"];
>> if (connectionString=="")
>> {
>> return null;
>> }
>> return new Role(connectionString);
>> }
>>
>> --
>> Jon Skeet - <(E-Mail Removed)>
>> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
>> If replying to the group, please do not mail me too

>
>



 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      29th Sep 2005
Scott <(E-Mail Removed)_YOU.com> wrote:
> As a reminder, as an addition to that, so that users of the class don't just
> call the constructor and circumvent your CreateRole() static method, make
> the constructo private, as in:
>
> private Role() {}


Note that you don't need to supply an extra private parameterless
constructor if you have other constructors (which would probably also
be private). The compiler only supplies a public parameterless
constructor if no constructors are specified in the code.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Oliver Sturm
Guest
Posts: n/a
 
      29th Sep 2005
tshad wrote:

>How do you create a null object if the Constructor finds an error and thus
>would make the object invalid?
>
>If I have a Class Role and during the constructer there is something
>wrong, I want to do something like the following:
>
> public Role()
> {
> connectionString = ConfigurationSettings.AppSettings["x"];
> if (connectionString == "")
> Don't create the object and pass back a null
> }


In addition to the other suggestions - this looks like a situation to me,
where you actually detect an invalid state during object construction. In
that case, I would probably throw an exception in the constructor.

I see the distinguishing factors like this: if the condition is part of
your normal application flow logic, you'll want to detect and handle it
without exceptions - but you need to do that "from the outside", meaning
not from the constructor. A factory method may be fine for that. But if
the condition is something that shouldn't occur unless something went
wrong, I'd rather throw an exception from the constructor - no need to
introduce additional management code in the form of factory methods or
other "outside" checks for this error situation.


Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)
 
Reply With Quote
 
tshad
Guest
Posts: n/a
 
      29th Sep 2005
"Oliver Sturm" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> tshad wrote:
>
>>How do you create a null object if the Constructor finds an error and thus
>>would make the object invalid?
>>
>>If I have a Class Role and during the constructer there is something
>>wrong, I want to do something like the following:
>>
>> public Role()
>> {
>> connectionString = ConfigurationSettings.AppSettings["x"];
>> if (connectionString == "")
>> Don't create the object and pass back a null
>> }

>
> In addition to the other suggestions - this looks like a situation to me,
> where you actually detect an invalid state during object construction. In
> that case, I would probably throw an exception in the constructor.


How do you do that?

If you are already in the constructor, what tells it to send back the null
and not to create the object in the first place (or at least destroy what it
has already created).

Thanks,

Tom
>
> I see the distinguishing factors like this: if the condition is part of
> your normal application flow logic, you'll want to detect and handle it
> without exceptions - but you need to do that "from the outside", meaning
> not from the constructor. A factory method may be fine for that. But if
> the condition is something that shouldn't occur unless something went
> wrong, I'd rather throw an exception from the constructor - no need to
> introduce additional management code in the form of factory methods or
> other "outside" checks for this error situation.
>
>
> Oliver Sturm
> --
> Expert programming and consulting services available
> See http://www.sturmnet.org (try /blog as well)



 
Reply With Quote
 
Scott
Guest
Posts: n/a
 
      29th Sep 2005
Ah, yes, I forgot about that, Jon.

Scott


"Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Scott <(E-Mail Removed)_YOU.com> wrote:
>> As a reminder, as an addition to that, so that users of the class don't
>> just
>> call the constructor and circumvent your CreateRole() static method, make
>> the constructo private, as in:
>>
>> private Role() {}

>
> Note that you don't need to supply an extra private parameterless
> constructor if you have other constructors (which would probably also
> be private). The compiler only supplies a public parameterless
> constructor if no constructors are specified in the code.
>
> --
> Jon Skeet - <(E-Mail Removed)>
> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too



 
Reply With Quote
 
Oliver Sturm
Guest
Posts: n/a
 
      30th Sep 2005
tshad wrote:

>>>If I have a Class Role and during the constructer there is something
>>>wrong, I want to do something like the following:
>>>
>>>public Role()
>>>{
>>> connectionString = ConfigurationSettings.AppSettings["x"];
>>> if (connectionString == "")
>>> Don't create the object and pass back a null
>>>}

>>
>>In addition to the other suggestions - this looks like a situation to me,
>>where you actually detect an invalid state during object construction. In
>>that case, I would probably throw an exception in the constructor.

>
>How do you do that?
>
>If you are already in the constructor, what tells it to send back the null
>and not to create the object in the first place (or at least destroy what
>it has already created).


I didn't say I'd "send back null", but I'd throw an exception in this
case. Like this:

public Role() {
connectionString = ConfigurationSettings.AppSettings["x"];
if (connectionString == "")
throw new InvalidOperationException("Role can't be created without a valid connection string.");
}


Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)
 
Reply With Quote
 
tshad
Guest
Posts: n/a
 
      30th Sep 2005
"Oliver Sturm" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> tshad wrote:
>
>>>>If I have a Class Role and during the constructer there is something
>>>>wrong, I want to do something like the following:
>>>>
>>>>public Role()
>>>>{
>>>> connectionString = ConfigurationSettings.AppSettings["x"];
>>>> if (connectionString == "")
>>>> Don't create the object and pass back a null
>>>>}
>>>
>>>In addition to the other suggestions - this looks like a situation to me,
>>>where you actually detect an invalid state during object construction. In
>>>that case, I would probably throw an exception in the constructor.

>>
>>How do you do that?
>>
>>If you are already in the constructor, what tells it to send back the null
>>and not to create the object in the first place (or at least destroy what
>>it has already created).

>
> I didn't say I'd "send back null", but I'd throw an exception in this
> case. Like this:
>
> public Role() {
> connectionString = ConfigurationSettings.AppSettings["x"];
> if (connectionString == "")
> throw new InvalidOperationException("Role can't be created without
> a valid connection string.");
> }
>

But what does the code that called it get back?

Role newRole = new Role();

Tom
>
> Oliver Sturm
> --
> Expert programming and consulting services available
> See http://www.sturmnet.org (try /blog as well)



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ASPX page jscript rt error: null is null or not an object Cirene Microsoft ASP .NET 2 8th Sep 2010 02:04 PM
IE SP2 bound error: null is null or not an object putty Windows XP Internet Explorer 1 5th Apr 2005 08:05 PM
Runtime error 'null" is null or not an object Doris Windows XP Internet Explorer 0 15th Jul 2004 03:54 PM
Runtime Error: 'null' is null and not an object christopher.greene@comcast.net Windows XP Internet Explorer 1 12th Dec 2003 10:54 PM
Re: Line20: Error: 'null' is null or not an object Dublevay Windows XP General 0 6th Nov 2003 09:21 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:12 AM.