Creating Object instance dynamically using new Class

Questions regarding the use of the .NET SDK 2.0 for Server or Client development or integration into customer products ...

Moderator: uasdknet

Post Reply
jack_thomas
Hero Member
Hero Member
Posts: 32
Joined: 02 Sep 2016, 16:14

Creating Object instance dynamically using new Class

Post by jack_thomas »

Hi all,
I want to create instance of an Object using new class so that I do not have to write the code again and again. I have implemented the basic code way instance as follows:

Code: Select all

//class Lesson2NodeManager
private void CreateDeviceTypeAddessSpaceStructure()
{
...
CreateObjectSettings device1 = new CreateObjectSettings()
            //objSettings = new CreateObjectSettings()
            {
                ParentNodeId = devices.NodeId,
                BrowseName = new QualifiedName("Device_Instance1", InstanceNamespaceIndex),
                ReferenceTypeId = ReferenceTypeIds.Organizes,
                RequestedNodeId = new NodeId("Device_Instace1", InstanceNamespaceIndex),
                TypeDefinitionId = new NodeId(1003, TypeNamespaceIndex)                
            };
            CreateObject(Server.DefaultRequestContext, device1);
...
}
 
this works fine.
But I have created a new class DynamicInstances.cs

Code: Select all

 public class DynamicInstances: BaseNodeManager
    {
        #region Fields
        private CreateObjectSettings objSetting;
        #endregion

        public DynamicInstances(ServerManager server): base(server)
        { 
            
        }

        public void CreateNodeInstance(ObjectNode parentNodeId, string browseName, uint typeDefinitionNodeId)
        {
            objSetting = new CreateObjectSettings()
            {
                BrowseName = new QualifiedName(browseName, InstanceNamespaceIndex),
                ParentNodeId = parentNodeId.NodeId,
                ReferenceTypeId = ReferenceTypeIds.Organizes,
                RequestedNodeId = new NodeId(browseName, InstanceNamespaceIndex),
                //TypeDefinitionId = new NodeId((uint)1003, TypeNamespaceIndex)
                TypeDefinitionId = new NodeId (typeDefinitionNodeId, TypeNamespaceIndex)
            };
            Console.WriteLine();
            CreateObject(Server.DefaultRequestContext, objSetting);
        }
    }
The object of DynamicInstances class is instantiated from Lesson2NodeManager class creating composition relationship

Code: Select all

instance2 = new DynamicInstances(this.Server);
            instance2.CreateNodeInstance(devices, "MyInstance2", 1003);
code builds successfully but run time exception occurs 'BadNodeIdInvalid', I have debugged but could not find out the solution.

Please assist me in this issue.

Thanks a lot,
Jack
Thanks and best regards,
Jack

User avatar
Support Team
Hero Member
Hero Member
Posts: 3070
Joined: 18 Mar 2011, 15:09

Re: Creating Object instance dynamically using new Class

Post by Support Team »

Hello Jack,

The property InstanceNamespaceIndex must be valid. I.e. the value must be a NamespaceIndex that is managed by the NodeManager. You should asign the value with the return value of AddNamespaceUri.
Best regards
Unified Automation Support Team

jack_thomas
Hero Member
Hero Member
Posts: 32
Joined: 02 Sep 2016, 16:14

Re: Creating Object instance dynamically using new Class

Post by jack_thomas »

Dear Support Team Colleague,

Well, I have checked the InstanceNamespaceIndex and I knew that it was (uint) 2, so I have entered it manually, though it was not working.

#Solution
I then realized that in a new class instead using same server instance I have mistakenly evoked a new server instance. That was the bug.

#newDesign
in new class's constructor I have passes the server instance from NodeManager class and it worked.

It took couple of hours to explore this basic bug but worth learning. ;)

Thanks for posting your reply,
Jack
Thanks and best regards,
Jack

Post Reply