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
typeconverting
|c#
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