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?
24 Replies
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.so these two conditions below are incorrect, and it shouldnt work this way if i implement an interface, is that what you mean?
No, what I wrote (and you quoted) is what it actually is.
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?
Why would there be an ambiguity?
im not sure but this is the exception visual studio give..
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.
hmm.. because i inherit from both interfaces
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
Yawnder#7904
REPL Result: Success
Console Output
Compile: 658.462ms | Execution: 58.445ms | React with ❌ to remove this embed.
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.
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
Once again, it's invalid code...
mm.. why..?
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.
ohh i should create a new object?
allNote
doesn't exist. It can't work.ok supposed i have this valid allNote, bcs i get the allNote from database, so i didnt post it in the code....
Never mind the question of why
MyNote
is generic in the first placeyah 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
It can't say that as any and all code you've shown doesn't compile.
Anyways, I'm off to bed.
You can't unify both interfaces with a single
IHasId
interface?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..
I think if you just have a single
IHasId
interface which both IMemo
and IStickyNote
inherits from, then it'll work much better.i see.. alright.. i'll try to refactor the code..
thanks!