Click here to Skip to main content
15,498,398 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,
I have 2 projects Namely Accounts and Info

One of the class in Accounts Project(Account after public is a column in a table)

using System.ComponentModel.Composition;
.......

C#
namespace Accounts
{
     [export(typeof(IAccount))]
      public class Account : IAccount
      {
             public Account GetAccountByEmail(string Ema)
             {
              ..........  
             }
       }
}


This is my interface(this is another project)

C#
namespace Interfaces
{
    public interface IAccount
    {
    }
}


this is my code in Info project

using System.Componentmodel.Composition;
using Interfaces;

public class AccountSer
{
   [Import]
   public IAccount _account;
   {
     public bool bhjyhf(string user)
     { 
          Account account = _account.GetAccountByEmail(user);
          if(account != null)
            return true;
          return false;
     }
    }
}


There is an error that IAccount has no extension method(GetAccountByEmail)
What is wrong should i type something in my interface.. or should i do something else??

please help me!!
When i Use
private Account _account ;
and proceed... with the rest of the code in the same way i get the required output

Please let me know what to do..

Thank you.
Posted
Comments
Sipherz 17-May-12 1:48am     CRLF
Thank you Guys!! But i found out another way Besides your posts were also equally helpfull Thanx

Your specific error is that you have to define a method in your IAccount interface so that you can use it:

C#
namespace Interfaces
{
    public interface IAccount
    {
        Account GetAccountByEmail(string Ema);
    }
}


That means that when you attempt to use the method when you have defined an object as IAccount, you can access the method.

You can also cast IAccount as Account to do the same.

acct = ((Account)iAcct).GetAccountByEmail(ema);
 
Share this answer
 
Comments
Maciej Los 16-May-12 15:52pm    
+5!
Your question has nothing specific to interfaces. Forget about interfaces for a minute and focus on just inheritance and run-time vs. compile-time types.

Consider this:
C#
class Base { / *...* / }

class Derived {
    internal DerivedSpecific() { / *...* / }
}

//...

// compile-time type: Base, actual run-time type: Derived
Base instance = new Derived();

// even though the instance is Derived and Derived.DerivedSpecific can be called,
// instance "does not know" about it during compile time.
// this will fail to compile:
instance.DerivedSpecific(); // compilation failure

Now, Base could be a class or in interface, the nature of your bug is the same. You need to move the method to the base type, in your case, to the interface. The methods specific to the derived class should be called in this class or via its instance, not the instance of the interface (or base class) type. One more thing: you can down-case the instance and still call the method. Don't even think about it — it would be possible but means a bad design and defeat of the purpose of inheritance and encapsulation. You code sample shows that you don't feel these purposes, just yet. You need to review your understanding of OOP from the very beginning, not just the interfaces.

—SA
 
Share this answer
 
v2
Comments
VJ Reddy 16-May-12 12:29pm    
Good explanation and advice. 5!
Sergey Alexandrovich Kryukov 16-May-12 12:46pm     CRLF
Thank you, VJ. --SA
Maciej Los 16-May-12 15:49pm     CRLF
I agree with you, VJ. +5
Start with this: Interfaces in C# (For Beginners)[^]
Interfaces - Programming guide[^]

If you want more, use Google with phrase: "C# interface"
 
Share this answer
 
Comments
Sipherz 16-May-12 11:12am    
Ok friend but before that is there some other thing that i should in my interface
Sergey Alexandrovich Kryukov 16-May-12 12:05pm     CRLF
I voted 4. The manual will certainly be helpful, but it's good to note that the problem is more general and related to inheritance and run-time vs. compile-time types, the bases of OOP (even before late binding and dynamic dispatch via virtual methods or interfaces). Please see my answer. --SA
Maciej Los 16-May-12 15:49pm     CRLF
OP doesn't used "General" tag, so my answer is as is ;) 4 is OK ;) Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900