Reputation: 375
I think the title might be 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 from data1 vars...
}
Etc...
How can I create a function to pass these in and then get the vars I need inside the function.. Example..
void Something(var data)
{
switch (typeof(data))
{
case data1:
break;
case data1:
break;
}
}
This wont work obviously.. but its just an example..
How can I actually do this?
Thanks
Upvotes: 1
Views: 1602
Reputation: 148150
You can use object
instead of var
void Something(object data)
{
if(typeof(data) == data1.GetType())
{
}
else
if(typeof(data) == data2.GetType())
{
}
}
Your classes seem they have common attributes you can use inheritance. Make a base abstract class and inherit remaining from it.
public abstract class Data
{
public Color color;
public int len;
public DrawingVisual dv;
public abstract void Something();
}
public class Data1 : Data
{
//Addition methods go here
}
public class Data2 : Data
{
//Addition methods go here
}
As a additional note I assume you will use follow the naming conventions for properties and classes etc.
Upvotes: 1
Reputation: 14880
First of all, you might want to reconsider using public fields and use public properties instead. Two options you might consider.
Implement a shared interface
Considering both your class have the same properties, you could have an interface:
public interface IData
{
Color color {get;set;}
int len {get;set;}
DrawingVisual dv {get;set;}
}
public interface Data1
{
public Color color {get;set;}
public int len {get;set;}
public DrawingVisual dv {get;set;}
}
public interface Data2
{
public Color color {get;set;}
public int len {get;set;}
public DrawingVisual dv {get;set;}
}
void Something(IData data)
{
// use the properties defined in the IData interface
}
Something(new Data1());
Something(new Data2());
Use method overloading
void Something(data1 data)
{
// Do something specific using data1 class
}
void Something(data2 data)
{
// Do something specific using data2 class
}
Something(new data1());
Something(new data2());
I'd suggest using the first option.
Upvotes: 1
Reputation: 236268
Consider to create classes hierarchy, and move method Something to that hierarchy. Base class:
public abstract class Data
{
public Color color;
public int len;
public DrawingVisual dv;
public abstract void Something();
}
And two derived classes Data1 and Data2:
public class Data1 : Data
{
// Other vars...
public override void Something()
{
// Use base class data and other vars
}
}
public class Data2 : Data
{
// Other vars different from Data1 vars
public override void Something()
{
// different implementation
}
}
Now when you have instances of these classes, you can simply call Something on each item without trying to determine its type. Each item will have all required variables to complete action:
List<Data> items = // get list of Data1 and Data2
foreach(Data data in items)
data.DoSomething();
Upvotes: 7