473,657 Members | 2,625 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2447
> 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 ArgumentOutOfRa ngeException 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******** ******@TK2MSFTN GP11.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.c om> wrote
in message news:em******** ******@TK2MSFTN GP12.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 ArgumentOutOfRa ngeException 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******** ******@TK2MSFTN GP11.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
1691
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
3408
by: Act | last post by:
Why is it suggested to not define data members as "protected"? Thanks for help!
18
30257
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
2849
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 example dsParts.xsd and including that in the data tier. I then will create a class that looks like this Public Class CPart Inherits dsParts
14
3834
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, ZipCode, etc). So each instance of the struct will describe a person - and about 900 instances (people) will be contained in the collection. Users must be able to search for a specific person by any of the properties (e.g., LastName, FirstName,...
61
3737
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 (myBranch + x) = new Branch(i); // it doesn't x is a loop iterator, i is an int for the constructor to define an array. What am I doing wrong here.
12
13340
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 shortint like all these variables became int. How can we define our own user data type in C# is we can? Just D.
2
9741
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 by creating a IsNull method in my structs: struct TestStruct {
29
2761
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 overhead (I think). A Struct seems more appropriate. At least it is what I would have used in other languages. But since a Struct *can* hold methods, I wander if I am saving anything. If not, why use it?
0
8823
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5632
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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 we have to send another system
2
1950
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1607
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.