Comparison question

  • Thread starter Thread starter tshad
  • Start date Start date
The Crow said:
you sure right about what properties are. but you should use (in my
opinion) decimal as data type of your property. if you will use object as
datatype, then dont bother with DBNull. just assign dbnull directly. my
intention using Decimal.MaxValue was because decimal type cant hold null
values...

But if I do that I won't be able to use the property directly
(myObject.Salary = mySalary.Text). Because mySalary.Text happens to be
blank, I end up with an error (as you can't assign a blank text field to a
decimal field).
you shouldnt assign a string to salary as database persistence will be a
disaster because your column type is decimal (or probably money in sql
server.).

I am not assigning a string to salary but converting the string to decimal
in the property methods.
have you inspected my decimalpicker example?

Yes, I assume you are talking about your nullHandler example..

That was where I got the idea of doing my class.

In your nullHandler, you pass an Object to the GetValueFromDbObject methods.
I call this from my Set/method and the overload is controlled by the
datatype of the private variable (salary).

Your handler works fine as long as you are not passing an empty textbox,
which was why I had to modify it. I wanted to handle the data regardless of
whether a textbox was being passed, decimal variable or decimal constant. I
also wanted to be able let the class handle conversions.

I didn't want to have to remember to use .ToString or .ToDecimal in my
code - I wanted the class to handle that.

My problem is that I needed to use 2 values MaxValue and MaxValue-1 to
handle the DBNull value as well as the Empty string value. If it came in as
an Empty string I would use that in my Get also (as a DBNull would cause an
error, if I was returning that to a textbox).

This may not work as cleanly as I would like as I cannot get the Get to work
correctly. This is because I have no way of knowing whether I am returning
the value to a Textbox or a decimal variable. I can't return an empty
string to a variable and I can't return a DBNull.Value to a textbox).

Tom
 
i suggested you using custom web user control that returns you decimal value
instead of string. but if you really want to this approach use something
like :
public object Salary
{
get{return salary;}
set
{
if(value == null || value == DBNull.Value) //again we take advantage
of DBNull.Value returns same instance in scope of the application
salary = DBNull.Value;
else if(typeof(value) == typeof(string)
{
if(value.ToString() == String.Empty)
salary = DBNull.Value;
else
salary = Decimal.Parse(value.ToString()); // you can use try
catch block here and throw your own exception rather then CLR's to be thrown
when string is not a correct string...
}
else if(typeof(value) == typeof(decimal)
salary = value;
else
throw new ApplicationException("Salary must be assigned to
either decimal or string value");
}
}


i think this approach suffers from performance a bit and user of the class
have to know that he/she can assign only decimal and string values to the
property. but it also leaves far less job to UI (parsing and checking for
null or DbNull or empty strings.). if you will use this approach and dont
want write this code everytime you can encapsulate that code in a static
method in your project and use everywhere.

public sealed class ValueSetterHelper // we use sealed to gain performance
and prevent inheriting from this class.
{
private ValueSetterHelper() // we use private constructor to prevent
instaninating this class.
{
}

public static object HandleDecimalValue(object value)
{
if(value == null || value == DBNull.Value) //again we take advantage
of DBNull.Value returns same instance in scope of the application
return DBNull.Value;
else if(typeof(value) == typeof(string)
{
if(value.ToString() == String.Empty)
return DBNull.Value;
else
return Decimal.Parse(value.ToString()); // you can use try
catch block here and throw your own exception rather then CLR's to be thrown
when string is not a correct string...
}
else if(typeof(value) == typeof(decimal)
return value;
else
throw new ApplicationException("Salary must be assigned to
either decimal or string value");
}
}

public object Salary
{
get{return salary;}
set{salary = ValueSetterHelper.HandleDecimalValue(value);
}


and i want to ask readers of this post, how is this approach?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

arithmetic overflow 3
Class method doesn't seem to work 2
String and null vs DBNULL 7
delegate usage? 7
Casting in a generic function 3
how to save my class in settings 1
Override the property 5
trinary statement 14

Back
Top