?
Solved

Can a class that implments an Interface declare a member that differs from the Interface definition?

Posted on 2012-08-11
8
Medium Priority
?
704 Views
Last Modified: 2012-08-12
I thought the answer to this question was no

public interface Iface
{
   void func1(string name);
   void func2(string name, string surname);
}

public class MyClass : IFace
{
  public void func1(string name, int age);
  public void func2(string name, string surname, string address);
}

Open in new window


Is the above leagal or does the class have to declare members that exactly match the Interface definition?
0
Comment
Question by:Julian Hansen
8 Comments
 
LVL 2

Assisted Solution

by:rajshekher
rajshekher earned 400 total points
ID: 38283225
Hi,

Yes, because, the declared class have different parametered function. then it is considered that different function through function overloading.

But you must to implement the Interface function in the declared class MyClass.
0
 
LVL 45

Assisted Solution

by:AndyAinscow
AndyAinscow earned 400 total points
ID: 38283234
No.  It isn't complete

public interface Iface
{
   void func1(string name);
   void func2(string name, string surname);
}

public class MyClass : IFace
{
  public void func1(string name);  //MUST implement
  public void func2(string name, string surname);  //MUST implement

  public void func1(string name, int age);
  public void func2(string name, string surname, string address);
}
                                  

Open in new window

0
 
LVL 60

Author Comment

by:Julian Hansen
ID: 38283359
Ok, that is what I thought - but here is my problem.

I have inherited a project from another developer - based on NopCommerce.

The original source from NopCommerce contains two files
INopService.cs
NopService.cs

Extracts to follow

INopService
public interface INopService
{
    ...
    void UpdateOrderBillingInfo(int orderId, string firstName, string lastName, string phone, string email, string fax, string company, string address1, string address2, string city, string region, string country, string postalCode, string usernameOrEmail, string userPassword);
    
    void UpdateOrderShippingInfo(int orderId, string firstName, string lastName, string phone, string email, string fax, string company, string address1, string address2, string city, string region, string country, string postalCode, string usernameOrEmail, string userPassword);
    ...
}

Open in new window


NopService
public class NopService : INopService
{
   ...
   public void UpdateOrderBillingInfo(int orderId, string firstName, string lastName, string phone, string email, string fax, string company, string address1, string address2,
            string suburb, string city, string stateProvinceAbbreviation, string countryThreeLetterIsoCode, string postalCode, string usernameOrEmail, string userPassword)

   public void UpdateOrderShippingInfo(int orderId, string firstName, string lastName, string phone, string email, string fax, string company, string address1, string address2,
            string suburb, string city, string stateProvinceAbbreviation, string countryThreeLetterIsoCode, string postalCode, string usernameOrEmail, string userPassword)

}

Open in new window


This code compiles fine - but the same two files in my source don't and generate the error that a member does not exist in the class implementing the interface.

My question then is how can the above compile with the Nop Commerce release?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 45

Expert Comment

by:AndyAinscow
ID: 38283369
Are you certain that is exactly what compiles in the other app ?  (I suspect it isn't)
0
 
LVL 60

Author Comment

by:Julian Hansen
ID: 38283384
This is why I am posting here - did a forum post at NC but no responses.

I just checked and I posted the code from my version of the source instead of the original NP code. Here is the code from their files NCC denotes NopCommerce Class and NCI denotes the interface. I have also attached the original files.

As you can see they are still different.

I searched the source for all instances of UpdateOrder and only these were found.

Just confused as to why the downloaded source for NC 2.6 compiles?


NCI void UpdateOrderBillingInfo(int orderId, string firstName, string lastName, string phone, string email, string fax, string company, string address1, string address2, string city, string region, string country, string postalCode, string usernameOrEmail, string userPassword);
NCC void UpdateOrderBillingInfo(int orderId, string firstName, string lastName, string phone, string email, string fax, string company, string address1, string address2, string city, string stateProvinceAbbreviation, string countryThreeLetterIsoCode, string postalCode, string usernameOrEmail, string userPassword)

NCI void UpdateOrderShippingInfo(int orderId, string firstName, string lastName, string phone, string email, string fax, string company, string address1, string address2, string city, string region, string country, string postalCode, string usernameOrEmail, string userPassword);

NCC void UpdateOrderShippingInfo(int orderId, string firstName, string lastName, string phone, string email, string fax, string company, string address1, string address2, string city, string stateProvinceAbbreviation, string countryThreeLetterIsoCode, string postalCode, string usernameOrEmail, string userPassword)

Open in new window

NopService.cs
INopService.cs
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1200 total points
ID: 38284827
The NC 2.6 code you just posted will compile because the function signatures match. Remember that the compiler doesn't care one bit about the names of parameters. All it cares about is the name of your method, the number of parameters, and the types of each parameter. This is how the compiler determines which version of an overload to use when it is compiling your code.

In your case, the NC 2.6 class correctly defines two methods which both correspond to a method definition in the interface with the appropriate name, number of parameters, and parameter types. It does not matter that the interface says "region" where the class says "stateProvinceAbbreviation"--both are the same type:  string. If the class instead had defined "stateProvinceAbbreviation" to be of type int, for example, then you would have a problem.
0
 
LVL 60

Author Comment

by:Julian Hansen
ID: 38285140
@kaufmed - I have been looking at this code for a week and I missed that.

Sometimes it takes a fresh pair of eyes. I thought I had lost my marbles - re-read the .Net doc's on Interfaces trying to see if I had missed something.

Thank you.
0
 
LVL 60

Author Closing Comment

by:Julian Hansen
ID: 38285146
All three of you assisted in helping solve this issue for me. kaufmed pointed me in the right direction to understand what was going on.

Thanks to all three of you - really appreciate your input.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Starting up a Project

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question