473,473 Members | 1,480 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

class initialisation vallidation

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.

Nov 15 '05 #1
5 1228
Hi,

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

Greetings,
Bram.

"Adie" <ar*******@h-o-t-m-a-i-l.com> wrote in message
news:8d******************************@news.teranew s.com...
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.

Nov 15 '05 #2
Bram wrote:
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
:-)
"Adie" <ar*******@h-o-t-m-a-i-l.com> wrote in message
news:8d******************************@news.terane ws.com...
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.


Nov 15 '05 #3
Adie wrote:
Bram wrote:
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?


Right, think I know what youre getting at:
http://msdn.microsoft.com/library/de...Components.asp

might do the trick -- I think?!?!

We'll see.
Nov 15 '05 #4
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.

"Adie" <ar*******@h-o-t-m-a-i-l.com> wrote in message
news:71******************************@news.teranew s.com...
Adie wrote:
Bram wrote:
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?


Right, think I know what youre getting at:

http://msdn.microsoft.com/library/de...Components.asp
might do the trick -- I think?!?!

We'll see.

Nov 15 '05 #5
Bram wrote:
Hi,

an example,
/* snip example */
Hope this helped,


Bram, that was superb, really, exactly what I was after.

Thanks.
Nov 15 '05 #6

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

Similar topics

106
by: A | last post by:
Hi, I have always been taught to use an inialization list for initialising data members of a class. I realize that initialsizing primitives and pointers use an inialization list is exactly the...
1
by: matro | last post by:
hi there, I have the following struct in my class: class CTbStDialog : public CDialog { public: int fooVar, fooVar2; ... //other foo variables...
8
by: jose luis fernandez diaz | last post by:
Hi, I am reading Stroustrup's book 'C++ Programming Language'. In the 10.4.9 section (Nonlocal Store) he says: "A variable defined outside any function (that is global, namespace, and class...
4
by: Hunter Hou | last post by:
Folks, If a member variable is initialized in class, it must be static and const. I know this and regard it as a convention. But who knows why standard define this? What's the advangtage or...
6
by: Fred | last post by:
Hi I have a class defined in a library that I'd like to add some extra functionality to. This will involve adding a few member variables and a few related methods. As I understand it I can...
3
by: deancoo | last post by:
I'm hoping this isn't too off topic, but I'm not sure if I've included some of my member functions in the right class. I needed to develop numerous functions to help define object A. These...
5
by: Adie | last post by:
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...
5
by: silverburgh.meryl | last post by:
How come I an not use class initialization to initalize inherited attributes? i have this code: B::B(A& a) { x = a.x; y = a.y; } class A {
2
by: .rhavin grobert | last post by:
i have (do try to have?) the following... & = breakpoints in debugger // ---------------------------------------------------------------- // cx.h class CX { public: CX(CX* pcx = NULL);...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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
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
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
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
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
muto222
php
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.