C
C#3y ago
jin.fuyou

inherit two interface that have same properties

hi all, i have two interfaces that have member with same name inside, and i inherit these two interfaces for some reason but i can't seem to access only one of the member from an interface.. is it possible we decide which member from which interface we wanted to use?
public interface IMemo{
public long id {get;set;}
}

public interface IStickyNote {
public long id {get;set;}
}
public class MyNote <T> where T :IMemo, IStickyNote {
//if i access id member here i will get ambiguity exception
}
public interface IMemo{
public long id {get;set;}
}

public interface IStickyNote {
public long id {get;set;}
}
public class MyNote <T> where T :IMemo, IStickyNote {
//if i access id member here i will get ambiguity exception
}
24 Replies
Yawnder
Yawnder3y ago
Yes it is, but I think you fundamentally don't understand what an interface is. An interface is not an implementation. An interface is a contract that says "whomever implements that interface has to expose [this thing]". Now, in your example, you said "Anything that implements IMemo has to provide an id, and anything that implements IStickyNote has to provide an id". If anything implements both, as long as it exposes an id, it matches both contracts so you're fine.
jin.fuyou
jin.fuyouOP3y ago
so these two conditions below are incorrect, and it shouldnt work this way if i implement an interface, is that what you mean?
Anything that implements IMemo has to provide an id, and anything that implements IStickyNote has to provide an id
If anything implements both, as long as it exposes an id, it matches both contracts so you're fine.
Anything that implements IMemo has to provide an id, and anything that implements IStickyNote has to provide an id
If anything implements both, as long as it exposes an id, it matches both contracts so you're fine.
Yawnder
Yawnder3y ago
No, what I wrote (and you quoted) is what it actually is.
jin.fuyou
jin.fuyouOP3y ago
ohh so it's correct.. hmm.. but how do we resolve the ambiguity if we want to access a member but both interface have same name?
Yawnder
Yawnder3y ago
Why would there be an ambiguity?
jin.fuyou
jin.fuyouOP3y ago
foreach (T note in allNote){
note.id = 1234; //ambiguity between IMemo.id and IStickyNote.id

}
foreach (T note in allNote){
note.id = 1234; //ambiguity between IMemo.id and IStickyNote.id

}
im not sure but this is the exception visual studio give..
Yawnder
Yawnder3y ago
New example: There is a bar. To enter the bar, you need to be 18y or more. There is a convenient store. To buy beer there, you need to be 18y or more. If you are 20y old, is there an ambiguity as to if you can enter the bar or buy beer? As long as you meet the criteria, you're good.
jin.fuyou
jin.fuyouOP3y ago
hmm.. because i inherit from both interfaces
public class MyNote <T> where T :IMemo, IStickyNote {

public void Test(){
//if i access id member here i will get ambiguity exception
foreach (T note in allNote){
note.id = 1234; //ambiguity between IMemo.id and IStickyNote.id

}
}
}
public class MyNote <T> where T :IMemo, IStickyNote {

public void Test(){
//if i access id member here i will get ambiguity exception
foreach (T note in allNote){
note.id = 1234; //ambiguity between IMemo.id and IStickyNote.id

}
}
}
and both IMemo and IStickyNote have "id" member so when i try to access "id" there's this message from visual studio ->//ambiguity between IMemo.id and IStickyNote.id
MODiX
MODiX3y ago
Yawnder#7904
REPL Result: Success
interface IA { int MyInt { get; set ; } }
interface IB { int MyInt { get; set ; } }
class C : IA, IB { public int MyInt { get; set ; } }

var c = new C();
c.MyInt = 3;
Console.WriteLine(c.MyInt);
interface IA { int MyInt { get; set ; } }
interface IB { int MyInt { get; set ; } }
class C : IA, IB { public int MyInt { get; set ; } }

var c = new C();
c.MyInt = 3;
Console.WriteLine(c.MyInt);
Console Output
3
3
Compile: 658.462ms | Execution: 58.445ms | React with ❌ to remove this embed.
Yawnder
Yawnder3y ago
No, your problem is because what you wrote is non-sense an just not valid code. You can't have a foreach in a class, it needs to be in a method.
jin.fuyou
jin.fuyouOP3y ago
ok i edited the code, i was typing in discord so there was a mistake but in the visual studio editor, the error is only in the ambiguity line
Yawnder
Yawnder3y ago
Once again, it's invalid code...
jin.fuyou
jin.fuyouOP3y ago
mm.. why..?
public class MyNote <T> where T :IMemo, IStickyNote {

public void Test(){
//if i access id member here i will get ambiguity exception
foreach (T note in allNote){
note.id = 1234; //ambiguity between IMemo.id and IStickyNote.id

}
}
}
public class MyNote <T> where T :IMemo, IStickyNote {

public void Test(){
//if i access id member here i will get ambiguity exception
foreach (T note in allNote){
note.id = 1234; //ambiguity between IMemo.id and IStickyNote.id

}
}
}
Yawnder
Yawnder3y ago
Look, I've explained, then I've shown you with some code that compiled and executed, that it works, not sure what else I can do.
jin.fuyou
jin.fuyouOP3y ago
ohh i should create a new object?
Yawnder
Yawnder3y ago
allNote doesn't exist. It can't work.
jin.fuyou
jin.fuyouOP3y ago
ok supposed i have this valid allNote, bcs i get the allNote from database, so i didnt post it in the code....
Thinker
Thinker3y ago
Never mind the question of why MyNote is generic in the first place
jin.fuyou
jin.fuyouOP3y ago
yah it's a code that i take over.. i think they've some requirement on this previously.. but actually the problem is only 1, the exception only gives me "ambiguity...." from visual studio
Yawnder
Yawnder3y ago
It can't say that as any and all code you've shown doesn't compile. Anyways, I'm off to bed.
Thinker
Thinker3y ago
You can't unify both interfaces with a single IHasId interface?
jin.fuyou
jin.fuyouOP3y ago
hmm.. i saw that there'll be lots of changes.. there's solution to this from the error https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs0229?f1url=%3FappId%3Droslyn%26k%3Dk(CS0229) - i click from the suggested link from the error but if i append ((IInterface)x).properties, it'll kind of messy..
Thinker
Thinker3y ago
I think if you just have a single IHasId interface which both IMemo and IStickyNote inherits from, then it'll work much better.
jin.fuyou
jin.fuyouOP3y ago
i see.. alright.. i'll try to refactor the code.. thanks!
Want results from more Discord servers?
Add your server