0

I am new to object oriented prog in c#.So kindly bear me. I dont want to create object, if this.OrderCost() is more than 10000.00.How to drop this object. Is it correct to do validtion here. What is the best possible way.

 public Bank(string bankCode, string bankName)
    {
        this.bankCode= bankCode;
        this.bankName= bankName;


        if (this.orderCost() > moneyInBankAccount)
        {
            MessageBox.Show("Order amount exceeds the money in bank account.");
            this. = null;  // <--what to do here.
        }
    }
1
  • 3
    Some advice that I wish someone had told me when I was a new OO programmer: clearly separate your mechanisms from your business logic policies. Construction is a mechanism for creating new objects; it is not the appropriate place to implement a business policy Commented Oct 8, 2011 at 22:50

4 Answers 4

4

Validation of this type shouldn't been done in the constructor of the object. Instead it should be done in the method that is performing the action you intend to perform.

So, if you are attempting to deduct the money from the bank account to pay for an order you would perform the validation in the "Withdraw" method.

Sign up to request clarification or add additional context in comments.

Comments

2

Except in several very rare circumstances that are not applicable here, if a constructor returns, it either returns the constructed object or it throws an exception.

So, to avoid construction of an object that would be invalid, you should throw an exception. Or you could create a method that returns null is the object would be invalid and create it otherwise.

Also, you shouldn't deal with the UI in the domain objects, so don't show that message box there.

Comments

1

It is not possible to "assign" to this or somehow else prevent the constructor from doing its job. You can either throw exception or somehow else indicate, that the newly created object is invalid.

EDIT
You can also create a static method, that will return a Bank object if your conditions are met, or return null otherwise.

4 Comments

Even when exception is raised it still creates the object. Can you please specify how to do it.
It is too late to do anything in the constructor, the object has already been constructed. What you can do is throw an exception, in which case the code that assigns the reference to the new object to the variable will not execute, making the object eligible for garbage collection right away. However, unless you add a try/catch block in the calling code, this will likely not do what you want. Let me rephrase that: there is no way to "return null" from a constructor.
@LasseV.Karlsen "What you can do is throw an exception, in which case the code that assigns the reference to the new object to the variable will not execute, making the object eligible for garbage collection right away." Throw exception from within the constructor ?? or the place where i created the the object of Bank class.
Within the constructor. As I said, if the constructor is already executing, the object has been created. You can't return null.
0

Here is nothing new than other answers. Just to show how you can do it.

public class Bank
{
    public Bank(string bankCode, string bankName)
    {
        if (ConditionNotMet) throw new SomeException("");
        .....
    }
}

or

public class Bank
{
    private Bank(string bankCode, string bankName)
    {
    }

    public static Bank Create(string bankCode, string bankName)
    {
        if (ConditionNotMet) return null; //or throw Exception
        return new Bank(string bankCode, string bankName);
    }
}

If you are not convinced try to read the "I" of test class

public class Test
{
    public int I=0;
    public Test()
    {
        I=666;
        throw new Exception("No you can't read");
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.