c# - Method to pass 3 different data types to do the same thing? -
i think title might wrong.. haha unsure.. anyway..
i have 3 different data types..
public class data1 { public color color; public int len; public drawingvisual dv; other vars... } public class data2 { public color color; public int len; public drawingvisual dv; other vars different data1 vars... }
etc...
how can create function pass these in , vars need inside function.. example..
void something(var data) { switch (typeof(data)) { case data1: break; case data1: break; } }
this wont work obviously.. example..
how can this?
thanks
consider create classes hierarchy, , move method something hierarchy. base class:
public abstract class data { public color color; public int len; public drawingvisual dv; public abstract void something(); }
and 2 derived classes data1 , data2:
public class data1 : data { // other vars... public override void something() { // use base class data , other vars } } public class data2 : data { // other vars different data1 vars public override void something() { // different implementation } }
now when have instances of these classes, can call something on each item without trying determine type. each item have required variables complete action:
list<data> items = // list of data1 , data2 foreach(data data in items) data.dosomething();
Comments
Post a Comment