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: Building a conditional expression in Access - microsoft.public ...I am trying to build a conditional expression in an Access query that will tell me how many weeks of vacation an employee has based on years of servi... Macro - "Where Condition" Expression - microsoft.public ...I'm sure this has a completely obvious answer... I'm using an "Open Form" macro that uses the expression "1=0" as the "Where Condition"... What does 1... expression for contains - microsoft.public.access.reports ...When doing a conditional formatting in a report and you want to type the expression contains "USA" in data for the field of [Country]. What expre... Conditional formatting based on matching record in separate query ...My question is, somehow I need my conditional expression to say "If Field1 matches any record's Field1 in this other query (Admissions), then make Field1 bold in my ... conditional formating if field is blank - microsoft.public.access ...Click on Format + Conditional Format. Set Condition1 to Expression Is write in the next box [FieldName] Is Null Select the color you wish Save teh changes -- Fred Please ... change captions with conditional formatting - microsoft.public ...My conditional formatting for the colours are, Expression Is: [Stock_Available]="Yes" Green Expression Is: [Stock_Available]="Part" Yellow Expression Is ... Conditional format duplicates - microsoft.public.access ...... This value will be the count of matches (1 if this is the unique record for the case). You could use a condition on this field in the Conditional Format expression. conditional formating in Access - microsoft.public.access.forms ...Tips for conditional formatting in Access - Access - Office.com Applies to Microsoft Access 2000 Conditional formatting with controls that contain expressions You might ... Building iif expression - microsoft.public.access.queries ...Building a conditional expression in Access - microsoft.public ... Thanks "Ofer Cohen" wrote: > I assume you have a field that return the number of years, if that the case ... Conditional Printing of Certain Fields in A Report - microsoft ...Conditional Printing of Certain Fields in A Report - microsoft ... i need to have a conditional printing of some fields ... Building a conditional expression in Access ... Conditional statement - Wikipedia, the free encyclopediaA conditional statement may refer to: In logic and mathematics: Material conditional, a propositional binary connective Strict conditional, a formal way of expressing ... Conditional (programming) - Wikipedia, the free encyclopediaIn computer science, conditional statements, conditional expressions and conditional constructs are features of a programming language which perform different ... Conditional-Expression OperatorC has one ternary operator: the conditional-expression operator (? :). If-Then-Else Conditionals in Regular ExpressionsIn Perl's regex flavor, and compatible flavors, you can use a special construct to create conditional if-then-else regular expressions. CONDITIONAL EXPRESSIONS - The VB Programmer - HomeWhen the conditional expression involves a character string compare, you can think of the relational operators as performing a comparison based on "alphabetical order". 7/17/2012 12:15:49 AM
|