c#.NET interface question
Results 1 to 8 of 8

Thread: c#.NET interface question

  1. #1
    Custom User
    Join Date
    Oct 2001
    Posts
    503

    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:

    Code:
    public interface IBSTNode
    {
        Comparable Data
        {
            get;
            set;
        }
    
        IBSTNode Right
        {
            get;
            set;
        }
    
        IBSTNode Left
        {
            get;
            set;
        }
    }
    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:

    Code:
    IBSTNode tmpNode = aNode;
    tmpNode = tmpNode.Right;
    
    or
    
    BSTNode tmpNode = aNode;
    tmpNode = (BSTNode)tmpNode.Right;
    If anyone understands what I'm talking about and can give me a way round it that would be great - just please bear in mind that I've had about two days experience at c# or so :P

    ac

  2. #2
    Elite Hacker
    Join Date
    Mar 2003
    Posts
    1,407
    What if you make Left and Right in both BSTNode and IBSTNode type BSTNode? I may be way off here .

  3. #3
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    I don't think that would work since BSTNode is implementing IBSTNode, plus I think it would make it pointless having IBSTNode in the first place. Thanks for the reply though.

    ac

  4. #4
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    Anyone else got any ideas? If not I'll have a search for a more programming oriented forum.

    Thanks,

    ac

  5. #5
    Elite Hacker
    Join Date
    Mar 2003
    Posts
    1,407
    Here is what I meant:

    ILinkedListNode.cs
    Code:
    public interface ILinkedListNode
    {
    	int Data
    	{
    		get;
    		set;
    	}
    
    	LinkedListNode Right
    	{
    		get;
    		set;
    	}
    
    	LinkedListNode Left
    	{
    		get;
    		set;
    	}
    }
    LinkedListNode.cs
    Code:
    public class LinkedListNode : ILinkedListNode
    {
    	private LinkedListNode left, right;
    	private int data;
    
    	public LinkedListNode(int val)
    	{
    		data = val;
    	}
    
    	public int Data
    	{
    		get
    		{
    			return data;
    		}
    		set
    		{
    			data = value;
    		}
    	}
    
    	public LinkedListNode Right
    	{
    		get
    		{
    			return right;
    		}
    		set
    		{
    			right = value;
    		}
    	}
    
    	public LinkedListNode Left
    	{
    		get
    		{
    			return left;
    		}
    		set
    		{
    			left = value;
    		}
    	}
    }
    LinkedList.cs
    Code:
    public class LinkedList
    {
    	public LinkedListNode Head;
    
    	public LinkedList()
    	{
    		Head = null;
    	}
    
    	public void add(LinkedListNode newnode)
    	{
    		if(isEmpty())
    		{
    			Head = newnode;
    		}
    		else
    		{
    			LinkedListNode temp = Head;
    
    			while(temp.Right != null)
    			{
    				temp = temp.Right;
    			}
    			temp.Right = newnode;
    		}
    	}
    
    	public bool isEmpty()
    	{
    		return Head == null;
    	}
    }
    LinkedListMain.cs
    Code:
    public class LinkedListMain
    {
    	public static void Main()
    	{
    		LinkedList list = new LinkedList();
    		list.add(new LinkedListNode(5));
    
    		System.Console.WriteLine("data = {0}", list.Head.Data);
    	}
    }

  6. #6
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    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)

    ac

  7. #7
    Elite Hacker
    Join Date
    Mar 2003
    Posts
    1,407
    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.

  8. #8
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    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.

    ac

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides