Dynamic class constructor in c#
c#, class, constructor, oop
Solution
I would create two static methods for the two parts where you've got only partial information. For example:
public Constructor(int minValue, int maxValue)
{
this.Min = minValue;
this.Max = maxValue;
}
public static Constructor FromMinimumValue(int minValue)
{
// Adjust default max value as you wish
return new Constructor(minValue, int.MaxValue);
}
public static Constructor FromMaximumValue(int maxValue)
{
// Adjust default min value as you wish
return new Constructor(int.MinValue, maxValue);
}
(The C# 4 option of using named arguments is good too, but only if you know that all your callers will support named arguments.)
Problem
I have one class with 2 properties called MinValue, MaxValue , If somebody wants to invoke this class and Instantiate this class ,I need some to have constructor that allow select MinValue Or Max Value Or Both Of them , the MinValue and MaxValue both of them are int, So the constructor doesn't allow me like this: ``` public class Constructor { public int Min { get; set; } public int Max { get; set; } public Constructor(int MinValue, int MaxValue) { this.Min = MinValue; this.Max = MaxValue; } public Constructor(int MaxValue) { this.Max = MaxValue; } public Constructor(int MinValue) { this.Min = MinValue; } } ``` Now I cannot do that because I cannot overload two constructor, How Can I implement this?