How do I know if an AFValue.Value of type int is a SYSTEM digital state, or just a number?

Parents
  • Gregor has given a straight forward answer than anyone can use. However, since you tagged the post as "AF SDK", then a developer related answer would be:

     

    First of all, if the AFValue.Value is a SYSTEM digital state, then it is NOT an integer period, i.e. if you were to attempt a typeof(Int32) check, it would return false for a SYSTEM state. Rather, you would attempt to cast to an AFEnumerationValue, and check to see if that object is null. If so, it is not a SYSTEM or any digital state. But if it is not null, then it is a digital state.

     

    If the defined AFAttribute or PIPoint was declared to be some numeric type, then that digital state would be a SYSTEM state. If the AFAttribute or PIPont itself was defined as a digital object, then the digital state value is from the SYSTEM state set if IsGood is false. If IsGood is true, then the defined state set for the PIPoint is used.

     

    But if all you are dealing with is numeric AFAttributes or PIPoints, be it Int32, Single, or Double, then IsGood is a very quick clue as to whether the AFValue.Value is a SYSTEM state or not.

     

  • Yes, thanks both Rick and Gregor. It is good to know this extra info. But in this case I am dealing with AFAttributes pointing to a PIPoint of type bool. My code currently casts the AFValue.Value to (bool) and comes back "true" when the (new) point is "Pt Created". I read through the docs about PI system states and IsGood was not mentioned, at least where I was looking. (Also my brain says, its "Pt Created", nothing is amiss, its "good"!) Its nice to know there is an easy check in this case; all that type checking would get tedious.

Reply
  • Yes, thanks both Rick and Gregor. It is good to know this extra info. But in this case I am dealing with AFAttributes pointing to a PIPoint of type bool. My code currently casts the AFValue.Value to (bool) and comes back "true" when the (new) point is "Pt Created". I read through the docs about PI system states and IsGood was not mentioned, at least where I was looking. (Also my brain says, its "Pt Created", nothing is amiss, its "good"!) Its nice to know there is an easy check in this case; all that type checking would get tedious.

Children
  • Its amazing I can still learn new things this far down the road. Non-zero int as true I'm used to, but the part about casting string to bool is definitely quirky. Never realized that SYSTEM states were stored right in there as the Value. But it makes sense.

    So I'll look into creating a function like this and use it every time I read a AFValue. Thanks!

  • There are quirks with casting to Boolean. The rules are (1) if the value is numeric, then 0 is false and anything non-zero is true; and (2) if the value is a string, then an empty string is false but a non-empty string is true.

     

    Consider the state code for "Pt Created". If a string is used for the casting, it would be a true since "Pt Created" is not an empty string. If the integer code is used, it too casts as true since 253 is not 0.

     

    My advice is that your code needs to be more vigorous when dealing with Boolean AFAttributes and SYSTEM states.

     

    private void ExampleCode(AFValue pv)
    {
        var digitalState = pv.Value as AFEnumerationValue;
        if (digitalState != null)
        {
            string prefix = pv.IsGood ? "" : "SYSTEM ";
            Console.WriteLine($"{prefix}{digitalState.Name} [{digitalState.Value}]");
        }
    }