470,874 Members | 1,435 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,874 developers. It's quick & easy.

Cancel Constructor (Me = Nothing)

Hi,

Most of my Objects have a constructor like this "Public Sub New(ID as
Integer)".
But when this ID isn't valid, I end up with an instantiated object, that
doesn't contain values. Is there a way to kind of cancel the constructor
when it detects that there isn't an object with this ID?

The main reason is that I like to test on: "MyObject Is Nohting", but in
these case is isn't nothing, but doesn't contain any values...

Thanks a lot in advance,

Pieter
Jun 6 '06 #1
7 1297
Nope, you can't cancel the construction of the object. The correct thing to
do in this case is to throw an ArgumentException (or possibly
ArgumentOutOfRangeException)

/claes

"Pieter" <pi**********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Hi,

Most of my Objects have a constructor like this "Public Sub New(ID as
Integer)".
But when this ID isn't valid, I end up with an instantiated object, that
doesn't contain values. Is there a way to kind of cancel the constructor
when it detects that there isn't an object with this ID?

The main reason is that I like to test on: "MyObject Is Nohting", but in
these case is isn't nothing, but doesn't contain any values...

Thanks a lot in advance,

Pieter

Jun 6 '06 #2
[FU2 microsoft.public.dotnet.general]

On Tue, 6 Jun 2006 17:20:19 +0200, Pieter wrote:
Most of my Objects have a constructor like this "Public Sub New(ID as
Integer)".
But when this ID isn't valid, I end up with an instantiated object, that
doesn't contain values. Is there a way to kind of cancel the constructor
when it detects that there isn't an object with this ID?

The main reason is that I like to test on: "MyObject Is Nohting", but in
these case is isn't nothing, but doesn't contain any values...


No, you can't "cancel" a constructor. However, if you detect that some
parameters passed to your constructor are invalid, you can throw an
exception such as e.g. ArgumentException or ArgumentOutOfRangeException
that can be then catched in the client code wherever it makes sense.

PS: please set the follow up group when cross-posting
Jun 6 '06 #3
Or consider an alternative approach:
Make the constructor private and add a shared CreateObject method. This
method can return null if the passed in id is not valid. If the id is valid
it creates and returns a valid object using the private constructor.

/claes

"Claes Bergefall" <lo*****@nospam.nospam> wrote in message
news:Oi**************@TK2MSFTNGP05.phx.gbl...
Nope, you can't cancel the construction of the object. The correct thing
to do in this case is to throw an ArgumentException (or possibly
ArgumentOutOfRangeException)

/claes

"Pieter" <pi**********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Hi,

Most of my Objects have a constructor like this "Public Sub New(ID as
Integer)".
But when this ID isn't valid, I end up with an instantiated object, that
doesn't contain values. Is there a way to kind of cancel the constructor
when it detects that there isn't an object with this ID?

The main reason is that I like to test on: "MyObject Is Nohting", but in
these case is isn't nothing, but doesn't contain any values...

Thanks a lot in advance,

Pieter


Jun 6 '06 #4
Pieter,
Is there a way to kind of cancel the constructor
when it detects that there isn't an object with this ID?


Throw an exception. It will not, however, cancel the creation of the
object. It's too late for that.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jun 6 '06 #5
"Pieter" <pi**********@hotmail.com> schrieb:
Most of my Objects have a constructor like this "Public Sub New(ID as
Integer)".
But when this ID isn't valid, I end up with an instantiated object, that
doesn't contain values. Is there a way to kind of cancel the constructor
when it detects that there isn't an object with this ID?

The main reason is that I like to test on: "MyObject Is Nohting", but in
these case is isn't nothing, but doesn't contain any values...


Either throw an exception or add a shared factory method which performs
initialization and returns 'Nothing' if construction fails.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Jun 6 '06 #6
Ok, thanks for the suggestions!
Jun 8 '06 #7
SP

"Pieter" <pi**********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Hi,

Most of my Objects have a constructor like this "Public Sub New(ID as
Integer)".
But when this ID isn't valid, I end up with an instantiated object, that
doesn't contain values. Is there a way to kind of cancel the constructor
when it detects that there isn't an object with this ID?

The main reason is that I like to test on: "MyObject Is Nohting", but in
these case is isn't nothing, but doesn't contain any values...

Thanks a lot in advance,

Pieter


You might also consider using a factory and return a Null Object when the ID
does not exist. Then the object has values and you can use the .IsNull
property as needed. The Null Object can eliminate testing for == null in
multiple places throughout your code.

SP
Jun 10 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Flzw | last post: by
45 posts views Thread by Ben Blank | last post: by
3 posts views Thread by Eidolon | last post: by
reply views Thread by Stuart Whiteford | last post: by
7 posts views Thread by Pieter | last post: by
12 posts views Thread by NewToCPP | last post: by
9 posts views Thread by Morten Lemvigh | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.