473,324 Members | 1,678 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,324 software developers and data experts.

Common practice, allowing null values in data classes.

What is the accepted method of creating a data class or business rules
object class with properties that will allow the returning of null values?

For example... I have a class named CResults with the following properties.

TestID int
QuestionID int
AnswerID int

So, this is a simple example, but I want to be able to know if AnswerID is
null. I don't want to know that it is 0 or anything else. I want to be
able to have it return null (instead of the default "nothing" value of 0)

This is not my real world example, it is just an example I'm using to
explain what I want to do. I am developing a large scale application and
the properties of the classes can contain null or legitmate values. If
there is an integer property I want to know if it was 0 or null. There is a
huge difference, as 0 implies that literally 0 was entered into the
database, and null is that nothing has been entered in the database.

How can I design my data classes using VB.NET to take this into account.
The intrinsic datatypes in VB (integer, long, string, etc) can not have null
values.

What is common practice in this case?
Jul 21 '05 #1
12 1740
One way would be to define your properties as SQL Types, they have an IsNull
property, I didn't like this I use strings instead, that way I can format it
however I want when I pull from and put into the database, it the string is
empty I put a null in the database or expect that value to be null in the
database. It works ok for me...
"D Witherspoon" wrote:
What is the accepted method of creating a data class or business rules
object class with properties that will allow the returning of null values?

For example... I have a class named CResults with the following properties.

TestID int
QuestionID int
AnswerID int

So, this is a simple example, but I want to be able to know if AnswerID is
null. I don't want to know that it is 0 or anything else. I want to be
able to have it return null (instead of the default "nothing" value of 0)

This is not my real world example, it is just an example I'm using to
explain what I want to do. I am developing a large scale application and
the properties of the classes can contain null or legitmate values. If
there is an integer property I want to know if it was 0 or null. There is a
huge difference, as 0 implies that literally 0 was entered into the
database, and null is that nothing has been entered in the database.

How can I design my data classes using VB.NET to take this into account.
The intrinsic datatypes in VB (integer, long, string, etc) can not have null
values.

What is common practice in this case?

Jul 21 '05 #2
What I usually do is have another property that indicates whether the value is
valid or not. For example, you could add a property "HasAnswerID" that would
return true if there is a valid (not null) AnswerID value and false if the
AnswerID value is null.

The AnswerID property would always return an int value, with some default value
if the underlying data is null.

Another option is to return an object value set to null if the underlying data
is null and to the integer value if it is not. The problem is that you have to
cast it as an int before you can use it.

--
Rodger

<http://www.SequenceDiagramEditor.com>
Sequence Diagram Editor - A quick and easy way to draw and edit sequence diagrams.

D Witherspoon wrote:
What is the accepted method of creating a data class or business rules
object class with properties that will allow the returning of null values?

For example... I have a class named CResults with the following properties.

TestID int
QuestionID int
AnswerID int

So, this is a simple example, but I want to be able to know if AnswerID is
null. I don't want to know that it is 0 or anything else. I want to be
able to have it return null (instead of the default "nothing" value of 0)

This is not my real world example, it is just an example I'm using to
explain what I want to do. I am developing a large scale application and
the properties of the classes can contain null or legitmate values. If
there is an integer property I want to know if it was 0 or null. There is a
huge difference, as 0 implies that literally 0 was entered into the
database, and null is that nothing has been entered in the database.

How can I design my data classes using VB.NET to take this into account.
The intrinsic datatypes in VB (integer, long, string, etc) can not have null
values.

What is common practice in this case?

Jul 21 '05 #3
Don't define the data type to be 'int'. Use the SQL data types instead,
like SqlInt32
See
http://msdn.microsoft.com/library/en...taSqlTypes.asp
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"D Witherspoon" <dw**********@noway.org> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
What is the accepted method of creating a data class or business rules
object class with properties that will allow the returning of null values?

For example... I have a class named CResults with the following
properties.

TestID int
QuestionID int
AnswerID int

So, this is a simple example, but I want to be able to know if AnswerID is
null. I don't want to know that it is 0 or anything else. I want to be
able to have it return null (instead of the default "nothing" value of 0)

This is not my real world example, it is just an example I'm using to
explain what I want to do. I am developing a large scale application and
the properties of the classes can contain null or legitmate values. If
there is an integer property I want to know if it was 0 or null. There is
a huge difference, as 0 implies that literally 0 was entered into the
database, and null is that nothing has been entered in the database.

How can I design my data classes using VB.NET to take this into account.
The intrinsic datatypes in VB (integer, long, string, etc) can not have
null values.

What is common practice in this case?

Jul 21 '05 #4
D,

An integer can be nothing however has than forever the value 0.

So I think that when you would use the integer for two purposes
1 the value
2 to evaluate
That the solution can be to make it not an integer however an object.

(Where I assume that your busines object is not a datatable where this is in
the non typed format build in).

Just my thought,

Cor
Jul 21 '05 #5
Thanks to everyone for their assistance.

I have come up with a different solution than the ones you guys mentioned.

I am going to create a class object, maybe called CInteger and have 2
properties. Value, and IsNull.
This will be the return type for my integer fields.

I don't like using SQL data types as the idea for my data classes is for
them to be generic and not tied to any particular DBMS data types.

This is the best idea I could come up with. Let me know what you guys
think.

"Nick Malik [Microsoft]" <ni*******@hotmail.nospam.com> wrote in message
news:4N********************@comcast.com...
Don't define the data type to be 'int'. Use the SQL data types instead,
like SqlInt32
See
http://msdn.microsoft.com/library/en...taSqlTypes.asp
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"D Witherspoon" <dw**********@noway.org> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
What is the accepted method of creating a data class or business rules
object class with properties that will allow the returning of null
values?

For example... I have a class named CResults with the following
properties.

TestID int
QuestionID int
AnswerID int

So, this is a simple example, but I want to be able to know if AnswerID
is null. I don't want to know that it is 0 or anything else. I want to
be able to have it return null (instead of the default "nothing" value of
0)

This is not my real world example, it is just an example I'm using to
explain what I want to do. I am developing a large scale application and
the properties of the classes can contain null or legitmate values. If
there is an integer property I want to know if it was 0 or null. There
is a huge difference, as 0 implies that literally 0 was entered into the
database, and null is that nothing has been entered in the database.

How can I design my data classes using VB.NET to take this into account.
The intrinsic datatypes in VB (integer, long, string, etc) can not have
null values.

What is common practice in this case?


Jul 21 '05 #6
D,

I am curious why not my answer?

Cor

Jul 21 '05 #7
From what I understood is that the return types of all of the properties
would be of Object. I'd like to specify the type of value being returned or
passed to the property.

"Cor Ligthert" <no************@planet.nl> wrote in message
news:ek**************@TK2MSFTNGP12.phx.gbl...
D,

I am curious why not my answer?

Cor

Jul 21 '05 #8
Cor,

Your solution is really inefficient since many of the types are value types
and using the Object type requires a great deal of boxing and unboxing.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Cor Ligthert" <no************@planet.nl> wrote in message
news:ek**************@TK2MSFTNGP12.phx.gbl...
D,

I am curious why not my answer?

Cor

Jul 21 '05 #9
Hi Cor,

Measurements work by comparison. I see that you are measuring the amount of
time it takes to run through some box and unbox routines (not quite
perfect... your assignment results in boxing the value, unboxing it, boxing
it again).

I don't see a comparison to the amount of time it would take to use a
SQLInt32 to store a number that contains an "isnull" column.
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Cor Ligthert" <no************@planet.nl> wrote in message
news:OX**************@TK2MSFTNGP14.phx.gbl...
Nick,

And too proof what I wrote, try this sample beneath.
I am curious about your answer,

Cor

\\\
Public Class Mytest
Public Shared Sub Main()
Dim start As Integer = Environment.TickCount
For i As Integer = 0 To 10000000
Dim a As New hulpclass
If a.myval Is Nothing Then
a.myval = 0 'Box
Else
a.myval = 1 'Box
End If
If CInt(a.myval) = 0 Then 'Unboxing
a.myval = 0 'Box
Else
a.myval = 1 'Box
End If
Next
Dim ends As Integer = Environment.TickCount - start
MessageBox.Show("Processing this routine 10.000.000 times cost: " &
ends.ToString _
& " Milliseconds")
End Sub
End Class
Public Class hulpclass
Private m As Object
Public Property myval() As Object
Get
Return m
End Get
Set(ByVal Value As Object)
m = CInt(Value)
End Set
End Property
End Class
///

Jul 21 '05 #10

"Nick Malik [Microsoft]"

Measurements work by comparison.
No measurments works by taking the zero point and than measure what is the
distance/weight or whatever from that. I think you are mixing things up.
I see that you are measuring the amount of time it takes to run through
some box and unbox routines >your assignment results in boxing the value,
unboxing it, boxing it again).
Exact that you have seen well, that was what I was after. When you take the
simple short time, (maximum one minute for a pro) too test it, than you can
see why I took more time to make it. The most important reason was trying to
help you to overcome that dogma of boxing. That seems as a strange
prejudiced idea in the mind of some people.
(not quite perfect... )
May I know what is the intention from this extra sentence in your message. I
hope it is not personal meant, because I did not see that I did something to
you, when that is the fact, feel at such a moment to tell me, because that
is never intended.
I don't see a comparison to the amount of time it would take to use a
SQLInt32 to store a number that contains an "isnull" column.


That was not what I was after, however you can take my sample to show what
you can win with that.

I am not able to use a SQLInt32 direct as a value in a program, I know how
to do that, however that is exactly in the way I showed.

And because of that last fact am I *very* curious for another method and
will be very glad when you make that. (I wrote that not earlier in this
thread, because I was not wanting to discuss that).

Cor
Jul 21 '05 #11
Hi Cor,

I took your challenge and wrote my own timing app. I used the HiPerfTimer
class from CodeProject and ran a simply box/unbox operation from 10,000
SqlInt32 values where about 20% of the SqlInt32 values were nulls. (to
simulate a normal situation).

I created two classes: one that used an internal "object" type value and
would allow the caller to test if the object is null, the other used two
internal values, one of type int, and the other of type boolean, to meet a
common interface.

It was interesting. The first time I ran it, the class based on storing
values as an object ran in about 0.0073 seconds. However, simply running
the test again (on the same data) returned about 0.0038 seconds. Crazy! On
the other hand, the class based on two integral types ran in about 0.0033
seconds every time.

(I keep saying "about X seconds" because I ran each test many times. The
numbers were slightly different each time, but they average out to the
number presented).

So, for 10,000 box/unbox operations, there is a performance hit, but it is
much smaller than I expected. Even assuming worst case, the difference is
so small that it is not likely to be human-percievable.

So... you are right. The difference is probably inconsequential from a
performance standpoint. Readability is another issue (and a different
discussion). And, then, there's personal preference. It appears that these
factors may weigh greater when determining which way a developer SHOULD go.

I'll see if I can get my little comparison app uploaded to GotDotNet or
CodeProject in the next day or so, to allow others to take a look.

I learned something today. Thank you Cor.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Cor Ligthert" <no************@planet.nl> wrote in message
news:e2**************@TK2MSFTNGP10.phx.gbl...

"Nick Malik [Microsoft]"

Measurements work by comparison.


No measurments works by taking the zero point and than measure what is the
distance/weight or whatever from that. I think you are mixing things up.
I see that you are measuring the amount of time it takes to run through
some box and unbox routines >your assignment results in boxing the value,
unboxing it, boxing it again).


Exact that you have seen well, that was what I was after. When you take
the simple short time, (maximum one minute for a pro) too test it, than
you can see why I took more time to make it. The most important reason was
trying to help you to overcome that dogma of boxing. That seems as a
strange prejudiced idea in the mind of some people.
(not quite perfect... )


May I know what is the intention from this extra sentence in your message.
I hope it is not personal meant, because I did not see that I did
something to you, when that is the fact, feel at such a moment to tell me,
because that is never intended.
I don't see a comparison to the amount of time it would take to use a
SQLInt32 to store a number that contains an "isnull" column.


That was not what I was after, however you can take my sample to show what
you can win with that.

I am not able to use a SQLInt32 direct as a value in a program, I know how
to do that, however that is exactly in the way I showed.

And because of that last fact am I *very* curious for another method and
will be very glad when you make that. (I wrote that not earlier in this
thread, because I was not wanting to discuss that).

Cor

Jul 21 '05 #12
Another method is to fill int.MinValue or int.MaxValue to int property
AnswerID . So if you nead to check if db returned null just check if AnswerID
property is int.MinValue.

When passing value to stored procedure parameter just check again is it
int.MinValue, and if it is just pass DBNull.Value (I usually use IIf function
in VB.NET or ( ? :) syntax in C#)

The same way you could use for other types (especially for DateTime,
Decimal, ...)

This way performances will not be affected.

Boris

"D Witherspoon" wrote:
What is the accepted method of creating a data class or business rules
object class with properties that will allow the returning of null values?

For example... I have a class named CResults with the following properties.

TestID int
QuestionID int
AnswerID int

So, this is a simple example, but I want to be able to know if AnswerID is
null. I don't want to know that it is 0 or anything else. I want to be
able to have it return null (instead of the default "nothing" value of 0)

This is not my real world example, it is just an example I'm using to
explain what I want to do. I am developing a large scale application and
the properties of the classes can contain null or legitmate values. If
there is an integer property I want to know if it was 0 or null. There is a
huge difference, as 0 implies that literally 0 was entered into the
database, and null is that nothing has been entered in the database.

How can I design my data classes using VB.NET to take this into account.
The intrinsic datatypes in VB (integer, long, string, etc) can not have null
values.

What is common practice in this case?

Jul 21 '05 #13

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

Similar topics

8
by: Neil Zanella | last post by:
Hello, I am just wondering, what is the current best common practice for online surveys: 1. allowing users to vote any number of times 2. allowing one vote per IP 3. requiring email address...
10
by: serge | last post by:
Using "SELECT * " is a bad practice even when using a VIEW instead of a table? I have some stored procedures that are identical with the difference of one statement in the WHERE clause. If I...
6
by: Modest Marsupial | last post by:
What is the DAO method of allowing a recordset to have null values? Thanks, marie
7
by: jsale | last post by:
I'm currently using ASP.NET with VS2003 and SQL Server 2003. The ASP.NET app i have made is running on IIS v6 and consists of a number of pages that allow the user to read information from the...
14
by: D Witherspoon | last post by:
What is the accepted method of creating a data class or business rules object class with properties that will allow the returning of null values? For example... I have a class named CResults with...
5
by: Chad | last post by:
I want to create a class which contains a date, integer and string property which all allow a NULL value. I don't want to store the values in an Object datatype. I prefer more strongly typed...
7
by: Steve | last post by:
I am building an object library for tables in a database. What is the best practice for creating objects like this? For example, say I have the following tables in my database: User: - Id -...
10
by: =?Utf-8?B?R3JlZw==?= | last post by:
I have the following three files. 1. Users.aspx is a webpage that uses the <asp:ObjectDataSourcecontrol to populate a simple <asp:ListBoxcontrol. 2. The UserDetails.cs file creates a Namespace...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.