Conditional Expression

  • Follow


Hello,

On a method I have the following:

  if (slide == null)
    return File("~/resources/image/defaultslide.jpg", "image/jpg");
  else
    return File(slide.Image, "image/jpg");

But if I use:

  return slide == null ? File("~/resources/image/defaultslide.jpg",
"image/jpg") : File(slide.Image, "image/jpg");

I get the error:

  Type of conditional expression cannot be determined because there is
no implicit conversion between 'System.Web.Mvc.FilePathResult' and
'System.Web.Mvc.FileContentResult'

File is described here:
http://msdn.microsoft.com/en-us/library/system.web.mvc.fileresult.aspx

Please see the Inheritance Hierarchy:

System.Object
  System.Web.Mvc.ActionResult
    System.Web.Mvc.FileResult
      System.Web.Mvc.FileContentResult
      System.Web.Mvc.FilePathResult
      System.Web.Mvc.FileStreamResult

Shouldn't my code work?

Thanks,
Miguel
0
Reply shapper 4/22/2010 10:25:12 PM

On 4/22/2010 6:25 PM, shapper wrote:
> Hello,
>
> On a method I have the following:
>
>    if (slide == null)
>      return File("~/resources/image/defaultslide.jpg", "image/jpg");
>    else
>      return File(slide.Image, "image/jpg");
>
> But if I use:
>
>    return slide == null ? File("~/resources/image/defaultslide.jpg",
> "image/jpg") : File(slide.Image, "image/jpg");
>
> I get the error:
>
>    Type of conditional expression cannot be determined because there is
> no implicit conversion between 'System.Web.Mvc.FilePathResult' and
> 'System.Web.Mvc.FileContentResult'
>
> File is described here:
> http://msdn.microsoft.com/en-us/library/system.web.mvc.fileresult.aspx
>
> Please see the Inheritance Hierarchy:
>
> System.Object
>    System.Web.Mvc.ActionResult
>      System.Web.Mvc.FileResult
>        System.Web.Mvc.FileContentResult
>        System.Web.Mvc.FilePathResult
>        System.Web.Mvc.FileStreamResult
>
> Shouldn't my code work?
>
> Thanks,
> Miguel

Are "slide", your function File(), and this function, defined as the 
same types?  It looks like your function might be defined as type 
System.Web.Mvc.FileResult, but slide is a more specific type, and File 
returns the more general type.

-- 
Mike
0
Reply Family 4/23/2010 12:08:10 AM


On Apr 23, 1:08=A0am, Family Tree Mike <FamilyTreeM...@ThisOldHouse.com>
wrote:
> On 4/22/2010 6:25 PM, shapper wrote:
>
>
>
> > Hello,
>
> > On a method I have the following:
>
> > =A0 =A0if (slide =3D=3D null)
> > =A0 =A0 =A0return File("~/resources/image/defaultslide.jpg", "image/jpg=
");
> > =A0 =A0else
> > =A0 =A0 =A0return File(slide.Image, "image/jpg");
>
> > But if I use:
>
> > =A0 =A0return slide =3D=3D null ? File("~/resources/image/defaultslide.=
jpg",
> > "image/jpg") : File(slide.Image, "image/jpg");
>
> > I get the error:
>
> > =A0 =A0Type of conditional expression cannot be determined because ther=
e is
> > no implicit conversion between 'System.Web.Mvc.FilePathResult' and
> > 'System.Web.Mvc.FileContentResult'
>
> > File is described here:
> >http://msdn.microsoft.com/en-us/library/system.web.mvc.fileresult.aspx
>
> > Please see the Inheritance Hierarchy:
>
> > System.Object
> > =A0 =A0System.Web.Mvc.ActionResult
> > =A0 =A0 =A0System.Web.Mvc.FileResult
> > =A0 =A0 =A0 =A0System.Web.Mvc.FileContentResult
> > =A0 =A0 =A0 =A0System.Web.Mvc.FilePathResult
> > =A0 =A0 =A0 =A0System.Web.Mvc.FileStreamResult
>
> > Shouldn't my code work?
>
> > Thanks,
> > Miguel
>
> Are "slide", your function File(), and this function, defined as the
> same types? =A0It looks like your function might be defined as type
> System.Web.Mvc.FileResult, but slide is a more specific type, and File
> returns the more general type.
>
> --
> Mike

Slide is just an object that I take from the repository:

    public virtual ActionResult Get() {

      Slide slide =3D _slideRepository.FindRandomly();
      if (slide =3D=3D null)
        return File("~/resources/image/defaultslide.jpg", "image/
jpg");
      else
        return File(slide.Image, "image/jpg");

    } // Get

This works. If I replace the if's with conditional ?: then it doesn't.
This seems strange to me.

Thanks,
Miguel
0
Reply shapper 4/23/2010 1:19:27 AM

shapper wrote:
> Hello,
> 
> On a method I have the following:
> 
>   if (slide == null)
>     return File("~/resources/image/defaultslide.jpg", "image/jpg");
>   else
>     return File(slide.Image, "image/jpg");
> 
> But if I use:
> 
>   return slide == null ? File("~/resources/image/defaultslide.jpg",
> "image/jpg") : File(slide.Image, "image/jpg");
> 
> I get the error:
> 
>   Type of conditional expression cannot be determined because there is
> no implicit conversion between 'System.Web.Mvc.FilePathResult' and
> 'System.Web.Mvc.FileContentResult'
> 
> File is described here:
> http://msdn.microsoft.com/en-us/library/system.web.mvc.fileresult.aspx

Nothing called File is described there.

> Please see the Inheritance Hierarchy:
> 
> System.Object
>   System.Web.Mvc.ActionResult
>     System.Web.Mvc.FileResult
>       System.Web.Mvc.FileContentResult
>       System.Web.Mvc.FilePathResult
>       System.Web.Mvc.FileStreamResult

So as the message says, there is no implicit conversion between 
FilePathResult and FileContentResult. There can't be. Neither is derived 
from the other.

Unlike the if ... else ... construction, the conditional operator, as in

	Z = b ? X : Y

like all operators, is the basis for an expression that produces a 
*result*. The type of that result is determined at compile time, and 
therefore must be determinable then. Even though at runtime only one of 
the operands X and Y or the other will be evaluated and used as the 
value of the result of the operation, the type of that result is fixed 
at compile time, and both X and Y must be implicitly convertible to that 
type. Per Section 7.13 of the C# Language Specification,

�	If X and Y are the same type, then this is the type of the conditional 
expression.
�	Otherwise, if an implicit conversion (�6.1) exists from X to Y, but 
not from Y to X, then Y is the type of the conditional expression.
�	Otherwise, if an implicit conversion (�6.1) exists from Y to X, but 
not from X to Y, then X is the type of the conditional expression.
�	Otherwise, no expression type can be determined, and a compile-time 
error occurs.
0
Reply Harlan 4/23/2010 3:06:04 AM

shapper wrote:
> On Apr 23, 1:08 am, Family Tree Mike <FamilyTreeM...@ThisOldHouse.com>
> wrote:
>> On 4/22/2010 6:25 PM, shapper wrote:
>>
>>
>>
>>> Hello,
>>> On a method I have the following:
>>>    if (slide == null)
>>>      return File("~/resources/image/defaultslide.jpg", "image/jpg");
>>>    else
>>>      return File(slide.Image, "image/jpg");
>>> But if I use:
>>>    return slide == null ? File("~/resources/image/defaultslide.jpg",
>>> "image/jpg") : File(slide.Image, "image/jpg");
>>> I get the error:
>>>    Type of conditional expression cannot be determined because there is
>>> no implicit conversion between 'System.Web.Mvc.FilePathResult' and
>>> 'System.Web.Mvc.FileContentResult'
>>> File is described here:
>>> http://msdn.microsoft.com/en-us/library/system.web.mvc.fileresult.aspx
>>> Please see the Inheritance Hierarchy:
>>> System.Object
>>>    System.Web.Mvc.ActionResult
>>>      System.Web.Mvc.FileResult
>>>        System.Web.Mvc.FileContentResult
>>>        System.Web.Mvc.FilePathResult
>>>        System.Web.Mvc.FileStreamResult
>>> Shouldn't my code work?
>>> Thanks,
>>> Miguel
>> Are "slide", your function File(), and this function, defined as the
>> same types?  It looks like your function might be defined as type
>> System.Web.Mvc.FileResult, but slide is a more specific type, and File
>> returns the more general type.
>>
>> --
>> Mike
> 
> Slide is just an object that I take from the repository:
> 
>     public virtual ActionResult Get() {
> 
>       Slide slide = _slideRepository.FindRandomly();
>       if (slide == null)
>         return File("~/resources/image/defaultslide.jpg", "image/
> jpg");
>       else
>         return File(slide.Image, "image/jpg");
> 
>     } // Get
> 
> This works. If I replace the if's with conditional ?: then it doesn't.
> This seems strange to me.
> 
> Thanks,
> Miguel

The different overloads of the File methods returns different data 
types. The types can both be cast to ActionResult, but there is no 
conversion between the specific types.

When you just return the values, they are cast to ActionResult because 
the return statement takes the return type into account.

When you put the expressions in the conditional statement, it doesn't 
take the return type into account, as the conditional statement only 
considers the type of it's arguments, not what the surrounding code 
might expect.

To make the conditional statement work you need to cast one (or both) of 
the expressions to ActionResult:

return (slide == null) ?
   File("~/resources/image/defaultslide.jpg", "image/jpg")
:
   (ActionResult)File(slide.Image, "image/jpg");

(If there is some other type that is a common base class or interface 
for FilePathResult and FileContentResult, and is convertible to 
ActionResult, you could of course also use that as common type in the 
conditional statement.)

-- 
G�ran Andersson
_____
http://www.guffa.com
0
Reply ISO 4/26/2010 7:46:18 AM

4 Replies
184 Views

(page loaded in 0.189 seconds)

Similiar Articles:
















7/17/2012 12:15:49 AM


Reply: