-4

I am experimenting in C# and I am trying to figure out how i can have certain classes as types.

Scenario:

I have a car:

public class Car
{
    public string Name { get; set; }

    public Car()
    {

    }
    public Car(string name, string type)
    {
        Name = name;
        Type = type;
    }    
}

i would like the car to have a Type property. I have two car types:

public class MonsterTruck
{
    public int Lengt => 1;
}

public class SportTruck
{
   public int Lengt => 3;
}

Within the Car class how can I have a "type" of the above two car-type classes?

awh112
  • 1,466
  • 4
  • 22
  • 34
ThunD3eR
  • 3,216
  • 5
  • 50
  • 94
  • 6
    Use the [Inheritance](https://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)), Luke. – Ondrej Tucny Mar 13 '17 at 18:05
  • 1
    Your type is your type. Ie. `MonsterTruck` is your type, you do not need another `type` property/field. What is missing above is that `MonsterTruck` should be a type of car, ie. `public class MonsterTruck : Car`. Same with `SportTruck`. See also [c# inheritence](https://msdn.microsoft.com/en-us/library/ms173149.aspx) – Igor Mar 13 '17 at 18:06
  • Im not sure i understand, I want a car to be of a certain type, example: a car is a monstertruck or a car is a sportstruck – ThunD3eR Mar 13 '17 at 18:09
  • 1
    Read the links provided by OndrejTucny and myself. It will provide you with your answers. – Igor Mar 13 '17 at 18:10
  • 2
    Do some research on OOP concepts. Since a "monstertruck is a car", review [this question](http://stackoverflow.com/q/2218937/215552) as well. – Heretic Monkey Mar 13 '17 at 18:11
  • @Mike McCaughan you are correct I had it the other way arround. With the links provided and the answers provided I enriched my knowledge about this. thank you. – ThunD3eR Mar 13 '17 at 18:18

3 Answers3

2

There are a few different ways to go about this depending on your needs.

You can create an Enum called CarType and have that be a property of Car:

public enum CarType
{
    MonsterTruck,
    SportTruck
}

public class Car
{
    public CarType Type {get;set;}
    //Rest of your code.
}

However This would be the perfect time to learn Inheritance

Instead of having a CarType property, imagine if you had an entirely new class called MonsterTruck that inherited the fields of a Car but also allowed it to handle things differently. After all, it is a different kind of car but still a car.

Example:

public class Car
{
    public int CylinderCount {get;set;}
    public double GasTankSize {get;set;}
    public double Weight {get;set;}

    public virtual string GetFormattedWeight()
    {
        return $"Your car's weight is {Weight}!"
    }
}

public class MonsterTruck : Car
{
    public override string GetFormattedWeight()
    {
        return $"Your monster truck's weight is {Weight}!"
    }
}

Later on when you need to discern the type you can just check the actual type of the class:

if(someInstance is MonsterTruck)
{
    //this object is a MonsterTruck type
}
maccettura
  • 10,514
  • 3
  • 28
  • 35
1

There's lots of ways to do this in programming. One option is to use inheritance, but I'm finding myself moving away from inheritance.

Running injection / strategy on something like this makes the system more maintainable and readable. It's a bit more work up-front to get it setup, however it provides a better solution due to the ability to quickly extend it and test each individual component. Working with inheritance layers can be quite painful if not managed properly, and unit testing layers of inheritance is never fun. Having to write If/Else statements for mundane settings on something isn't efficient :)

I would suggest injection / strategy patterns.

I created a quick fiddle on it: https://dotnetfiddle.net/26u5ck

Ryan Ternier
  • 8,714
  • 4
  • 46
  • 69
0

You can inherit Car and use the constructor in the child classes to setup the Type.
Please see the code below for more details:

public class MonsterTruck : Car
{
    public int Lengt => 1;

    public MonsterTruck() 
        : base(null, "MonsterTruck")
    { 

    }

    public MonsterTruck(string name) 
        : base(name, "MonsterTruck")
    { 

    }
}

If you want to know the Type per se, you can do as follow:

Car myCar = new MonsterTruck();
if(myCar is MonsterTruck)
{
    // do monster truck specific process
}
StfBln
  • 1,137
  • 6
  • 11