views:

87

answers:

5
+1  Q: 

Conversion type

Hi, I've got the following question.
I've got one class "Instellingen" that's a field in 3 other classes

    public class Instellingen
    {
        private int _ID;
    }
    public class Class1: Button
    {
        private Instellingen _Instellingen;
    }
    public class Class2 : Label
    {
        private Instellingen _Instellingen;
    }
    public class Class3 : TextBox
    {
        private Instellingen _Instellingen;
    }

If I've got another class, that uses the other classes (but it can be anyone of this 3 classes) Do i have to use a switch? or is there an easier way?

    public class AnotherClass
    {
        public AnotherClass ()
        {
            GetInstellingenFromClass(new Class1());
            GetInstellingenFromClass(new Class2());
            GetInstellingenFromClass(new Class3());
        }
        private void GetInstellingenFromClass(Control c)
        {
            switch (c.GetType.ToString())
            {
                case "Class1":
                    Class1 klasse = (Class1) c;
                    //Do something with the _Instellingen of this class
                    break;
                case "Class2":
                    Class2 klasse2 = (Class2) c;
                    //Do something with the _Instellingen of this class
                    break;
                case "Class3":
                    Class3 klasse3 = (Class3)c;
                    //Do something with the _Instellingen of this class
                    break;

            }
        }
    }

(does there exists something so i can just do something like c._Instellingen --> without converting it first to the right type, where it doesn't matter what type c is?)

I hope you understand my question.

Thanks

+1  A: 

Polymorphism. In your case, all classes extend Label, so you could have Label define your Instellingen:

public class Label
{
    public Instellingen Instellingen
    {
        get { return ...; }
    }
}

Then AnotherClass can just work with Labels:

    private void GetInstellingenFromClass(Label l)
    {
        var instellingen = l.Instellingen;

        // do something with instellingen here
    }

Of course, if you don't own the Label class you can always subclass it and use that from your classes. Another option is to define an interface with the property and have AnotherClass depend on that interface.

HTH,
Kent

Kent Boogaart
+5  A: 

You should make an interface that has an Instellingen property and implement it in the three classes.

For example:

interface IHasInstellingen {
    Instellingen Instellingen { get; }
}

public class Class1: Label, IHasInstellingen {
    public Instellingen Instellingen { get; private set; }
}
public class Class2: Button, IHasInstellingen {
    public Instellingen Instellingen { get; private set; }
}


private void GetInstellingenFromClass(IHasInstellingen c) {
    Instellingen ig = c.Instellingen;
    //Do things...
}
//Alternatively:
private void GetInstellingenFromClass(Control c) {
    IHasInstellingen hi = c as IHasInstellingen;
    if (hi == null)
        return;     //Or throw an ArgumentException

    Instellingen ig = hi.Instellingen;
    //Do things...
}
SLaks
In this case, since they all share that same functionality and implement it in the same way...wouldn't an abstract base class make more sense?
Justin Niessner
Since his method takes a `Control`, I'm assuming that they aren't necessarily all labels.
SLaks
But he checks c.GetType to see if it matches one of his custom types. It only handles calls for types of Class1, Class2, and Class3 which all inherit from Label to begin with. I see what you're saying though.
Justin Niessner
Since the names have obviously changed, the base types may have also changed.
SLaks
i'm sorry, it can be something else also, it doesn't have to be a label (it could be Button, Label, ....)
Ruben
I'm going to try this one. Thanks a lot people for the quick and good responses.
Ruben
A: 

Instead of having each of your three classes derive from Label, can you create an intermediate base class, like LabelWithInstellingen?

jwismar
so you mean Instellingen: Label and than Class1: Instellingen?
Ruben
A: 

Consider using an interface to expose an Instellingen property in the classes that need them.

class Instellingen
{
    public int ID { get; set; };
}
interface IHasInstellingen
{
    Instellingen Instellingen { get; set; }
}

class MyLabel: Label, IHasInstellingen
{
    public Instellingen Instellingen { get; set; }
}
class MyButton: Button, IHasInstellingen
{
    public Instellingen Instellingen { get; set; }
}

class AnotherClass
{
    public AnotherClass ()
    {
        GetInstellingenFromClass(new MyLabel());
        GetInstellingenFromClass(new MyButton());
        // ...
    }
    private void GetInstellingenFromClass(IHasInstellingenc)
    {
        Console.WriteLine( c.Instellingen.ID );
        // ... Do something with Instellingen
    }
}
Yannick M.
+1  A: 

No, you don't have to use a switch. Actually the concept Interface is what you are looking for. Something like;

public interface IIntelingenProvider
{
    Intelingen Item {get;}
}

public class Class1: Label, IIntelingenProvider
{
    private Instellingen _Instellingen;
    public Intelingen Item { get { return _Instellingen; } }
}

public class Class2: Label, IIntelingenProvider
{
    private Instellingen _Instellingen;
    public Intelingen Item { get {return _Instellingen; } }
}

And the type you are going to provie to the GetInstellingenFromClass method would be IIntelingenProvider. Hence you can write it as;

    private void GetInstellingenFromClass(IIntelingenProvider c)
    {
        // regardless of the type, they all have an Item property of type Intelingen
        // c.Item
    }

I suggest you to read and learn more about Interitance and Polymorphism

tafa
Your syntax is wrong. (Interfaces do not have access modifiers)
SLaks
Thank you, SLaks. Corrected
tafa