“Class-checking” a variable used to set a value inside a property C#

Say I have

public static class Foo
{
    internal static string Speed;
    internal static string Duration;

    public const string SPEED_0 = "0"; 
    public const string SPEED_1 = "1"; 
    public const string SPEED_2 = "2"; 
    public const string SPEED_3 = "3"; 

    public const string DURATION_0 = "0"; 
    public const string DURATION_1 = "1"; 
    public const string DURATION_2 = "2"; 
    public const string DURATION_3 = "3"; 


}

And in another class, made properties public for these variables
public static class AnotherClassSomewhere
{
    public static string Speed
    {
        get { return Foo.Speed; }
        set { if( value.??? ) Foo.Speed = value; }
    }

    public static string Duration
    {
        get { return Foo.Duration; }
        set { if ( value.??? ) Foo.Duration = value; }
    }

}

How can I ensure that for each property (i.e. Speed and Duration), they ONLY receive values from either Foo.SPEED_0/1/2/3 or Foo.DURATION_0/1/2/3 respectively.

I could just check for a range (e.g. between 0 and 3), given that I know the constant values ahead of time, but I want to force the use of those constants. Is there a way to check if they are from my Foo class?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Try use enum.

public static class Foo
{
    internal static string Speed;
    internal static string Duration;
}
public static class AnotherClassSomewhere
{
    public static string Speed
    {
        get { return Foo.Speed; }
        set { if (Enum.TryParse<SPEED>(value, out SPEED result)) Foo.Speed = result.ToString(); }
    }

    public static string Duration
    {
        get { return Foo.Duration; }
        set { if (Enum.TryParse<DURATION>(value, out DURATION result)) Foo.Duration = result.ToString(); }
    }

    private enum SPEED : String
    {
        SPEED_0 = "0",
        SPEED_1 = "1",
        SPEED_2 = "2",
        SPEED_3 = "3"
    }

    private enum DURATION : String
    {
        DURATION_0 = "0",
        DURATION_1 = "1",
        DURATION_2 = "2",
        DURATION_3 = "3"
    }
}

Method 2

Use enums and also store them as a string for your Serial library:

public static class Foo
 {
    internal static string SpeedString { get; private set; }
    internal static string DurationString { get; private set; }

    private static Speed _speed;
    public static Speed Speed
     {
        get => _speed;
        set
        {
          _speed = value;
          SpeedString = ((int)value).ToString();
        }
     }

   private static Duration _duration;
    public static Duration  Duration 
     {
        get => _duration;
        set
        {
          _duration= value;
          DurationString = ((int)value).ToString();
        }
     }
 }

public enum Speed
 {
 Speed0 = 0,
 Speed1 = 1,
 Speed2 = 2,
 Speed3 = 3
 }

public enum Duration
 {
 Duration0 = 0,
 Duration1 = 1,
 Duration2 = 2,
 Duration3 = 3
 }

If your string should also have other values then just numbers, you can also create a dictionary which assings a string to each enum value.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x