3 Replies Latest reply on Nov 23, 2017 2:24 AM by Eugene Lee

    Comparing AF Value

    Steve Boyko

      I need to compare an AFValue (from a PI tag's archive values) to a comparison value which may be a string or it may be a number. This is the code I wrote up to do the comparison, and it works, but I'm wondering if there is a better way. Any suggestions?

          ' Compares an AFValue against a string that may contain either text or a number

          ' Returns TRUE if they are equal, FALSE otherwise

          Private Function AFValueEquals(ByVal afv As AFValue, ByVal sComparison As String) As Boolean

              Try

                  If afv.ValueType = GetType(AFEnumerationValue) Then           ' PI digital state

                      Return afv.ToString().ToUpper = sComparison.ToUpper

                  ElseIf afv.ValueType = GetType(String) Then                   ' PI string tag

                      Return afv.ToString().ToUpper = sComparison.ToUpper

                  Else

                      If IsNumeric(sComparison) Then

                          If afv.ValueType = GetType(Int32) Then                ' PI int32

                              Return afv.ValueAsInt32 = CLng(sComparison)

                          ElseIf afv.ValueType = GetType(Single) Then           ' PI single

                              Return afv.ValueAsSingle = CSng(sComparison)

                          Else                                                  ' Assume a double

                              Return afv.ValueAsDouble = CDbl(sComparison)

                          End If

                      Else

                          Return False                                          ' Comparison value is not numeric, can't compare

                      End If

                  End If

              Catch ex As Exception

                  ' deal with the exception

                  Return False

              End Try

          End Function

        • Re: Comparing AF Value
          Eugene Lee

          Hi Steve,

           

          This seems to work fine for me. Let me know if there are any scenarios where it won't work.

          Function AFValueEquals(ByVal afv As AFValue, ByVal sComparison As String) As Boolean
                  Try
                      If afv.ValueType = GetType(AFEnumerationValue) Then
                          Return afv.ToString().ToUpper = sComparison.ToUpper
                      Else
                          Return afv.Value.ToString.ToUpper = sComparison.ToUpper
                      End If
                  Catch ex As Exception
                      ' deal with the exception
                      Return False
                  End Try
              End Function
          
          2 of 2 people found this helpful