|
-
Feb 7th, 2011, 05:55 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] Passing Nulls
Hi ALL,
I'm using VS2010 C# 4.0.
I have a method declared:
Code:
public spGetBorderauxLog(String Client, String ProcessDate, Int16? Status, Boolean? Actioned)
and am attempting to call with the following:
Code:
...spGetBorderauxLog(drpClient.Text, datepicker.Value == "" ? null : datepicker.Value, drpStatus.SelectedItem.Value == "" ? null : Convert.ToInt16(drpStatus.SelectedItem.Value), null);
I'm receiving the red line under the following text:
Code:
null : Convert.ToInt16(drpStatus.SelectedItem.Value)
Stating Type of conditional expression cannot be determined because there is no implicit conversion between '<null>' and 'short'
I've declared the parameter as nullable, so I cannot see the problem.
-
Feb 7th, 2011, 05:19 PM
#2
Re: [RESOLVED] Passing Nulls
Wait, is this resolved?
The type of the parameter that the expression is heading for isn't considered in resolving the type of the expression itself.
The type of a ternary expression is derived from the types of the two return values. The first (null) is of type void (well, that's not exactly a type, but you know what I mean, right?) and the second (Convert.ToInt16(drpStatus.SelectedItem.Value)) is of type int.
int and null are not comparable, therefore that expression is not legal. You need to give the compiler a hint, by casting the null to an 'int?'.
That way you're comparing an 'int?' to an int, which have a common type of 'int?'
-
Feb 8th, 2011, 04:58 AM
#3
Thread Starter
Fanatic Member
Re: [RESOLVED] Passing Nulls
Hi,
Although I didn't post my resolution, I needed to cast with Null.
(Int16?)null
So My complete test became.
Code:
drpStatus.SelectedItem.Value == "" ? (Int16?)null : Convert.ToInt16(drpStatus.SelectedItem.Value)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|