Results 1 to 5 of 5

Thread: Interfaces

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2002
    Posts
    382

    Interfaces

    It was suggested to me to use Interfaces on a project i'm working on because you are supposed to be able to Iterate through a interface regardless of the classes that use them. I'm a bit confused on exactely what that means. Does it mean that I could have 5 different classes using one interface and I would be able to Iterate through all the classes that are using that Interface?

  2. #2
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    suppose you have the interface IPhysicalObject, which implements the following members: (C#, you get the idea though)
    Code:
    public interface IPhysicalObject
    {
         double GetWeight();
         string GetName();
         Color GetColor();
    };
    and you then have the following classes implement it:
    Code:
    public class MyBox : IPhysicalObject
    {
         public double GetWeight() { return 15.00; } ;
         public String GetName() { return "My Heavy Red Chest"; };
         public Color GetColor() { return Color.Red; };
         public void Lock() { /* .... */ };
         public void Unlock() { /* .... */ };
    
    };
    
    public class MyFridge: IPhysicalObject
    {
         public double GetWeight() { return 100.00; } ;
         public String GetName() { return "My Big Refrigerator"; };
         public Color GetColor() { return Color.White; };
    
         public bool IsFoodRotten() { /* .... */ };
    };
    
    public class MyShirt: IPhysicalObject
    {
         public double GetWeight() { return .5; } ;
         public String GetName() { return "My Green Shirt"; };
         public Color GetColor() { return Color.Green; };
    
         public void TurnInsideOut() { /* .... */ };
    };

    now suppose you wanted to keep these three things in an array together, maybe for some weird game where you had objects in a room:
    Code:
    myShirt shirt = new myShirt();
    myFridge fridge = new myFridge();
    myBox box = new myBox();
    
    ArrayList myRoom = new ArrayList();
    myRoom.Add(shirt);
    myRoom.Add(fridge);
    myRoom.Add(box);
    since all the objects in your array implement IPhysicalObject, you can do something like this:
    Code:
    foreach(IPhysicalObject myObj in myRoom)
    {
        Console.Write("This Object [" + myObj.Name + "]");
        Console.WriteLine("is " + myObj.Color.ToString());
    }
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2002
    Posts
    382
    Thank you sunburnt..

    Excellent example, just what I needed....


  4. #4
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870
    is that the only real use interfaces have? For when you iterate through a collection of different object using the same one?

    I never bother using interfaces to be honest as i never saw the point of them. Am beginning to from this example tho!

    Nick
    www.vb-tech.com
    .Net Freelance Development
    http://weblog.vb-tech.com/nick
    My blog

  5. #5
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    They also provide a set of rules that a class must follow that can be enforced. for example if you make your app have a plugin system. You would want to create an interface so that all plugins have a set functions and properties that they MUST write code for because those are what the main app will expect.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

Posting Permissions

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



Click Here to Expand Forum to Full Width