Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all

In our application (either in c# or vb.net),
Is it possible to have multiple interface in a single class ?
if so how ? and if so ,

in the following scenario (either in c# or vb.net) for eg.,
Single Class
{
interface 1;
interface 2;
interface 3;
}

In this ,how the program identifies and which interface runs
first ,second and third...

Thanks in advance

S.Naveen...
Posted

There is no problem implementing multiple interfaces, but I imagine what you're trying to do is differentiate between collisions in multiple interfaces?

In order to do this you need to explicitly implement the interface. In c# that would look something like the following:

public class Class1 : IInterface1, IInterface2
{
    string IInterface1.foo
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }

    string IInterface2.foo
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }
}


There is no order between the two; you can't call one and have the other automagically execute. You would need to explicitly call one method from the implementation of the other.

If you want to call either selectively, you would do something like this:
Class1 theClass = new Class1();

IInterface1 i1 = theClass as IInterface1;
IInterface2 i2 = theClass as IInterface2;

string bar1 = i1.foo; // executes the IInterface1 version
string bar2 = i2.foo; // executes the IInterface2 version


Cheers.
 
Share this answer
 
v2
There are many ways to cause a program to execute code in a specific order. The best way for you to do it based on your poorly described question is have each interface's foo method call another interface's foo method in the order you want. I don't know of a way to specify the order though, short of just hard-wiring it.
 
Share this answer
 
v2
Comments
TheyCallMeMrJames 28-Jun-10 9:42am    
hey, i suppose you could use reflection and examine the call stack if you wanted to. i mean, if you were fully hell-bent on negating years of programming disciplines and methodology and wanted to obscure what your code actually did. (what? no sarcastic smilies in comments?)
Thank you Mr.James

But still im little confused like,

How the code knows to run which interface first ,either 1st or 2nd ?

is it needed that we should giv in order or we can pass some
specific instructions ,so that it follows the order of interface...

Thanks in advance

Naveen...
 
Share this answer
 
Comments
#realJSOP 26-Jun-10 11:18am    
It's called "programming" for a reason.
Sandeep Mewara 26-Jun-10 11:23am    
Reason for my vote of 1
You keep using 'Answer' as a mode to discuss further. There is a 'Add comment' feature provided to do the same. Don't press Answer button unless it is not.
nav234 26-Jun-10 11:28am    
Ya im not pressing answer as i still dint get it...
TheyCallMeMrJames 28-Jun-10 9:41am    
still, don't add answers to your questions. i've added a couple of notes to my original answer.

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