Using c#, How can I take benefit of using nullable value types when I have a variable of object type?
For example, I have a method Insert within a class which takes 4 arguments:
public int Update(Int32 serial, object deliveryDate, object quantity, object shiftTime)
{
....
....
....
}
As you can guess, this method inserts a new record in a table. The table (Table1), has 4 colums: Serial int, DeliveryDate DateTime null, Quantity float not null and ShiftTime smallint null
Now, my quastion is : how can I take benefit of using nullable valuetypes and also ho can I convert object to my desirable types like DateTime?
Thank you
Why are your parameters of type object in the first place? Why isn’t it:
(Note that
decimalmay be a better choice thandouble– something to think about.)If you can possibly change your method signature, that’s probably the way to go.
Otherwise, what are you really asking in your question? If you can’t change the parameters, but the arguments should be of type
DateTime(or null),double(or null) andshort(or null) then you can just cast them to their nullable equivalents. That will unboxnullto the null value of the type, and non-null values to corresponding non-null values:EDIT: To respond to the comment…
Suppose you have a text box for the shift time. There are three options: it’s filled in but inappropriately (e.g. “foo”), it’s empty, or it’s valid. You’d do something like this: