Results 1 to 3 of 3

Thread: [RESOLVED] Passing Nulls

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Dunmow,Essex,England
    Posts
    898

    Resolved [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.

  2. #2
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    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?'

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Dunmow,Essex,England
    Posts
    898

    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
  •  



Click Here to Expand Forum to Full Width