Iecstring 80 in a struct

Hello, I’ve made a struct in C#. If I am using this in a C# functionblock, it is necessary to call the string.ctor() to give it the 80 stringlength. After it is possible to write in the string. I have a situation now where I want to use a struct directly in IEC code. But I am unable to call the ctor function since the C# code is only a normal C# struct, with the system.Ie61131Lib and the IECtypes lib called (and the [Structure] attribute to make it visible in PLCNext.) And a struct does not have something like a constructor since it’s not a class. How am I able to call the ctor of the string? Initializing in C# is also not working, since the length is still zero. Arne

Hello Arne, i recommend implementing a ctor() method in your struct that runs all the ctors for the string elements. A struct just can not have a Default constructor. Then you call the struct.ctor() in the init method of your FB and you should be good to go. @ I have to check If it is possible to use the C# Struct directly in IEC without a FB. Try it like this: [Structure] public struct CWithString_andCtor { public short Code; public float Setpoint; public IecString80 strID; public void ctor() { strID.ctor(); } } kind regards, Oliver

I’m not sure if that’s solving the problem. [quote]@ I have to check If it is possible to use the C# Struct directly in IEC without a FB.[/quote] This is what I’m doing. I made a pointer (type is the struct) as InOut of my FB. Then I’m creating a global variable in PLCNext Engineer from the Type Struct. Then it’s possible to read all data all over the PLCNext program. Every FB has a string instanceName as input, which will be stored in the Struct. I check the length of that one to decide if ctor has to be called. In Execution, I’ve made a new: ownInit(). That function starts with: if (Inout->instanceName.s.maximumLength < 1). If so i’m calling ctor and write the instancename with .s.init (and other inits of the pointer can be set aswell). The pointer is connected to the data after the __init()… That’s why I’ve made my own at the beginning of the execution. Quite a workaround. Maybe not the best one, but it works :slight_smile:

I have the identical situation: - Struct defined in C# with an IECString80 member [Structure] public struct MyMessage { public IecString80 Device; public bool Result; } In PLC Engineer the Type MyMessage can be used, but the member Device cannot be written (because the ctor() was not called). For me both Solutions (ctor() and the length-check ) are a bit impraticable. Are there plans to fix this “issue”?

Hello beecksche, if I am correct all you would have to do is this: [Structure] public struct MyMessage { public IecString80 Device; public bool Result; public void ctor(){ Device.ctor(); } } and it should work in the Engineer already just like a IEC Struct. The ctor() will be called for every variable instance automatically at the initialisation of the variable by the IEC61131 Runtime.

Hi Oliver, this sounds great, thank you. I will check this next week. [EDIT]: Works perfectly! When I start coding, most of the time I follow the C# examples of your github repository. I didn’t found any hint for this ctor() implementation in structs. An update of this examples could help others as well.

Hello Beeksche, thats good to hear. You are absolutly right. We will be extending the C# documentation in the commig months. kind regards, Oliver