1

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

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
Chris Fazzio
  • 375
  • 1
  • 6
  • 16

3 Answers3

7

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();
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
1

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.

Adil
  • 146,340
  • 25
  • 209
  • 204
1

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.

Simon Belanger
  • 14,752
  • 3
  • 41
  • 35
  • Why implement 3 identical contracts ? Just to distinguish types ? I would suggest to create abstract class and implement specialized method in each child class as @lazyberezovsky did. – Mechanical Object Jul 25 '13 at 14:11
  • @MechanicalObject It is _one_ option. I'm not saying it is _ideal_. If Chris can't refactor the class, it still is a valid option. – Simon Belanger Jul 25 '13 at 14:13
  • It's all good.. Every post here will help me to fix my classes to perform better.. Right now my stuff is like jelly slapped on bread.. I want to move everything to the next step.. – Chris Fazzio Jul 25 '13 at 14:17
  • Can you explain why I should use interface over class? I will research online for the difference.. Thanks – Chris Fazzio Jul 25 '13 at 14:34
  • @ChrisFazzio Conceptually, you want to use an interface if you have common behaviour (a plane and a bird can both "fly" and implement the `IFly` interface, but a plane isn't a bird and vice-versa) relationship and class inheritance if you have a "is-a" relationship and composition if you have a "has-a" relationship. You can look at [this SO for more details](http://stackoverflow.com/questions/761194/interface-vs-abstract-class-general-oo). – Simon Belanger Jul 25 '13 at 14:40
  • Thanks.. I did look it up.. I pretty much get it.. but trying to get the stuff that was given here to work with the stuff I made already seems a little hard.. anyway, I made a public interface IData with the vars I need.. and now I need to figure out how to set them vars from the variable data types.. I think I am on the right path. – Chris Fazzio Jul 25 '13 at 14:43