Object creation at internal condition in constructor
-
Hi All, I'm having an issue with constructors. I want to prevent the creation internally in the following example:
using System; public class ConditionalObject { private string _MajorName; private string _MinorName; public string MajorName { get { return _MajorName; } } public string MinorName { get { return _MinorName; } } public ConditionalObject() { if ((_MajorName.Length == 0) || (_MinorName.Length == 0)) { throw new Exception("Unable to initialise new object without identifier."); /******* what to do here to be destroyed? *******/ } } public ConditionalObject(string AMajorName, string AMinorName): base() { _MajorName = AMajorName; _MinorName = AMinorName; } }
Neither Dispose(); nor return null; nor this = null; can be applied in constructor. Could anybody help me guys? Thanks for the answers. :)I don't know what ur actual intention is, If your intention is to prevent object creation using default constructor aking ur Default Constructor private will solve ur issue.
-
Tony Lambert wrote:
I just throw it, it raises the exception message and the constructor returns with the object reference in the caller method instead of null.
No, it doesn't. If you throw an exception in the constructor, the created object is never used. You don't get a reference to the object, as the reference is not assigned to any variable.
Tony Lambert wrote:
Actually, this is the missing part of the puzzle: how to destroy or give null operand to the object in its constructor or mark it as destroyable or return with null?
You can't do that, and you don't need to, as the reference to the object will never be used. Just make sure that the object is left in a state so that it can be garbage collected.
--- It's amazing to see how much work some people will go through just to avoid a little bit of work.
I fail to see the use for a constructor that is not sufficient for instanciating your object. Why have the constructor there if it will make an object you deem invalid? Remove it and only accept calls to constructors that initialize the fields you need set. -Larantz-
for those about to code, we salute you
http://www.tellus-software.com -
I fail to see the use for a constructor that is not sufficient for instanciating your object. Why have the constructor there if it will make an object you deem invalid? Remove it and only accept calls to constructors that initialize the fields you need set. -Larantz-
for those about to code, we salute you
http://www.tellus-software.com"Remove it and only accept calls to constructors that initialize the fields you need set." Well it is okay in end user application, but it's an abstract class in library. Since it will be called by end-developers, the API must be consistent and any internal operation must to be transparent to make the library clear, and understandable.
-
Tony Lambert wrote:
I just throw it, it raises the exception message and the constructor returns with the object reference in the caller method instead of null.
No, it doesn't. If you throw an exception in the constructor, the created object is never used. You don't get a reference to the object, as the reference is not assigned to any variable.
Tony Lambert wrote:
Actually, this is the missing part of the puzzle: how to destroy or give null operand to the object in its constructor or mark it as destroyable or return with null?
You can't do that, and you don't need to, as the reference to the object will never be used. Just make sure that the object is left in a state so that it can be garbage collected.
--- It's amazing to see how much work some people will go through just to avoid a little bit of work.
Thanks again. Here is a simple code:
using System; public class ConditionalObject { public ConditionalObject(string name) { try { if (name.Equals("")) { throw new ArgumentOutOfRangeException(this.ToString() + ": Unable to create new object without identifier."); } } catch (ArgumentOutOfRangeException AException) { Console.WriteLine(AException.Message); } } } public class CallerClass { public static void Main() { ConditionalObject obj = new ConditionalObject(""); Console.WriteLine(obj == null ? "null" : "not null"); } }
When I change this:ConditionalObject obj = new ConditionalObject("");
to this:ConditionalObject obj = new ConditionalObject("anything");
the last line gives the same value (not null). What did I wrong? :doh: -
Thanks again. Here is a simple code:
using System; public class ConditionalObject { public ConditionalObject(string name) { try { if (name.Equals("")) { throw new ArgumentOutOfRangeException(this.ToString() + ": Unable to create new object without identifier."); } } catch (ArgumentOutOfRangeException AException) { Console.WriteLine(AException.Message); } } } public class CallerClass { public static void Main() { ConditionalObject obj = new ConditionalObject(""); Console.WriteLine(obj == null ? "null" : "not null"); } }
When I change this:ConditionalObject obj = new ConditionalObject("");
to this:ConditionalObject obj = new ConditionalObject("anything");
the last line gives the same value (not null). What did I wrong? :doh: -
Because you catch the exception inside the constructor, the constructor never exits with an exception.
--- It's amazing to see how much work some people will go through just to avoid a little bit of work.
It is the same without catch (first example). When I throw an exception it only gets registered in an exception buffer till something catches. It does nothing with the constructor itself. That is the reason why it comes back with not null value all the time. :( When I don't catch it, the application gets killed at the exit point of constuctor. That's another problem: I need that null value. :( Anyway, somebody has given to me another solution with indirect static method initialisation in the next thread. I think I'll do it that way. :doh: Thanks to All again. :)
-
It is the same without catch (first example). When I throw an exception it only gets registered in an exception buffer till something catches. It does nothing with the constructor itself. That is the reason why it comes back with not null value all the time. :( When I don't catch it, the application gets killed at the exit point of constuctor. That's another problem: I need that null value. :( Anyway, somebody has given to me another solution with indirect static method initialisation in the next thread. I think I'll do it that way. :doh: Thanks to All again. :)
Tony Lambert wrote:
It is the same without catch (first example). When I throw an exception it only gets registered in an exception buffer till something catches. It does nothing with the constructor itself. That is the reason why it comes back with not null value all the time.
An exception buffer? Why do you think that? When you throw an exception in the constructor it immediately exits. The exception isn't stored in any buffer anywhere, it's returned from the constructor.
Tony Lambert wrote:
When I don't catch it, the application gets killed at the exit point of constuctor.
No, the application doesn't get killed. The constructor exits with an exception, and the exception travels through each level in the call stack until it's caught somewhere. As you don't catch it in your program, the program exits with the exception and it's caught by the system.
Tony Lambert wrote:
That's another problem: I need that null value.
A constructor never returns a null value, it always returns the address of the object. If you wan't to abort a constructor, an exception is the only way to do that, and you have to catch the exception in the code that calls the constructor for it to be useful.
--- It's amazing to see how much work some people will go through just to avoid a little bit of work.
-
"Remove it and only accept calls to constructors that initialize the fields you need set." Well it is okay in end user application, but it's an abstract class in library. Since it will be called by end-developers, the API must be consistent and any internal operation must to be transparent to make the library clear, and understandable.
Quote:
public class ConditionalObject { public ConditionalObject(string name) { try { if (name.Equals("")) { throw new ArgumentOutOfRangeException(this.ToString() + ": Unable to create new object without identifier."); } } catch (ArgumentOutOfRangeException AException) { Console.WriteLine(AException.Message); } } }
Your next example was according to what I meant. No empty constructors :) -Larantz-
for those about to code, we salute you
http://www.tellus-software.com -
Tony Lambert wrote:
It is the same without catch (first example). When I throw an exception it only gets registered in an exception buffer till something catches. It does nothing with the constructor itself. That is the reason why it comes back with not null value all the time.
An exception buffer? Why do you think that? When you throw an exception in the constructor it immediately exits. The exception isn't stored in any buffer anywhere, it's returned from the constructor.
Tony Lambert wrote:
When I don't catch it, the application gets killed at the exit point of constuctor.
No, the application doesn't get killed. The constructor exits with an exception, and the exception travels through each level in the call stack until it's caught somewhere. As you don't catch it in your program, the program exits with the exception and it's caught by the system.
Tony Lambert wrote:
That's another problem: I need that null value.
A constructor never returns a null value, it always returns the address of the object. If you wan't to abort a constructor, an exception is the only way to do that, and you have to catch the exception in the code that calls the constructor for it to be useful.
--- It's amazing to see how much work some people will go through just to avoid a little bit of work.
"An exception buffer? Why do you think that?" call stack :) Thanks Guffa. I think you're right, it comes back with object reference. BTW, I've solved this issue across a static init method, where I do the try/cacth as well before the instance gets created. The constructor is hidden now. :-)
-
Quote:
public class ConditionalObject { public ConditionalObject(string name) { try { if (name.Equals("")) { throw new ArgumentOutOfRangeException(this.ToString() + ": Unable to create new object without identifier."); } } catch (ArgumentOutOfRangeException AException) { Console.WriteLine(AException.Message); } } }
Your next example was according to what I meant. No empty constructors :) -Larantz-
for those about to code, we salute you
http://www.tellus-software.com:)