|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Why is this invalidIDerived1 derived1 = GetDerived1();
IDerived2 derived2 = GetDerived2(); IBase ibase = (derived1 != null ? derived1 : derived2); Where "IDerived1" and "IDerived2" each inherit from "IBase". The followinge error results on the last line: Error 32 Type of conditional expression cannot be determined because there is no implicit conversion between 'Test.IDerived1' and 'Test.IDerived2' I understand the error but why does the language consider it a problem in this context. Thanks. No idea, but I'm curious as to whether this might work?
IBase ibase = (derived1 != null ? (IBase)derived1 : (IBase)derived2); Jon Show quoteHide quote "Larry Smith" <no_spam@_nospam.com> wrote in message news:%23QPfIm6dHHA.4636@TK2MSFTNGP03.phx.gbl... > IDerived1 derived1 = GetDerived1(); > IDerived2 derived2 = GetDerived2(); > IBase ibase = (derived1 != null ? derived1 : derived2); > > Where "IDerived1" and "IDerived2" each inherit from "IBase". The > followinge error results on the last line: > > Error 32 Type of conditional expression cannot be determined because there > is no implicit conversion between 'Test.IDerived1' and 'Test.IDerived2' > > I understand the error but why does the language consider it a problem in > this context. Thanks. > > No idea, but I'm curious as to whether this might work? It does but I don't see why it has to be explicit.> > IBase ibase = (derived1 != null ? (IBase)derived1 : (IBase)derived2); On Apr 5, 10:40 am, "Larry Smith" <no_spam@_nospam.com> wrote: Because C# determines expression types independently of where that> IDerived1 derived1 = GetDerived1(); > IDerived2 derived2 = GetDerived2(); > IBase ibase = (derived1 != null ? derived1 : derived2); > > Where "IDerived1" and "IDerived2" each inherit from "IBase". The followinge > error results on the last line: > > Error 32 Type of conditional expression cannot be determined because there > is no implicit conversion between 'Test.IDerived1' and 'Test.IDerived2' > > I understand the error but why does the language consider it a problem in > this context. Thanks. expression is ultimately to be assigned. In other words, the compiler sees this: IDerived1 derived1 = GetDerived1(); IDerived2 derived2 = GetDerived2(); (derived1 != null ? derived1 : derived2); and has no idea whether the result of the expression should be IDerived1 or IDerived2 (or something else). It hasn't noticed (yet) that you're going to assign the result to an IBase reference variable. It just knows that the two parts of the expression have two different types that have no available conversion. Jon's solution is the correct one, although you don't need to cast them both (assuming that IBase is the base interface for both IDerived1 and IDerived2). If you cast just one, the compiler will understand that it has to cast the other to the base type in order to make the expression work: derived1 != null ? (IBase)derived1 : derived2
Show quote
Hide quote
> Because C# determines expression types independently of where that Ok, thanks. I'm not a C# specialist but coming from the C++ world I see no > expression is ultimately to be assigned. > > In other words, the compiler sees this: > > IDerived1 derived1 = GetDerived1(); > IDerived2 derived2 = GetDerived2(); > (derived1 != null ? derived1 : derived2); > > and has no idea whether the result of the expression should be > IDerived1 or IDerived2 (or something else). It hasn't noticed (yet) > that you're going to assign the result to an IBase reference variable. > It just knows that the two parts of the expression have two different > types that have no available conversion. > > Jon's solution is the correct one, although you don't need to cast > them both (assuming that IBase is the base interface for both > IDerived1 and IDerived2). If you cast just one, the compiler will > understand that it has to cast the other to the base type in order to > make the expression work: > > derived1 != null ? (IBase)derived1 : derived2 (immediate) reason for this restriction. Bruce Wood <brucew***@canada.com> wrote:
<snip> > Because C# determines expression types independently of where that At least, mostly :) It's a noble ideal, but (unfortunately?) the types > expression is ultimately to be assigned. of anonymous methods and (in C# 3) lambda expressions depend on how they're used. Still, I'm glad it's mostly true :) -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too Larry Smith schreef:
Show quoteHide quote > IDerived1 derived1 = GetDerived1(); An alternative in C# 2.0 is:> IDerived2 derived2 = GetDerived2(); > IBase ibase = (derived1 != null ? derived1 : derived2); > > Where "IDerived1" and "IDerived2" each inherit from "IBase". The followinge > error results on the last line: > > Error 32 Type of conditional expression cannot be determined because there > is no implicit conversion between 'Test.IDerived1' and 'Test.IDerived2' > > I understand the error but why does the language consider it a problem in > this context. Thanks. > > IBase ibase = derived1 ?? derived2; Wiebe Tijsma <newsREM***@CAPITALStijsma.com> wrote:
> > Error 32 Type of conditional expression cannot be determined because there That has exactly the same issue as the original code. Try the > > is no implicit conversion between 'Test.IDerived1' and 'Test.IDerived2' > > > > I understand the error but why does the language consider it a problem in > > this context. Thanks. > > An alternative in C# 2.0 is: > > IBase ibase = derived1 ?? derived2; following: class Base {} class Derived1 : Base {} class Derived2 : Base {} class Test { static void Main() { Derived1 derived1 = null; Derived2 derived2 = null; Base foo = derived1 ?? derived2; } } The "fix" is to cast either derived1 or derived2 to Base: Base foo = derived1 ?? (Base)derived2; or Base foo = (Base)derived1 ?? derived2; -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too
Wrap command shell in System.Diagnostics.Process
Inter-process communication exchange 2003 create draft using webDAV Iterating over a list and altering it? RichText in asp.net IntelliSense Question. SOAP security negotiation error when using WCF (please help!) convert string to binary Windows Forms AppDomains - App within an App |
|||||||||||||||||||||||