473,497 Members | 2,190 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Not creating an instance

I am trying to create a class that may have an error in the constructor.

For example:

using System;

public class Demo
{
public Demo ()
{
Some code that reads database and fails for some reason
}
}

If the constructor fails, is there a way to just return a null object?

Such that I could write:

Demo theDemo = new Demo()
if (theDemo == null) then ...

Thanks,

Tom
Mar 10 '06 #1
6 1207
No there is not.

You have to throw an exception.

Mar 11 '06 #2
And it's bad practice to do any substantial work in your constructor. It's
nasty to have constructors throw exceptions too. I would recommend just
*constructing* in your constructor and putting the meaty work into a
separate method.

HTH,
Kent
"Bruce Wood" <br*******@canada.com> wrote in message
news:11*********************@e56g2000cwe.googlegro ups.com...
No there is not.

You have to throw an exception.

Mar 11 '06 #3
"tshad" <ts**********@ftsolutions.com> wrote
If the constructor fails, is there a way to just return a null object?

Such that I could write:

Demo theDemo = new Demo()
if (theDemo == null) then ...


You could simply try making the constructor private and using a static
method to call it (and do the "dirty" work which could fail):

private thingy() {
//... construct it
}

public static thingy createThingy() {
thingy t = new thingy();
// ... make dirty things with it.
if ( /* failure */) {
return null;
}
return t;
}

Mar 11 '06 #4
Kent Boogaart <ke****@internode.on.net> wrote:
And it's bad practice to do any substantial work in your constructor. It's
nasty to have constructors throw exceptions too. I would recommend just
*constructing* in your constructor and putting the meaty work into a
separate method.


It's not particularly nasty to have constructors throwing exceptions in
..NET. It causes difficulties in unmanaged C++, but there's no issue
about it in .NET.

There are plenty of system libraries which do it, and quite rightly in
my view. If you try to create a reading FileStream on a file which
doesn't exist, I think it's perfectly reasonable to throw an exception.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 11 '06 #5

"Flo 'Irian' Schaetz" <ir***@arcor.de> wrote in message
news:44***********************@newsread4.arcor-online.net...
"tshad" <ts**********@ftsolutions.com> wrote
If the constructor fails, is there a way to just return a null object?

Such that I could write:

Demo theDemo = new Demo()
if (theDemo == null) then ...


You could simply try making the constructor private and using a static
method to call it (and do the "dirty" work which could fail):


<snip>

Why make the ctor private?
If you leave it public you can have it either way.
Mar 11 '06 #6

"Kent Boogaart" <ke****@internode.on.net> wrote in message
news:%2******************@tk2msftngp13.phx.gbl...
And it's bad practice to do any substantial work in your constructor. It's
nasty to have constructors throw exceptions too. I would recommend just
*constructing* in your constructor and putting the meaty work into a
separate method.


It is not bad practice!

IMHO when designing anything invariants are your greatest aid to
understanding and bug free code and constructors that don't do all the work
drastically reduce the number of invariants in your class and typically
increase the number of states from 2 (initialized,disposed) to 3
(+uninitialized) [Unfortunately you cannot reduce it to just 1 state as you
can with C++]

Furthermore do you propose that the init method should throw an exception?
In which case what have you gained?
Mar 11 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1756
by: jm | last post by:
I have a Form1 and two class files. In the first class, I call the second class. The second class references the Form1 notifyicon and changes the icon. It works. The problems is, however,...
15
6715
by: Carlos Lozano | last post by:
Hi, What is the right way to create an OCX COM component. The component is already registerred, but can't create an instance. I am using the reference to the interop module created. If I use...
12
3137
by: Mats Lycken | last post by:
Hi, I'm creating a CMS that I would like to be plug-in based with different plugins handling different kinds of content. What I really want is to be able to load/unload plugins on the fly without...
3
1365
by: Mag | last post by:
I am trying to install onDemand content manager for Linux I am looking at these instructions http://publib.boulder.ibm.com/infocenter/cmod/v8r3m0//topic/com.ibm.ondemand.mp.doc/ars1i07135.htm ...
1
4075
by: vijju | last post by:
Hi all ! I'm an amateur to DB2.I installed DB2 ESE trial edition on my system.It was all going well and suddenly when i was trying to create an instance, i faced a problem which i listed it below....
7
1335
by: Brett Romero | last post by:
I need a static version of a class that can be referenced anywhere as a singleton and the same class that can be used as instances. Can this be done without basically creating the same class twice...
26
5333
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
19
1908
by: =?Utf-8?B?WWFua2VlIEltcGVyaWFsaXN0IERvZw==?= | last post by:
I'm doing my c# more and more like i used to code c++, meaning i'm casting more often than creating an instance of objects. like : protected void gvOrderDetailsRowDataBound(object sender,...
3
1461
by: Peter Morris | last post by:
Hi all Let's say I am creating a model to represent classes and properties. In addition to this I need instances of classes and values for those properties. KEY: (AssociationEndName)
4
8945
by: =?Utf-8?B?Z3JlZw==?= | last post by:
I am trying to create an instance of a 3rd party COM component, and I'm getting an error. I've opened a ticket with this vendor as well, but I want to approach it from the C# .NET side to make...
1
6878
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7373
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5456
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4897
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4583
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1405
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
649
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.