From 08c747f2ea3fbd118800eacee4ffd31144ff5320 Mon Sep 17 00:00:00 2001 From: ig Date: Wed, 1 Mar 2023 08:25:23 +0100 Subject: [PATCH] implement ToString for State --- csharp/Lib/Units/Percent.cs | 16 +++++----------- csharp/Lib/Units/State.cs | 10 +++++++++- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/csharp/Lib/Units/Percent.cs b/csharp/Lib/Units/Percent.cs index 02bf1a146..a358c438f 100644 --- a/csharp/Lib/Units/Percent.cs +++ b/csharp/Lib/Units/Percent.cs @@ -7,23 +7,17 @@ public readonly struct Percent public static String Unit => "%"; public static String Symbol => "%"; // ?? - public Percent(Decimal value) - { - if (value < 0) - throw new ArgumentException(nameof(Frequency) + " cannot be negative", nameof(value)); - - Value = value; - } - + public Percent(Decimal value) => Value = value; + // not generated public Decimal Value { get; } public override String ToString() => Value + Unit; // scalar multiplication - public static Decimal operator *(Decimal scalar, T t) => scalar * t.Value/100m; - public static Decimal operator *(T t, Decimal scalar) => scalar * t.Value/100m; + public static Decimal operator *(Decimal scalar, T t) => scalar * t.Value / 100m; + public static Decimal operator *(T t, Decimal scalar) => scalar * t.Value / 100m; // parallel - public static Percent operator |(T left, T right) => new T((left.Value + right.Value)/2m); + public static Percent operator |(T left, T right) => new T((left.Value + right.Value) / 2m); } \ No newline at end of file diff --git a/csharp/Lib/Units/State.cs b/csharp/Lib/Units/State.cs index af96735e0..bdd76d470 100644 --- a/csharp/Lib/Units/State.cs +++ b/csharp/Lib/Units/State.cs @@ -4,7 +4,13 @@ public readonly struct State { public IReadOnlyList Values { get; } - public State(IReadOnlyList values) => Values = values; + public State(IReadOnlyList values) + { + if (values.Any(v => v.Contains(";"))) + throw new ArgumentException("State values cannot contain the character ;", nameof(values)); + + Values = values; + } public State(params String[] values) : this((IReadOnlyList)values){} public State(params State[] states) : this(states.SelectMany(s => s.Values).ToList()){} @@ -15,4 +21,6 @@ public readonly struct State public static implicit operator State(String s) => new State(s); public static State operator |(State left, State right) => new State(left, right); + + public override String ToString() => String.Join("; ", Values); } \ No newline at end of file