If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

c#.NET interface question

Hi, I'm trying to learn a bit of c#.NET for a job placement that I've got over the summer and I was wondering if someone could clarify something for me.

I was attempting to implement a binary search tree - or rather have implemented it, since it works - (for practice as much as anything else) and I ran into a bit of trouble with the use of interfaces. Before going on I'll paste in the code for my BSTNode:

The problem that I'm getting is that when I attempt to implement the code, because I've set the types of left and right to IBSTNode I either have to then declare every use of BSTNodes as IBSTNode or I have to typecast, eg:

Thanks for the answer, man - I understood what you meant. All I'm trying to say is that wouldn't doing that make having the interface in the first place completely pointless since whenever you wanted to implement a node class you would have to use whatever class you specified in the interface as the left and right pointers meaning that, for example, the LinkedListNode class would contain a pointer to another LinkedListNode class and a NewNode class would also contain a pointer to LinkedListNode class. I just wanted to know if there was a way to specify that any class implementing a certain interface contained a certain data member which also implemented that certain interface.

I guess that it really doesn't matter since I can either typecast or just change other parts of the code but it was just bugging me since it didn't really make sense to me.

Thanks again and I hope you can understand the jumbled mess I've just written (and the names I used for the classes don't matter)

Originally posted here by gothic_type I just wanted to know if there was a way to specify that any class implementing a certain interface contained a certain data member which also implemented that certain interface.

I think I misunderstood because if I understood you correctly, ILinkedListNode does exactly that. It specifies that whatever implements it must have a LinkedListNode Left and Right property or whatever they call those get set things. LinkedListNode implements ILinkedListNode and since it contains two instances of itself it has two data members which also implement that interface. I'm assuming that's not what you meant by what I quoted above :P.

Anyways, I think the whole point of interfaces is to provide a frame for a class which is going to implement them. I'm sure there's a better reason for them, but I would think it'd be good for projects. The person wanting you to do the project can give you interfaces with methods he wants you to write. You implement the interfaces, which forces you to write the methods.

Also, I think you can do something like this with my code.

ILinkedListNode node = new LinkedListNode(27);

and you shouldn't need to typecast. I may not have the best understanding of interfaces, especially in C# since I'm probably as new as you or newer to it, but I think that's pretty much what they're for. I don't personally use them :P. Peace.

edit
ok, I just got some clarification from Wizeman on IRC. I'll just paste :P.

<skiddieleet> What uses does it have though?
<Wizeman> well you need to consider this in the case of objects
<Wizeman> so if I create a shape class
<Wizeman> and you have the option of implementing a square class that extends shape, then I may want to ensure that you have a method area() that has a particular signature
<Wizeman> I may want to ensure that all such child classes implement that area method, so I set it as an interface

So really they aren't useful for single classes which won't be extended.

I'm sure you can use them where you want to define a common framework for a datatype but allow for different implementations of that datatype. For example if you created a LookupTable interface with various methods for adding to the table and searching it, but didn't want to specify in advance how it was going to be implemented.

My point above about the code you gave me was that if you did that you would end up having to use LinkedList in every class that implemented the interface. I'll try and give you an example of what I mean but I don't promise it'll be understandable:

Code:

public interface IMyClass
{
MyClass aDataMember // what I thought should be here is IMyClass, but alas, no
{
get;
set;
}
}
public class MyClass : IMyClass
{
MyClass aDataMember
{
...
}
}
but you would also have the following if you tried to create another
class implementing the interface:
public class MyOtherClass : IMyClass
{
MyClass aDataMember // what I really want here is MyOtherClass
{
...
}
}

I don't know if you can understand from that what I'm trying to say, but meh.

ILinkedListNode node = new LinkedListNode(27);

I know I can do that, but I didn't want to because it looks a bit illogical since you would expect that you could substitute LinkedListNode for ILinkedListNode anywhere in any case.