-2

I'm just learning C#, but what's the 'fun' of SET if I can't do that? And then what should I do if my input is string only?

public class Parameter
{
    public double? low_bound {
        get { return low_bound ; } 
        set { low_bound = Convert.ToDouble(value); }
    } 
}

and in another file I do: param.low_bound = "string that I recieved from DB";

  • 3
    Because setter expects Nullable not string... And c# is statically typed – Selvin May 18 '23 at 17:09
  • UPD: I know that I can do `param.low_bound = Convert.ToDouble("input str");`, but it's not what I'm looking for – Michael Levin May 18 '23 at 17:12
  • 1
    because c# is type safe and a string isn’t a double you need to convert **prior** to setting it – Rand Random May 18 '23 at 17:12
  • 1
    Maybe add a method: `SetLowBound(string value) ...` – Hans Kesting May 18 '23 at 17:12
  • So why the hell I need SET if I can't do this when I set new value? – Michael Levin May 18 '23 at 17:13
  • You need to use javascript – Selvin May 18 '23 at 17:13
  • have a look at: https://stackoverflow.com/questions/295104/what-is-the-difference-between-a-field-and-a-property | https://stackoverflow.com/questions/2166433/why-ever-use-fields-instead-of-properties | https://stackoverflow.com/questions/653536/difference-between-property-and-field-in-c-sharp-3-0 – Rand Random May 18 '23 at 17:14
  • Yes, you can use `set` to add a new value, however the value *needs* to be of the type of the property (or at least implicitly convertible to it) – Hans Kesting May 18 '23 at 17:14
  • 1
    "what's the 'fun' of SET if I can't do that?" - not sure what your idea of 'fun' is, but you simply cannot have the type of `value` in your setter to be anything but the type of the data being set. In other words, `value` is already going to be a `double`. If you want to convert a string to a double, use a new method such as what `@HansKesting recommended. – h0r53 May 18 '23 at 17:17
  • 1
    could also be helpful https://stackoverflow.com/questions/1537272/convert-type-in-setter-method-possible-when-yes-how – Rand Random May 18 '23 at 17:20
  • or this: https://stackoverflow.com/questions/37408124/is-it-possible-to-call-a-property-setter-with-a-different-type-as-the-propertys – Rand Random May 18 '23 at 17:21

2 Answers2

3

C# is a strongly typed language so the code you show is just not permitted. The property is typed as a double? so it can ONLY accept numeric values (or a null). This means that you can't assign a string to that property at all.

But, you could do this:

    public double? low_bound_double { get; set; } = 0.0;
    public string low_bound
    {
      get { return low_bound_double.ToString(); }
      set { low_bound_double = double.Parse(value); }
    }

Then you would get two properties on your class; one is a double? and one is a string. In a sense, the property called low_bound_double is the actual variable while the string called low_bound is just a pair of converters that convert between string and double types.

Mark Ewer
  • 1,835
  • 13
  • 25
-1

Thanks a lot for answers, but I think I'll go with that:

public double? low_bound { get; set; }

public void SetParameterLowBound(string input_value)
{
    low_bound = Convert.ToDouble(input_value);
}

The 'old' setter method. Honestly don't understand why C# doesn't permit me do this with standard 'set' if it's literally it's duty

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • It does. https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/using-properties – SpicyCatGames May 19 '23 at 04:18
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 19 '23 at 04:18