Decimal numbers with possible null values?

  • Thread starter Thread starter Gustaf Liljegren
  • Start date Start date
G

Gustaf Liljegren

I searched for previous answers on this, but couldn't find something
fitting. I need advice on how to store decimal numbers with possible
null values in memory. The numbers may be negative, so storing null
values as -1 doesn't work. The numbers are amounts of money, so
decimal is the best datatype, except that it can't store null values.

The best idea so far is to make an object of each number and have it
store either a decimal or null. It's not pretty, but I think it would
work. Can anyone find something more straightforward?

Thanks,

Gustaf
 
I suppose you could use a string, then have some conversion to decimal after
you've confirmed the string is not null. You then also have the case where
the value is null.
 
Gustaf,

If you can use the beta of .NET 2.0, then I would look into using the
Nullable generic type. If you can not use that, then I would recommend
using the SqlDecimal class in the System.Data.SqlTypes namespace. It will
have pretty much what you want (IsNull or HasValue properties), and you
should be able to swap them out with little problems when you move to .NET
2.0.

Hope this helps.
 
Gustaf Liljegren said:
I searched for previous answers on this, but couldn't find something
fitting. I need advice on how to store decimal numbers with possible
null values in memory. The numbers may be negative, so storing null
values as -1 doesn't work. The numbers are amounts of money, so
decimal is the best datatype, except that it can't store null values.

The best idea so far is to make an object of each number and have it
store either a decimal or null. It's not pretty, but I think it would
work. Can anyone find something more straightforward?

Along with the other options presented by other posters, have you
considered using a "special value" which will never actually come up?
For instance, Decimal.MaxValue? If any *real* data ever came close to
that, you'd have to worry about overflow anyway.
 
Back
Top