2 Replies Latest reply on Apr 16, 2014 11:11 PM by neilg

    PIProperty.Add specify propertytype in Module DB


      Hello fellow people of the world,


      I am attempting to add a PIProperty under a root PIProperty using the .Add method. The parameters for the Add method only allow for the name and value. What I also need is the ability to specify the type.


      Does anyone have any quick samples to show how to specify the Data Type for the PIProperty? I have looked on VCampus and techsupport but haven't found anything concrete yet.




      Sample code:


      Dim rootProperty as PISDK.PIProperty


      ' Get the root property from the module db - this is a long piece of code and I am excluding it from here


      rootProperty.PIProperties.Add(name, value)

      By default all adds end up as a String and I need to add properties with the following types as well:




      NOTE: I am aware that we should not be using ModuleDB much but this is a special case where the client does not have AF yet.

        • Re: PIProperty.Add specify propertytype in Module DB
          Sam Pride

          G'Day Neil,


          If you wish you change the value type of a PIProperty, you can do so by passing a VARIANT to the value. The Data Type of the Variant determines the DataType of the Property.


          Here's some sample code: 

          'This example creates a "TestProperty" with a string value of 2 under %OSI. Subsequent calls to it will change the value '2' from string to long, if the value is 'string', and from 'long' to 'string', if the value is 'long
          Private Sub CmdChangePropertyTypeSilent_Click()
              Dim TestRoot As PIProperties
              Dim Property As PIProperty
              Dim varValue As Variant
              Dim varValue2 As Variant
              On Error GoTo Handler
              Set TestRoot = PISDK.Servers.Item("localhost").PIModuleDB.PIModules("%OSI").PIProperties
              On Error Resume Next
              'Create TestProperty under %OSI
              Set PIProperty = TestRoot.Add("TestProperty", "2")
              If Err.Number = -2147220447 Then
                   Set PIProperty = TestRoot.Item("TestProperty")
              ElseIf Err.Number <> 0 Then
                   GoTo Handler
              End If
              On Error GoTo Handler
              If Err.Number = 0 Then
                   varValue = PIProperty.Value
                   If IsNumeric(varValue) Then
                       If TypeName(varValue) = "String" Then
                            'Change the type to Integer
                            varValue2 = CLng(varValue)
                            'Change the type to a String
                            varValue2 = CStr(varValue)
                       End If
                       PIProperty.Value = varValue2
                   End If
              End If
              Exit Sub
              MsgBox "Error " & Err.Number & ": " & Err.Description
          End Sub



          (this is new to me, my assumption would have been to use ModifyAttributes, so it turns out I learnt something today. And I've only had 1 coffee!!)