473,387 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Data validation for structs and classes?

Here's a very general programming question. I often wonder where to put the
data validation when adding data to a struct or class, using its
constructor.

Suppose for example I got a class called Account with the variable Balance
(float) and this variable shall not be negative. Shall I put this test to
the code that creates the class and call its constructor, or shall it be
part of the class itself?

If it should be part of the class, how do I handle the situation when there
is a negative number submitted? In that case I don't want the Account to be
created or updated. If data validation should *not* be part of the class,
that seem to make the class very fragile.

Gustaf

Nov 15 '05 #1
2 2433
> Suppose for example I got a class called Account with the variable Balance
(float) and this variable shall not be negative. Shall I put this test to
the code that creates the class and call its constructor, or shall it be
part of the class itself?
It should be a part of the class itself, according to the principle of
Encapsulation (one of the basic OOP principles).
If it should be part of the class, how do I handle the situation when there is a negative number submitted? In that case I don't want the Account to be created or updated. If data validation should *not* be part of the class,
that seem to make the class very fragile.
You should throw ArgumentOutOfRangeException when the class constructor is
supplied with values that do not fall into allowed range of values. To the
best of my knowledge, when an exception is thrown from a constructor, the
instance is not created.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Gustaf Liljegren" <gu**************@bredband.net> wrote in message
news:eQ**************@TK2MSFTNGP11.phx.gbl... Here's a very general programming question. I often wonder where to put the data validation when adding data to a struct or class, using its
constructor.

Suppose for example I got a class called Account with the variable Balance
(float) and this variable shall not be negative. Shall I put this test to
the code that creates the class and call its constructor, or shall it be
part of the class itself?

If it should be part of the class, how do I handle the situation when there is a negative number submitted? In that case I don't want the Account to be created or updated. If data validation should *not* be part of the class,
that seem to make the class very fragile.

Gustaf


Nov 15 '05 #2
Hi,

the instance *will* get created, that is, allocated on the heap. It just
won't get fully constructed because of the exception, if thrown. In that
case, you don't get a chance to reference the just-created object, which
will get garbage-collected at some later point in time.
I agree on validating the arguments within the constructor and throwing
exceptions if they don't.
(I answered a same question yesterday)

Greetings,

Bram.
"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote
in message news:em**************@TK2MSFTNGP12.phx.gbl...
Suppose for example I got a class called Account with the variable Balance (float) and this variable shall not be negative. Shall I put this test to the code that creates the class and call its constructor, or shall it be
part of the class itself?


It should be a part of the class itself, according to the principle of
Encapsulation (one of the basic OOP principles).
If it should be part of the class, how do I handle the situation when

there
is a negative number submitted? In that case I don't want the Account to

be
created or updated. If data validation should *not* be part of the class, that seem to make the class very fragile.


You should throw ArgumentOutOfRangeException when the class constructor is
supplied with values that do not fall into allowed range of values. To the
best of my knowledge, when an exception is thrown from a constructor, the
instance is not created.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Gustaf Liljegren" <gu**************@bredband.net> wrote in message
news:eQ**************@TK2MSFTNGP11.phx.gbl...
Here's a very general programming question. I often wonder where to put

the
data validation when adding data to a struct or class, using its
constructor.

Suppose for example I got a class called Account with the variable Balance (float) and this variable shall not be negative. Shall I put this test to the code that creates the class and call its constructor, or shall it be
part of the class itself?

If it should be part of the class, how do I handle the situation when

there
is a negative number submitted? In that case I don't want the Account to

be
created or updated. If data validation should *not* be part of the class, that seem to make the class very fragile.

Gustaf

Nov 15 '05 #3

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

Similar topics

4
by: Der Andere | last post by:
Two short questions: 1. Is there any implementation of sets in PHP4? 2. Can I define structs (e.g., as in C++) or do I have to use classes instead?? Cheers, Matthias --
28
by: Act | last post by:
Why is it suggested to not define data members as "protected"? Thanks for help!
18
by: Rick | last post by:
Hi, We all know that Java has classes but how about basic storage objects like structs? C and C++ have Structs, Pascal has Records, Visual Basic has Types etc. How about Java? Greetings, Rick
18
by: D Witherspoon | last post by:
I am developing a Windows Forms application in VB.NET that will use .NET remoting to access the data tier classes. A very simple way I have come up with is by creating typed (.xsd) datasets. For...
14
by: Jeff S. | last post by:
In a Windows Forms application I plan to have a collection of structs - each of which contains a bunch of properties describing a person (e.g., LastName, FirstName, EmployeeID, HomeAddress,...
61
by: Marty | last post by:
I am new to C# and to structs so this could be easy or just not possible. I have a struct defined called Branch If I use Branch myBranch = new Branch(i); // everything works If I use Branch...
12
by: Just D | last post by:
All, It was possible before in Pascal, C++, etc. to define our custom data type or redefine the existing type, like in Turbo Pascal we could assume that shortint is int and use all references to...
2
by: S. Lorétan | last post by:
Hello. I have some structs in different namespaces/classes/other structs and I sometime have to check if it contains something or not. myStruct == null doesn't work. I've currently done it...
29
by: Dom | last post by:
I'm really confused by the difference between a Struct and a Class? Sometimes, I want just a group of fields to go together. A Class without methods seems wrong, in that it carries too much...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

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.