class initialisation vallidation

A

Adie

Hi, wonder if anyone could give me some code advice.

I'm wondering about the best way to validate if all inputs are present
and correct for object creation in an ASP.NET application.

Not sure how to explain this, but I really want to separate the
interface from the business logic as I may eventually move to windows
forms. So I really don't want to validate on the form inputs, rather
in the class's.

So say I provide too few initialisers for class creation, I want the
object to not create itself and somehow return null or something, so
that I can pass this along to the GUI and the user be informed.

I thought maybe using something akin to the code in the singleton
pattern, where the constructor is protected and called from an
initialisers method, which could do the validations. But I'm not sure
and am really curious as to how experienced developers tackle this
sort of problem.
 
B

Bram

Hi,

you could validate the inputs within the constructor(s) and throw an
exception when something is wrong or missing.

Greetings,
Bram.
 
A

Adie

Bram said:
Hi,

you could validate the inputs within the constructor(s) and throw an
exception when something is wrong or missing.

You mean I can pass an exception back from a constructor?

Sorry havent used much in the way of exception handling in the past,
straight out of University programmer here - bit wet behind the ears
:)
 
B

Bram

Hi,

an example,

class PositiveNumber
{
private int i;
public PositiveNumber(int i)
{
// let's check the arguments first
if (i < 0)
throw new InvalidArgumentException("i"); // let's pass some info
with the exception object

this.i = i;
}
}

now if you try to create a PositiveNumber with a negative value, an
exception is thrown. The constructor will (in that case) not run till it's
end. Instead, execution will resume at the first catch clause willing to
handle this kind of exception. You might need to read a bit about
exceptions.

an example,

PositiveNumber number;
try // there might go something wrong in the following code, so let's guard
it
{
number = new PositiveNumber(-1);
}
catch (InvalidArgumentException e) // only catching InvalidArgumentException
exceptions here
{
MessageBox.Show("invalid argument passed: " + e.Message);
// you can do whatever you like here, like creating a default value
number = PositiveNumber(0);
}


Writing the evaluation code within the constructor keeps everything clean
(that's what object-oriented programming is all about).
One possible downside: objects are always created, that is, allocated on the
gc heap, even with invalid arguments, but they will be discarded immediately
because of the exception thrown. Usually nothing to worry about.
Exceptions are supposed not to happen frequently. That's why they are
called exceptions. Everything will be fine, except ...

Hope this helped,

Greetings,

Bram.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top