PC Review


Reply
Thread Tools Rate Thread

Common practice, allowing null values in data classes.

 
 
D Witherspoon
Guest
Posts: n/a
 
      3rd Mar 2005
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?


 
Reply With Quote
 
 
 
 
=?Utf-8?B?QWxpZW4yXzUx?=
Guest
Posts: n/a
 
      3rd Mar 2005
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?
>
>
>

 
Reply With Quote
 
Rodger Constandse
Guest
Posts: n/a
 
      3rd Mar 2005
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?
>
>

 
Reply With Quote
 
Nick Malik [Microsoft]
Guest
Posts: n/a
 
      4th Mar 2005
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" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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?
>
>



 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      4th Mar 2005
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


 
Reply With Quote
 
D Witherspoon
Guest
Posts: n/a
 
      4th Mar 2005
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]" <(E-Mail Removed)> wrote in message
news:4NOdnaU_GZYMYLrfRVn-(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>> 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?
>>
>>

>
>



 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      4th Mar 2005
D,

I am curious why not my answer?

Cor



 
Reply With Quote
 
D Witherspoon
Guest
Posts: n/a
 
      4th Mar 2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> D,
>
> I am curious why not my answer?
>
> Cor
>
>
>



 
Reply With Quote
 
Nick Malik [Microsoft]
Guest
Posts: n/a
 
      4th Mar 2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> D,
>
> I am curious why not my answer?
>
> Cor
>
>
>



 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      5th Mar 2005
Nick,

>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.


However not as inefficient as the now chosen method.

It seems that boxing is often a big hit by a lot of people, and I don't know
why.

Boxing and unboxing is a standard (ILS) procedure. When this kind of things
become important than you should start with avoiding properties and use
direct the values again, with that you will probably gain much more
performance than avoiding boxing and unboxing. (Not that I shall advice
that).

(By the way have a look at the datarow, in my opinion will you you will see
there the same approach)

Just my thought,

Cor


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
comparing two columns of data to find common values patman Microsoft Excel Misc 2 25th Jul 2006 03:05 PM
HELP! Best practice for lookup data and foreign key values? James E Microsoft Dot NET 1 7th Sep 2005 09:07 PM
HELP! Best practice for lookup data and foreign key values? James E Microsoft Dot NET 0 7th Sep 2005 12:35 PM
Common practice, allowing null values in data classes. D Witherspoon Microsoft Dot NET Framework Forms 14 10th Mar 2005 08:13 AM
Common practice, allowing null values in data classes. D Witherspoon Microsoft VB .NET 14 10th Mar 2005 08:13 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:45 PM.