Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we interrupt creating an object in constructor

Tags:

c#

constructor

Could you help me please. I have one idea but don't know how can I implement it.

So the question is: Can we interrupt creating an object in constructor i.e.

//Code

SomeClass someClass = new SomeClass(someCriteria);

So if someCriteria doesn't answer on our requirements we shouldn't create an object and should return null, instead of new object.

Is it possible to implement it in C#?

like image 818
Sergey Avatar asked Mar 14 '11 15:03

Sergey


People also ask

How do you stop the initiation of an object in a constructor?

The only way to "stop" a constructor is to throw an exception.

How do you stop an object from creation in Java?

In java we can avoid object creation in 2 ways : Making the class as abstract, so we can avoid unnecessary object creation with in the same class and another class. Making the constructor as private ( Singleton design pattern ), so we can avoid object creation in another class but we can create object in parent class.

Do Constructors make objects?

Simply speaking, a constructor does not create an object. It just initializes the state of the object. It's the new operator which creates the object.


3 Answers

Best way is a factory class but if your class is so small you can use this:

class SomeClass
    {
        private string _someCriteria;

        private SomeClass(string someCriteria)
        {
            _someCriteria = someCriteria;
        }

        public static SomeClass CreateInstance(string someCriteria)
        {
            if (someCriteria.Length > 2)
            {
                return new SomeClass(someCriteria);
            }
            return null;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {

            // returns null
            SomeClass someClass = SomeClass.CreateInstance("t");

            // returns object
            SomeClass someClass2 = SomeClass.CreateInstance("test");
        }
    }
like image 163
Cihan Yakar Avatar answered Sep 19 '22 12:09

Cihan Yakar


You may want to use a factory class that creates instances of SomeClass returning null if the someCriteria is invalid.

like image 28
bleeeah Avatar answered Sep 22 '22 12:09

bleeeah


It is quite usual that you check the constructor parameters if they are valid. If not, you usually throw an exception.

I also read a nice advice to provide static methods for validating constructor parameters. This enables the user of your class to check if whatever he is going to pass in the constructor will succeed or not. Those who are certain the parameters are ok (they made some sort of their validation) will go directly with the constructor.

Also consider what the user of your class will possibly do with null instead of the object (if some sort of factory class was used). Would this typically result in an NullPointerException on the next line? It's usually good idea to stop the wrong things as soon as possible, in this case throw the exception and finish. It is cleaner solution than returning null and if someone really wants to (this definetly won't be best practise) he can still catch this exception ...

like image 28
Jan Zyka Avatar answered Sep 21 '22 12:09

Jan Zyka