473,406 Members | 2,439 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,406 software developers and data experts.

Hoiw to expose a property that can be null.

MP
Hello,
I am trying to write a class that will expose some properties. One of the
property is extracted from a SQL database and can be NULL or some integer
value.

Is there a elegant way of implementing this in C# ? I do not want to use
variant (or similar types) because I want to retain a strnong types
property. Will I have no choices but to add another property that would
set/get the NULL value?

Thank you!

Nov 15 '05 #1
7 2592
MP,

First off I would say don't allow null values , for example
integers can be defaulted to 0 or 1... But, there are
always those rare cases and of course the famous DateTime
which can be null in the db but NOT in C# so you will need
to provide a low level routine that converts values in
and out. For example, DateTime null in the db can be
converted to DateTime.MinValue in C# and then back
to null at storage time. We have elimited all nulls from
our database except for datetime and foriegn keys.
We then have a DAL tool that does the conversion
for us.

JIM

"MP" <No****@TheWorld.com> wrote in message
news:#V**************@TK2MSFTNGP11.phx.gbl...
Hello,
I am trying to write a class that will expose some properties. One of the property is extracted from a SQL database and can be NULL or some integer
value.

Is there a elegant way of implementing this in C# ? I do not want to use variant (or similar types) because I want to retain a strnong types
property. Will I have no choices but to add another property that would
set/get the NULL value?

Thank you!

Nov 15 '05 #2

I know what you want to do, and I've battled with it in
the past, but understand there is no perfect answer here.
Think about it; you want strong typing but you're
breaching the concept of type "int" by adding in the
concept of DBNull. A true type "int" can be many things
but it can't be DBNull.

Anyway I've used both of these approaches before:

- If you implement the property as type DataColumn you
can 'set' the property to DBNull at will. However you
must ensure that a 'set' is setting the column to an int
value and, even uglier, the user takes on the
responsibility of calling MyProperty.Value.AsInt() each
time they wish to access the property. In this approach
you gain DBNull but lose compiler enforced "int-ness".

- You could also implement the property internally as a
DataColumn field but expose the property to the world as
an int; you the implementor handle type casting. The user
would see, aka 'get' and 'set' an int property. Under
this scheme you could initialize the property to DBNull at
startup. If the user never sets the property the internal
value of the property will be DBNull. If the user sets
the property it takes on the integer value. If the
user 'gets' the property they get an integer. If the
property is DBNull during a 'get' then you can throw a
DBNull exception or return 0 {or whatever}. The only
problem with this scheme is that once the property is set
to a non-null value then the user cannot reset it to
DBNull without a helper 'reset' method or a special
integer value that signals the property implementer to set
the value to DBNull...

--Richard
-----Original Message-----
Hello,
I am trying to write a class that will expose some properties. One of theproperty is extracted from a SQL database and can be NULL or some integervalue.

Is there a elegant way of implementing this in C# ? I do not want to usevariant (or similar types) because I want to retain a strnong typesproperty. Will I have no choices but to add another property that wouldset/get the NULL value?

Thank you!

.

Nov 15 '05 #3
MP
Thank you Jim,
I fully agree that mapping DbNULL to the real world does not make sense.
In some previous projects I moved away from DbNULLs for the very same
reason.... But on this one NULL's will stay ! <--- There is a joke in
there somewhere.
Thank you again.

"james" <jk****@openshopinc.com> wrote in message
news:#m**************@TK2MSFTNGP09.phx.gbl...
MP,

First off I would say don't allow null values , for example
integers can be defaulted to 0 or 1... But, there are
always those rare cases and of course the famous DateTime
which can be null in the db but NOT in C# so you will need
to provide a low level routine that converts values in
and out. For example, DateTime null in the db can be
converted to DateTime.MinValue in C# and then back
to null at storage time. We have elimited all nulls from
our database except for datetime and foriegn keys.
We then have a DAL tool that does the conversion
for us.

JIM

"MP" <No****@TheWorld.com> wrote in message
news:#V**************@TK2MSFTNGP11.phx.gbl...
Hello,
I am trying to write a class that will expose some properties. One of

the
property is extracted from a SQL database and can be NULL or some integer value.

Is there a elegant way of implementing this in C# ? I do not want to

use
variant (or similar types) because I want to retain a strnong types
property. Will I have no choices but to add another property that would
set/get the NULL value?

Thank you!


Nov 15 '05 #4
MP
Thank you Richard!

I appreciate your input.

"Richard" <ri******@amgen.com> wrote in message
news:07****************************@phx.gbl...

I know what you want to do, and I've battled with it in
the past, but understand there is no perfect answer here.
Think about it; you want strong typing but you're
breaching the concept of type "int" by adding in the
concept of DBNull. A true type "int" can be many things
but it can't be DBNull.

Anyway I've used both of these approaches before:

- If you implement the property as type DataColumn you
can 'set' the property to DBNull at will. However you
must ensure that a 'set' is setting the column to an int
value and, even uglier, the user takes on the
responsibility of calling MyProperty.Value.AsInt() each
time they wish to access the property. In this approach
you gain DBNull but lose compiler enforced "int-ness".

- You could also implement the property internally as a
DataColumn field but expose the property to the world as
an int; you the implementor handle type casting. The user
would see, aka 'get' and 'set' an int property. Under
this scheme you could initialize the property to DBNull at
startup. If the user never sets the property the internal
value of the property will be DBNull. If the user sets
the property it takes on the integer value. If the
user 'gets' the property they get an integer. If the
property is DBNull during a 'get' then you can throw a
DBNull exception or return 0 {or whatever}. The only
problem with this scheme is that once the property is set
to a non-null value then the user cannot reset it to
DBNull without a helper 'reset' method or a special
integer value that signals the property implementer to set
the value to DBNull...

--Richard
-----Original Message-----
Hello,
I am trying to write a class that will expose some

properties. One of the
property is extracted from a SQL database and can be NULL

or some integer
value.

Is there a elegant way of implementing this in C# ? I

do not want to use
variant (or similar types) because I want to retain a

strnong types
property. Will I have no choices but to add another

property that would
set/get the NULL value?

Thank you!

.

Nov 15 '05 #5
Ther is a tools that can solve this for you. Check out
www.thona-consulting.com

They have a DAL tool that will handle the bi-directional
conversion of nulls to the db

JIM
"MP" <No****@TheWorld.com> wrote in message
news:ux*************@tk2msftngp13.phx.gbl...
Thank you Jim,
I fully agree that mapping DbNULL to the real world does not make sense. In some previous projects I moved away from DbNULLs for the very same
reason.... But on this one NULL's will stay ! <--- There is a joke in
there somewhere.
Thank you again.

"james" <jk****@openshopinc.com> wrote in message
news:#m**************@TK2MSFTNGP09.phx.gbl...
MP,

First off I would say don't allow null values , for example
integers can be defaulted to 0 or 1... But, there are
always those rare cases and of course the famous DateTime
which can be null in the db but NOT in C# so you will need
to provide a low level routine that converts values in
and out. For example, DateTime null in the db can be
converted to DateTime.MinValue in C# and then back
to null at storage time. We have elimited all nulls from
our database except for datetime and foriegn keys.
We then have a DAL tool that does the conversion
for us.

JIM

"MP" <No****@TheWorld.com> wrote in message
news:#V**************@TK2MSFTNGP11.phx.gbl...
Hello,
I am trying to write a class that will expose some properties. One of
the
property is extracted from a SQL database and can be NULL or some integer value.

Is there a elegant way of implementing this in C# ? I do not want
to use
variant (or similar types) because I want to retain a strnong types
property. Will I have no choices but to add another property that

would set/get the NULL value?

Thank you!



Nov 15 '05 #6
MP wrote:
Hello,
I am trying to write a class that will expose some properties. One of the
property is extracted from a SQL database and can be NULL or some integer
value.

Is there a elegant way of implementing this in C# ? I do not want to use
variant (or similar types) because I want to retain a strnong types
property. Will I have no choices but to add another property that would
set/get the NULL value?

Thank you!


Look at the SqlInt32 data type (or other types in the
System.Data.SqlTypes namespace)

--
mikeb

Nov 15 '05 #7
I would suggest to use System.Data.SqlTypes in the Data
Layer, I mean where you are moving data from the Db (with
a DataAdapter or with a DataCommand) to the memory
(DataSet, variables, Array, etc.) and vice versa.
That's because SqlTypes better represent the native Db
types (i.e. SqlDateTime have MinValue and MaxValue
different from System.DateTime; SqlDecimal can set Scale
and Precision while System.Decimal cannot, etc).

I would suggest to use NullableTypes in the Business Layer
and in the User Interface Layer because
- they are identical to the native .NET types
(NullableDateTime have MinValue and MaxValue
equals to System.DateTime MinValue and MaxValue,
etc.)
- they work with .NET Remoting
- they work with .NET Web Services
- they are data base agnostic
- they have the NullConvertClass that can be
used to seamlessly integrate NullableTypes
with Web server controls and WinForms controls
- they have the DbNullCOnvert class that converts
NullableTypes values to in-memory Db values
(Command Parameters, DataReader values, DataSet
column values) and vice versa

ciao, (luKa)
-----Original Message-----

Look at the SqlInt32 data type (or other types in the
System.Data.SqlTypes namespace)


Nov 15 '05 #8

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

Similar topics

2
by: maxim | last post by:
Hi, I have a Class in C# that exposes property from System.Uri type. I want to access this property from classic Visual Basic. public class TestClass {
3
by: MattC | last post by:
Hi, I found this code somewhere on the net and need to make some alterations. public class GenericSorter : IComparer { String sortProperty; bool sortOrder; public GenericSorter(String...
3
by: Ant | last post by:
Hi, Im wrapping a MailMessage object & need to expose the Priority property. This property is set to a value of the 'MailPriority' enum. My question is, how do I create a member that accepts this...
1
by: erik | last post by:
Hi! Is it possible to expose customer properties used in a class in a web servies. Can I use on a property or how do I do? The thing is that I do not want to use parameters for the method I have...
2
by: GoCoogs | last post by:
I'm trying to count how many items are in a dynamic collection. This is the code I have so far. *** Begin Code *** Public Class Rule Private _rulevars As RuleVarsCollection Private _rulename...
2
by: rodchar | last post by:
hey all, i've exposed the .Text property of a TextBox on my control page. Is there anyway to make this show up in the QuickMenu -EditBindings dialog? thanks, rodchar
11
by: Andrus | last post by:
I'm implementing entity object which should populate its properties from database when property is first referenced. In RDL reports I use object properties like MyObject.MyProperty MyObject...
2
by: Brett Wiltshire | last post by:
Hi, I have subclassed System.Web.UI.Page. (Well done me). Pages in my project should be accessed based on a user's membership to a Role. Membership to a Role is determined by a small API...
14
by: Gordon Allott | last post by:
Hello :) The result of various incompatibilities has left me needing to somehow extract the address that a null pointer is pointing to with the null pointer being exposed to python via...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
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,...
0
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...

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.