Conversion type

Posted by Ruben on Stack Overflow See other posts from Stack Overflow or by Ruben
Published on 2010-03-24T13:30:42Z Indexed on 2010/03/24 13:33 UTC
Read the original article Hit count: 327

Filed under:
|

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

    public class Instellingen
    {
        private int _ID;
    }
    public class Class1: Label
    {
        private Instellingen _Instellingen;
    }
    public class Class2 : Label
    {
        private Instellingen _Instellingen;
    }
    public class Class3 : Label
    {
        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

© Stack Overflow or respective owner

Related posts about typeconverting

  • processing: convert int to byte

    as seen on Stack Overflow - Search for 'Stack Overflow'
    Hello SO, I'm trying to convert an int into a byte in Processing 1.0.9. This is the snippet of code that I have been working with: byte xByte = byte(mouseX); byte yByte = byte(mouseY); byte setFirst = byte(128); byte resetFirst = byte(127); xByte = xByte | setFirst; yByte = yByte >> 1; port… >>> More

  • Conversion type

    as seen on Stack Overflow - Search for 'Stack Overflow'
    Hi, I've got the following question. I've got one class "Instellingen" that's a property in 3 other classes public class Instellingen { private int _ID; } public class Class1: Label { private Instellingen _Instellingen; } public class Class2 : Label … >>> More

Related posts about c#