473,382 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 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 1428
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.