Search code examples
c#objectoverloadingderived-class

How to use a overload function in a constructor in c#


I want to create a geometry, based on different dimensions and a geometry flag, to determine if it is a cube or circle. To do so I have to use overload functions, but I do not know how i can utilize these in a class function to store my input. Here is what I did so far:

public void Object( double x, double y, double z)
        {
            name = "Cube";
            a = x;
            b = y;
            c = z;
        }
        public void Object(double r, double y)
        {
            name = "Cylinder";
            r1 = r;
            b = y;

        }

    protected double a{ get; private set; }
    protected double b{ get; private set; }
    protected double c{ get; private set; }
    protected double r1{ get; private set; }

First problem I have is, i can't use the declared variables multiple times, i have to to declare a variable for each possible object, in this case I can not save two variables on b, which is kinda ineffective.

My second problem is if i want to call the object in my dataclass like this along with other values it does not work:

public MeasureObject(double hash, string name, new Object obj(int n, different variables), double coordinates, ...)

{
 this.Hash = hash;
 this.Object=obj;
}

Is there a better way to implement a generic geometry in an object which can take an integer and n-different dimensions,lengths whatever?


Solution

  • Geometric objects have enough differences between them that it would be better to have separate classes.

    For example, calculating the volume of each is done differently.

    One solution could be to have a base class from which to inherit and have a factory class that determines which geometrical object instance you need based on the flag.