Cancel Constructor (Me = Nothing)

P

Pieter

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
 
M

Mehdi

[FU2 microsoft.public.dotnet.general]

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
 
C

Claes Bergefall

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
 
C

Claes Bergefall

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
 
M

Mattias Sjögren

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
 
H

Herfried K. Wagner [MVP]

Pieter said:
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.
 
S

SP

Pieter said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top