Cannot convert source type to target type

I have this subclass implementing my interface, and there are no errors in terms of satisfying the contract. However, when I try to establish the current session in the constructor of the subclass, I get this compile-time error when I try to compare the type of the variable with the return type of GetCurrentSession ():

"Cannot convert the source type of IAPISession to the target type of FacebookSession"

Ok why? Facebook is an IAPISession ... right ??? Polymorphism in the game is my thinking, so he should be happy with this comparison. Not sure here.

public class FacebookSession : IAPISession
{
    private FacebookSession currentSession;

    private FacebookSession()
    {
        currentSession = GetCurrentSession();
    }

    ...more code

    public IAPISession GetCurrentSession()
    {
        // my logic is here...whatever that may be
    }
     ... more code
 }

Update

here is my actual interface:

public interface IAPISession
{
    #region Properties

    int SessionID { get; }

    string UserID { get; }

    bool SessionHasExpired { get; }

    DateTime ExpirationDate { get; }

    void LogOut(); // expires the session & sets SessionHasExpired

    #endregion Properties

    #region Methods

    IAPISession GetCurrentSession();

    #endregion Methods

}

This interface will be used in any API shell projects (e.g. FlickrSession, etc.).

+5
5

, . () , (Facebook) /.

, GetCurrentSession() IAPISession!

  • currentSession = (FacebookSession) GetCurrentSession();

  • try . , , FacebookSession, .

:

FacebookSession fbSess;
IAPISession     genSess;
FacebookSession getFbSession() { ... return this; }
IAPISession     getSession()   { ... return this; }

genSess = getSession();        // legal
genSess = getFbSession();      // legal - implicit cast works as FacebookSession 
                               // is always a kind of IAPISession
fbSess  = getFbSession();      // legal
fbSess  = getSession();        // ILLEGAL - not all IAPISession will be
                               // kinds of FacebookSession
fbSess  = (FacebookSession) getSession();
                               // legal, but might throw a class cast exception
                               // if it isn't a FacebookSession.

,

genSess = fbSess;              // ok, implicit cast to generic type
fbSess  = genSess;             // ILLEGAL, it may not be a FacebookSession
fbSess  = (FacebookSession) genSess; 
                               // legal but can throw an exception
+4

GetCurrentSession FacebookSession, IAPISession, , .

GetCurrentSession FacebookSession, currentSession IAPISession ( , ).

+3

:

public class FacebookSession : IAPISession
{
    private IAPISession currentSession;

    private FacebookSession()
    {
        currentSession = GetCurrentSession();
    }

    ...more code

    public FacebookSession GetCurrentSession()
    {
        // my logic is here...whatever that may be
    }
     ... more code
 }

, . GetCurrentSession. :

public class FacebookSession : IAPISession
{
    private FacebookSession currentSession;

    private FacebookSession()
    {
        currentSession = new FacebookSession();
    }

    ...more code

    public IAPISession GetCurrentSession()
    {
        return currentSession;
    }
     ... more code
 }

, , . . . . .NET Optimized Code

+1

# (IAPISession, ) (FacebookSession) , .

, :

var flickrSession = new FlickrSession();
var iApiFlickrSession = flickrSession as IAPISession;

var faceBookSession = iApiFlickrSession;

, FacebookSession IAPISession, , IAPISession FacebookSession.

, .

- as ( ). null, :

currentSession = GetCurrentSession() as FacebookSession;
0

, :

private FacebookSession() 
{ 
    currentSession = (FacebookSession) GetCurrentSession(); 
} 

, , GetCurrentSession(), FacebookSession.

:

, , , :

    currentSession = GetCurrentSession(); 

, , , GetCurrentSession, FacebookSession , , IAPISession, -FacebookSeesion, IAPISession.

    currentSession = (FacebookSession) GetCurrentSession(); 

: " - , FacebookSession. lyin ', !"

    currentSession = (int) GetCurrentSession(); 

. : " " blah-blah-blah, : "! , IAPISession int"

0

All Articles