Why are your parameters of type object in the first place? Why isn't it:
public int Update(int serial, DateTime? deliveryDate,
double? quantity, short? shiftTime)
(Note that decimal
may be a better choice than double
- 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) and short
(or null) then you can just cast them to their nullable equivalents. That will unbox null
to the null value of the type, and non-null values to corresponding non-null values:
object x = 10;
int? y = (int?) x; // y = non-null value 10
x = null;
y = (int?) x; // y is null value of the Nullable<int> type
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:
short? shiftTime = null;
string userInput = shiftTimeInput.Text;
if (userInput.Length > 0) // User has put *something* in
{
short value;
if (short.TryParse(userInput, out value))
{
shiftTime = value;
}
else
{
// Do whatever you need to in order to handle invalid
// input.
}
}
// Now shiftTime is either null if the user left it blank, or the right value
// Call the Update method passing in shiftTime.