3 Replies Latest reply on Apr 18, 2018 1:23 PM by Rick Davin

    Using an Enum

    alanR

      HI,

       

      I declared an enum ;

      private AFSummaryTypes _timeRangeMethod = AFSummaryTypes.None;

       

      I would like to reference _timeRangeMethod in ;

        public string TimeRange

              {

                  get { return _timeRange; }

                  set

                  {

                      if (_timeRange != value)

                      {

                          _timeRange = value;

                          if (_timeRange != null)

                              _timeRange = _timeRange.Trim();

                          SaveConfigChanges();

                      }

                  }

              }

      When i change _timeRange  to _timeRangeMethod , i get , cannot implicitly convert type 'OSIsoft.AF.Data.AFSummaryTypes' to 'string'

        • Re: Using an Enum
          tramachandran

          I do not completely understand your objective with the enum usage, but it is relatively easy to convert enums to strings and vice-versa

          Enum.ToString Method (): Converts the value of this instance to its equivalent string representation

          Enum.Parse Method (Type, String): Converts the string representation of the name or numeric value of enum

          1 of 1 people found this helpful
          • Re: Using an Enum
            Rick Davin

            Hi Alan,

             

            Let's provide some context to other readers here who may not have been following your posts over the past few weeks regarding writing a data reference.  To those who are unfamiliar with it, SaveConfigChanges() is a method used within custom data references.  Thus, it helps to let other readers know that your code snippet is defining a custom property inside of a custom data reference.

             

            My advice is that your property signature should be changed from  public string TimeRange to  public AFSummaryTypes TimeRange.  However, by doing so you would need to make a corresponding change in your ConfigString Setter around the code that reads a substring of the ConfigString, and then converts that substring to an AFSummaryTypes.  My colleague Thyagarajan Ramachandran has already given you a great link towards that end.  Grabbing a relevant section of code from another of your posts:

             

            I would change your original code here:

             

            case "timerangemethod":
                AFSummaryTypes summaryType;
                Enum.TryParse(keyValue[1], true, out summaryType);
                if (Enum.IsDefined(typeof(AFSummaryTypes), summaryType) | summaryType.ToString().Contains(","))
                    _timeRangeMethod = summaryType;
                else
                    _timeRangeMethod = AFSummaryTypes.None;
                break;
            

             

            To this:

             

            case "timerangemethod":
                if (!Enum.TryParse(keyValue[1], true, out _timeRangeMethod))
                    _timeRangeMethod = AFSummaryTypes.None;
                break;
            

             

            What I would most like to convey to you is that it is your duty as the developer of this custom data reference to take extreme care of composing the ConfigString based on associated properties (as determined by YOU), as well decomposing the ConfigString into those associated properties.  The recommendation is that such properties should be typed accordingly (e.g. AFSummaryTypes rather than string).

            3 of 3 people found this helpful