472,954 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,954 software developers and data experts.

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 1392
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Flzw | last post by:
Is it possible to cancel a constructor ? I mean, for example I have an object which is created by reading a file so there is a filename argument to the constructor. But if it happens the file is...
1
by: AP | last post by:
Hi, I'm trying to use c# to pop up a dialog box when a user attempts to close word to prompt them if they want to exit or cancel (obviously other stuff needs to happen based on their selection...
9
by: Player | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all. I am in the process of teaching myself C# and I think I am doing OK. I have learnt how to how to call the right constructor of a...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
3
by: Eidolon | last post by:
Is there any way that you can write your constructor so that it returns nothing? For example: Take a Customer object which has a constructor whose constructor has the signiture: Public Sub...
0
by: Stuart Whiteford | last post by:
Hi, I've got a basic web form, two textboxes, a couple of radio button groups, some required field validators, and a Submit and Cancel button. When the page loads, if I click the Cancel...
7
by: Pieter | last post by:
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...
12
by: NewToCPP | last post by:
does the default constructor initialize values? I have a class as defined below: class A { int i; char c; int * iPtr;
9
by: Morten Lemvigh | last post by:
Is it possible to pass a pointer to a constructor or a class definition as argument to a function? Maybe in a way similar to passing function pointers...? The function should construct a number...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.