dpConnect on CTRL++

Discussion about recent product features & solutions!
Search

Post Reply
3 posts • Page 1 of 1
Victor.Ermakov
Posts: 11
Joined: Fri Oct 20, 2017 6:50 am

dpConnect on CTRL++

Post by Victor.Ermakov » Wed Apr 22, 2020 6:27 am

How to use dpConnect on CTRL++ ?

This Sample isn't work:

Code: Select all

struct TestStruct{
  string Name;
  TestStruct(string _Name){
      Name=_Name;
      DebugTN("TestStruct",Name);
      //Тест обработчика событий в классах
      if(dpConnectUserData("OnTestStruct",this,true,"Debug_ServerSOU")){
        DebugTN("TestStruct",getLastError());
      }
      if(dpConnect(this,"OnTest",true,"Debug_ServerSOU")){
        DebugTN("TestStruct",getLastError());
      }
  }
  void OnTest(string dp,anytype value){
    DebugTN("OnTest",Name);
  }
};
void OnTestStruct(TestStruct me,string dp,anytype value){
  DebugTN("OnTestStruct",me.Name);
}
global TestStruct TestStructG = new TestStruct("TestStructG");

main(mapping event)
{
    {

      TestStruct TestStructL = new TestStruct("TestStructL");
      return;
    }
}

Log output:
WCCOAui1:2020.04.22 09:24:04.853["TestStruct"]["TestStructG"]
WCCOAui1:2020.04.22 09:24:04.853["TestStruct"]["TestStructL"]

User avatar
adaneau
Posts: 212
Joined: Tue Feb 21, 2012 8:49 am

Re: dpConnect on CTRL++

Post by adaneau » Wed Apr 22, 2020 11:16 am

Hi,

Seeing some mistakes in your code so far:

- if you use a callback from inside your class, you should have this.OnTest in your dpConnect
- you should have dpConnect(this, this.OnTestStruct ...
- I wouldnt recommend to have a callback function out of the struct (OnTestStruct), especially if you use your calling class as parameter...
- In your main the object is immediately destroyed as it is not saved in any variable. To solve this you use a panel global mapping to save the newly created object.

For example of working code (first is in class lib, second is main):

Code: Select all

class someObject
{
    public someObject(bool somecondition)
    {
    if (somecondition == 0)
    {
      dpConnectUserData(this, this.cbValueChange, "whatever data", TRUE, "ExampleDP1");
    }
    else
    {
      dpConnect(this, this.cbValueChangeWithExtra,TRUE,"ExampleDP1", "ExampleDP2");
    }
  }

  public cbValueChangeWithExtra(string name1, float val1, string name2, float val2)
  {

//Do something
  }

  public cbValueChange(anytype whatever, string sDpName, float fValue)
  {
//do something
  }
};

Code: Select all

#uses "classes/someObject"

mapping mObjPtr;

void main()
{
  mObjPtr["obj1"] = new someObject(TRUE);
}
HOpe it will help

Best regards
Alexandre Daneau

Victor.Ermakov
Posts: 11
Joined: Fri Oct 20, 2017 6:50 am

Re: dpConnect on CTRL++

Post by Victor.Ermakov » Thu Apr 23, 2020 5:41 am

Thank you it's working

Post Reply
3 posts • Page 1 of 1