Re: Beginner interface question

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Thanks for the replies all, it makes more sense now. I thought it might
be a bit 'fiddly' casting to the interface type to call certain
methods, but after playing around, I found out this:

public interface IShape
{
double area();
double circumference();
int sides();
void blah();
}

public class Oblong :IShape
{
public int side;

public double area() { return (double)((side * side) * 2); }

public double circumference() { return ((double)(6 * side)); }

public int sides() { return 4; }
public Oblong() { side = 0;}

void IShape.blah(){ Console.WriteLine("Test"); }
}

public class Shape
{
public static void Main(string[] args)
{
Oblong myOb = new Oblong();
myOb.side = 5;
Console.WriteLine("Displaying Info:");
displayInfo(myOb);
}

static void displayInfo(IShape myShape)
{
Console.WriteLine("Area: {0}", myShape.area());
Console.WriteLine("Sides: {0}", myShape.sides());
Console.WriteLine("Circumference: {0}",
myShape.circumference());
myShape.blah();
}
}

using a method taking the interface type, I can perhaps see uses for
that.

A quick question:
If I had a number of objects which were not derived from the same base
class. Each of these objects had a load and save method, which were
called on every instanced object when the user selected a save event.
Would it be best to create a load/save interface, and then use a method
such as above. Or would it be best to use a function which calls every
object in turn, decides what form the object is and then load/save. Or
even factoring all objects which need load and save functionality into
a common base class? (the load or save could be something like
drawToScreen etc).

I'm trying to get my head around when best to use interfaces etc.

Thanks,

Chris

.



Relevant Pages

  • Re: Names of static types
    ... interface A_ ... public class Main ... public static void main(java.lang.Stringargs) ...
    (comp.lang.java.programmer)
  • Re: Casting Generic Classes - Possible Solution
    ... It leaves me with the open option to use generics or not when I ... This works as saying "accept a store of anything that are Record ... public class Record { ... So I tried adding an interface into the structure: ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Circular Referencing in C#
    ... I used this problem as an interview question about a month ... I configure it using config files. ... put the interface into the base code libraries. ... > public class A1C1 ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: List<> classes and inheritance (C#)
    ... I think the answer to your question is the use of an interface that all ... public class MyClass1: IMyInterface ... There may be no need at all to override the Add method at this point because ...
    (microsoft.public.dotnet.csharp.general)
  • Re: Help with this sample
    ... you could use an interface: ... class Cls1: IReturn ... class Cls2: IReturn ... public class Class1 ...
    (microsoft.public.dotnet.languages.csharp)