I have a class that accepts String and Long datatypes.
Class Menu
Private FirstTier as String
Private SecondTier as Long
Sub New(ByVal FirstTier As String, _
ByVal SecondTier As Long)
....
Ok...great. this works fine but the problem arises as a result of filling
this from a datareader that reads from a stored procedure in a SQL database.
The database allows nulls for these columns, but I cannot add a null to the
class that is expecting a String or Long, right?
For my immediate problem, I can use IIf(IsDBNull(dr.Item("FirstTier"...
approach, and add a zero-length string if the DB returns a null, or a 0 for
the numeric datatype.
I poked around a saw a couple approaches to this:
- Don't allow nulls in the database (I don't have control over this, and
the stored procedures are already written..so I can't change them).
- I can do the above approach.
- I suppose I can change the String and Long to object datatypes, and
fill them with Nulls, right?
If I do the above approach, I am creating a business rule independent to the
database setup. Let's say I add a zero if there's a NULL for the long
datatype. That would work for my immediate problem, because there are no
zeroes in the database, but downstream, let's say the database does begin
using zeroes. My program would then not work.
My question is..how should I handle this? I don't see a "clean" way out
without making rules that might later contradict changes made to the
database, forcing the program to be rewritten. Ideally, I would like this
class to work similarly to the way the database works. A numeric datatype
constraint is applied to a column, but it can also store nothing. That
doesn't seem to transfer well to my numeric-expecting datatype in my class.
How do developers work around this issue?