Conversion type
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 Instell开发者_StackOverflow中文版ingen _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
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...
}
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
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 Label
s:
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.
Instead of having each of your three classes derive from Label, can you create an intermediate base class, like LabelWithInstellingen?
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
}
}
精彩评论