Conditional Expression

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

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

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
 
shapper said:
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.
 
shapper said:
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.)
 
Back
Top