CODESYS - the IEC 61131-3 automation software

Welcome to the official CODESYS Forum by 3S-Smart Software Solutions GmbH | A member of the CODESYS Group
Deutsche Version English version russian version 
It is currently Mon Feb 24, 2020 9:28 pm

All times are UTC+01:00




Post new topic  Reply to topic  [ 18 posts ]  Go to page Previous 1 2
Author Message
PostPosted: Fri Jan 06, 2017 2:28 pm 
Offline
Frequent User
Frequent User

Joined: Fri Sep 02, 2011 8:02 pm
Posts: 312
I don't think your array initialization will ever work without a change from 3s. If you play around a bit, one of the precompile errors will be Cannot convert type 'SINT' to type 'MyObject'.

To clarify... when you try to initialize your array, you are defining what your objects should literally BE, not how they should initialize. Take for example:

Code:
MyArray : ARRAY[0..1] OF INT := [1, 2];


You are telling the compiler you want your integers to BE integers 1 and then 2.

If you do:

Code:
MyArray : ARRAY[0..1] OF MyObject := [1, 2];


You are telling the compiler that you want your MyObjects to BE integers 1 and then 2.

This is not the same as having your object instantiate with a value of 1 and then 2. I doubt CoDeSys can do what you are hoping for.

In fact, this works (unfortunately, the precompiler really wants me to use MyObject(0) - otherwise it complains I am not following the Fb_INIT):
Code:
VAR
   Obj1 : MyObject(1);
   Obj2 : MyObject(2);
   MyArray : ARRAY[0..1] OF MyObject(0) := [Obj1, Obj2];
END_VAR


But this means your objects have to be created first and then put into the array (not what you wanted).

Attachment:
array_init.png


If you are looking to simply have a unique ID with every instance, see this thread: viewtopic.php?f=11&t=7348


You do not have the required permissions to view the files attached to this post.

_________________
Scott Cunningham
KEB America, Inc.
www.kebblog.com
www.kebamerica.com


Top
   
PostPosted: Thu Apr 05, 2018 11:25 am 
Offline

Joined: Wed Apr 04, 2018 2:02 pm
Posts: 2
I hope some answers had given above have helped you with such a thing. Actually, I was trying to figure that out with CoDeSys for several times, but have called it a day afterwards. It's just because of lack of practice, because I'm new to programming, like new-new, I just have started with Java 2 months earlier. Now I have a brief confidence with fundamentals, by swotting all of them via these java tutorials https://explainjava.com/ They are written simple here, so it went out clear to me as to guys with no experience. But now I know for sure that CDS is not the very thing you need to try when you just have started practicing


Top
   
PostPosted: Tue Dec 17, 2019 7:11 pm 
Offline
Frequent User
Frequent User

Joined: Mon Apr 28, 2014 7:05 pm
Posts: 107
@Joan M

If anyone still looking for a way to achieve initialization of an array of objects through FB_Init, here's the syntax that worked for me:

MyObjectArray: ARRAY[0..3] OF MyObject[(1), (2), (3), (4)];

It is almost the same as Joan M tried in his exemple except that I have removed the " := " sign.

Enjoy.


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 18 posts ]  Go to page Previous 1 2

All times are UTC+01:00


Who is online

Users browsing this forum: andre_teprom, Cybot, I-Campbell and 10 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited