Home All Groups Group Topic Archive Search About

Cancel Constructor (Me = Nothing)

Author
6 Jun 2006 3:20 PM
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

Author
6 Jun 2006 3:28 PM
Mehdi
[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
Author
6 Jun 2006 3:32 PM
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

Show quote
"Pieter" <pietercou***@hotmail.com> wrote in message
news:%23ckb6yXiGHA.1272@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
>
Author
6 Jun 2006 3:36 PM
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

Show quote
"Claes Bergefall" <louplou@nospam.nospam> wrote in message
news:OisnW4XiGHA.3408@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" <pietercou***@hotmail.com> wrote in message
> news:%23ckb6yXiGHA.1272@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
>>
>
>
Author
6 Jun 2006 3:36 PM
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

--
Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Author
6 Jun 2006 6:46 PM
Herfried K. Wagner [MVP]
"Pieter" <pietercou***@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/>
Author
7 Jun 2006 4:37 PM
Saad Rehmani
Pieter,

I would protect the constructor from direct access (make it private) and
create a static method that allows you to inspect the parameters being passed
in before you actually call the constructor.


public class foo {
  private foo(string s)
  {
    ...
  }

  public static Foo CreateFoo(string s)
  {
    if (s == null) return null;
    return new foo(s);
  }
}


--
Saad Rehmani / Prodika / Dallas / TX / USA

Show quote
> 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
>
Author
8 Jun 2006 7:56 AM
Pieter
Ok, thanks for the suggestions!
Author
10 Jun 2006 1:38 AM
SP
Show quote
"Pieter" <pietercou***@hotmail.com> wrote in message
news:%23ckb6yXiGHA.1272@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

AddThis Social Bookmark Button