473,748 Members | 2,398 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2614
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.MinVal ue 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****@TheWorl d.com> wrote in message
news:#V******** ******@TK2MSFTN GP11.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.Valu e.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****@opensho pinc.com> wrote in message
news:#m******** ******@TK2MSFTN GP09.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.MinVal ue 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****@TheWorl d.com> wrote in message
news:#V******** ******@TK2MSFTN GP11.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.Valu e.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****@TheWorl d.com> wrote in message
news:ux******** *****@tk2msftng p13.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****@opensho pinc.com> wrote in message
news:#m******** ******@TK2MSFTN GP09.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.MinVal ue 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****@TheWorl d.com> wrote in message
news:#V******** ******@TK2MSFTN GP11.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.Sql Types namespace)

--
mikeb

Nov 15 '05 #7
I would suggest to use System.Data.Sql Types 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
(NullableDateTi me 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 NullConvertClas s 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.Sq lTypes namespace)


Nov 15 '05 #8

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

Similar topics

2
1567
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
5153
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 sortBy, bool asc) {
3
3451
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 enum as a value? as a field named Priority: public MailPriority Priority; //doesn't work So that I can set it from the Email instance like this:
1
3355
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 developed but instead use properties. /erik
2
2178
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 As String Public Property Name() As String Get
2
1287
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
3970
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 is instance of MyEntity class. There is no MyProperty property in MyObject at design time.
2
1312
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 provided by the client along the lines of: if ( ! IsInRole(accountName, roleName) ) {
14
2982
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 PyCObject_FromVoidPtr the code that creates the PyCObject is as follows: tmp = PyCObject_FromVoidPtr (info.info.x11.display, NULL); PyDict_SetItemString (dict, "display", tmp); Py_DECREF (tmp);
0
8991
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9372
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9324
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
8243
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6074
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
4606
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...
1
3313
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
3
2215
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.