pavjel
pavjel

Reputation: 486

Get property default value on class initialize

Let's say we have some class CarsBase

public class CarsBase
{
    public string DisplayName { get; set; }
}

Then we have some other class Toyota

public class Toyota : CarsBase
{
    public EngineType EngineType { get; set; }
}

Then we are initializing our class instance by using object initializer like so:

var myVar = new Toyota()
{
    // DisplayName = "", ← We could do this by our hands, but can it be done automatically?
    EngineType = EngineType.UZ
}

Question: Is there any way to fill CarsBase's DisplayName property automatically on object initialize?

Like, if I had several more car classes (BMW, Suzuki , etc.), each is extending CarsBase and as a result have DisplayName property in each class.

Upvotes: 0

Views: 817

Answers (4)

bhagi
bhagi

Reputation: 56

Yes, it can be done during the initialization stage of object where constructor is fired . I have created two classes * one for holding enum constant value for engine_Types --> EngineType

  • one for explaining the inheritance,Constructor-Chaining, creating an instance of class which is an object----> CarsBase

[pre]

    namespace stacketst
     {

    public class CarsBase
     {
    public string DisplayName { get; set; }
          public CarsBase()
          {
    //called when CarBase object is initialized
     DisplayName = "Base Car";

     }
    }

     public class Toyota : CarsBase
     {
    //getters , setters called as properties in C#         
     public int number_of_wheels { get; set; }
    public double fuel_capacity { get; set; }
    public string engine_type { get; set; }

     public Toyota() //called when an instance of Toyota is created
     {
    //assinging value to this property calls set 
    fuel_capacity = 4.2; 
    number_of_wheels = 4; 
    engine_type = EngineType.name_engines.UZ.ToString(); 
   }     
   }

     public class TestClass
    {
    static void Main()
    {
        //when below line is executed,constructor is fired & the initialization of variable inside constructor takes place    

    var myVar = new Toyota();
  Console.WriteLine(myVar.DisplayName);
     }
    }
    }

    namespace stacketst
        {
     public class EngineType
    {
     //enums to hold constants, common for any Car Class
         public enum name_engines 
    { 

    V12, V10, V8, V6, UZ        
     };
     }
    }

[/pre]

Upvotes: 0

Kiradien
Kiradien

Reputation: 104

This sounds like something that should be done in a constructor.

public class Toyota : CarsBase
{
    public Toyota() : base()
    {
        base.DisplayName = "Toyota";
    }
    public EngineType EngineType { get; set; }
}

Another option, however less recommended, instead of getting/setting a DisplayName in the same sense, the base class could be changed to use reflection retrieve the classname and use that as the display name:

public class CarsBase
{
    public string DisplayName 
    { 
        get 
        { 
            return this.GetType().Name; 
        }
    }
}

This method should just return the "Toyota" from the classname, however would prevent usage of spaces or other special characters. Reflected code such as this also has a tendency to be slower.

Upvotes: 1

BenKoshy
BenKoshy

Reputation: 35575

Just set the property value in the constructor. Something like this:

    internal class Program
{
    private static void Main(string[] args)
    {
        Toyota t = new Toyota() { EngineType = new EngineType() };
        Console.WriteLine(t.DisplayName);
        Console.ReadLine();
    }
}

public class CarsBase
{
    public string DisplayName { get; set; }
}
public class Toyota : CarsBase
{
    public EngineType EngineType { get; set; }

    public Toyota()
    {
        // set the default Display Name
        // that way you don't have to set it everytime
        this.DisplayName = "Oh what a feeling!";
    }
}
public class EngineType { }

Upvotes: 0

Souvik Ghosh
Souvik Ghosh

Reputation: 4606

Create a constructor to pass dispay name (or other parameters as required)-

Toyota(string displayName)
{
    DisplayName = displayName;
    EngineType = EngineType.UZ;
}

Then you can call like this-

new Toyota("some display name");

Upvotes: 0

Related Questions