jmease
jmease

Reputation: 2535

Accessing Method from Interface Throwing Conversion Error

public class NewCallInfo
{
    public string Site { get; set; }
    public string CustomerName { get; set; }
    public string CustomerPhone { get; set; }
    public string CustomerExt { get; set; }
    public string CustomerAddress { get; set; }
    public string CustomerCity { get; set; }
    public string CustomerState { get; set; }
    public string CustomerZip { get; set; }
    public string ServiceType { get; set; }
    public string Priority { get; set; }
    public string Description { get; set; }
    public string Technician { get; set; }
    public string Serial { get; set; }
    public string Model { get; set; }
}

public interface INewCall
{
    NewCallInfo GetNewCallInfo(string site);
}

public class HandleNewCall : INewCall
{
   private SqLiteHelper Helper {get;set;}

   public HandleNewCall(Context context)
   {
      Helper = new SqLiteHelper(context);
   }
   public NewCallInfo GetNewCallInfo(string site)
   {
      string whereClause = string.Format("Where Site='{0}'", site);

        ICursor callCursor = _helper.ReadableDatabase.Query("NewCall", null, whereClause, null, null, null, null);
        NewCallInfo newCall = new NewCallInfo();

        while(callCursor.MoveToNext())
        {
            newCall.Site                =       callCursor.GetString(0);
            newCall.CustomerName        =       callCursor.GetString(1);
            newCall.CustomerPhone       =       callCursor.GetString(2);
            newCall.CustomerExt         =       callCursor.GetString(3);
            newCall.CustomerAddress     =       callCursor.GetString(4);
            newCall.CustomerCity        =       callCursor.GetString(5);
            newCall.CustomerState       =       callCursor.GetString(6);
            newCall.CustomerZip         =       callCursor.GetString(7);
            newCall.ServiceType         =       callCursor.GetString(8);
            newCall.Priority            =       callCursor.GetString(9);
            newCall.Description         =       callCursor.GetString(10);
            newCall.Technician          =       callCursor.GetString(11);
            newCall.Serial              =       callCursor.GetString(12);
            newCall.Model               =       callCursor.GetString(13);
        }

        if (string.IsNullOrEmpty(newCall.Site))
            newCall.Site = "None";

        return newCall;
   }
}

class MyApp : Application
{
    public INewCall NewCall { get; set; }

    public MyApp(IntPtr handle, JniHandleOwnership transfer)
        :base(handle, transfer)
    {

    }

    public override void OnCreate()
    {
       NewCall = new HandleNewCall(this);
    }
}

Then in my activity when I do:

var call = ((MyApp) Application).NewCall.GetNewCallInfo("siteString");

I get "Cannot cast from source type to destination type". All of the above stuff with interfaces and application sub classes is extremely new to me, but I feel like I've done everything the same way as the examples online. Namely in this example. Been banging my head against the wall for hours. Anybody see anything glaring that I'm missing?

Upvotes: 0

Views: 67

Answers (1)

Greg Shackles
Greg Shackles

Reputation: 10139

You're missing the [Application] attribute on your MyApp class to properly generate AndroidManifest.xml, so Android doesn't know to use that as your application class. Because of that, trying to cast the Application property to MyApp is failing since the object isn't actually an instance of your class. Update your class definition to look like:

[Application]
public class MyApp : Application
{
    // ...
}

Upvotes: 1

Related Questions