-1

I am trying to solve some assignments in C#(MS Visual Studio) and would be grateful for some guidance. I will "paraphrase" the logic as below:

    class Vehicle
     {
        public int Property{get {return Fuel}
          set{ if TwoWheeler child called property then set max = 10
          else(other alternate is FourWheeler child)  max = 50}

     }

    class TwoWheeler : Vehicle
      { }

    class FourWheeler : Vehicle
     {}

    class Program
     {
       TwoWheeler TwoWheeler_object = new TwoWheeler();
       FourWheeler FourWheeler_object = new FourWheeler();
       CW(TwoWheeler_object.Property);
       CW(FourWheeler_object.Property);

     }

This is the basic skeleton version of the code. If any clarifications needed, would be happy to do so. Thank you for the help in advance.

EDITED

The Property is set if the TwoWheeler called Property then the maxLimit=10 and if given Fuel is less than maxLimit then Fuel = value and if Fuel is greater or equal to maxLimit then Fuel = maxLimit

If FourWheeler called Property from Vehicle then maxLimit=50 then rest is the same

Thomas A Mathew
  • 167
  • 1
  • 13
  • Please provide all the code. For instance, I can't see where `TwoWheeler_object` is instantiated. Also, may I ask why you want this? Like why does the base need to know which inherited class called its property? What's your goal? – Scircia Mar 12 '21 at 13:46
  • `Vehicle` shouldn't know who is calling it. If the "Property" behaves differently for `TwoWheeler` and `FourWheeler`, the child classes need to override that behavior. – LarsTech Mar 12 '21 at 14:04
  • Does this answer your question? [Determining the caller inside a setter -- or setting properties, silently](https://stackoverflow.com/questions/19737351/determining-the-caller-inside-a-setter-or-setting-properties-silently) – Crowcoder Mar 12 '21 at 14:04

2 Answers2

2

If I understand correctly you want the "max" value to be depending on the child class calling your code.

Good news if there is a pattern especially for that kind of case called "template pattern":

public abstract class Vehicle
{
    protected abstract int MaxValue { get; }

    private int _myField;

    public int MyProperty
    {
        get => _myField;
        set
        {
            if (value <= MaxValue)
            {
                _myField = value;
            }
        }
    }
}

public class TwoWheeler : Vehicle
{
    protected override int MaxValue => 10;
}

public class FourWheeler : Vehicle
{
    protected override int MaxValue => 50;
}

I made your Vehicle class abstract because in your code you always need it to be a FourWheeler or a TwoWheeler, meaning it should never be a simple Vehicle.

The Vehicle contain the logic you want to apply: Check if the maximum value is not reached (you can obviously put a else and thow an exception, in this case I just choose to not assign anything).

Then the child are oblige to override the MaxValue (because it's abstract) and this value will be used by the parent.

It has some advantages:

  • Logic is not duplicated.
  • Parent is not aware of children.
  • Adding new children without provide this value will make the compilation break.
Arcord
  • 1,724
  • 1
  • 11
  • 16
0

if i understand you correctly You can put this logic inside the set of the parent class:

public int Property{get {return Fuel}
      set
      { 
           switch(this.GetType().Name)
           {
              case typof(TwoWheeler).Name:
                max = 10;
                break;
              case typof(FourWheeler).Name:
                max = 50
                break;
           }
      }
Michael Gabbay
  • 413
  • 1
  • 4
  • 20