AnsweredAssumed Answered

Comparing AF Value

Question asked by Steve Boyko Champion on Nov 21, 2017
Latest reply on Nov 23, 2017 by Eugene Lee

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

Outcomes