Union types?

Discussions about product bugs & problems!
Note: This is no replacement for the Official ETM Support!
Search

Post Reply
7 posts • Page 1 of 1
Smiffy
Posts: 29
Joined: Thu May 18, 2017 2:21 pm

Union types?

Post by Smiffy » Mon Apr 15, 2019 3:09 pm

Does CONTROL support union data types?

User avatar
leoknipp
Posts: 1712
Joined: Tue Aug 24, 2010 5:28 pm

Re: Union types?

Post by leoknipp » Mon Apr 15, 2019 3:54 pm

Probably you can use the CTRL datatype "anytype" to fulfill the requirements.

Best Regards
Leopold Knipp
Senior Support Specialist

Smiffy
Posts: 29
Joined: Thu May 18, 2017 2:21 pm

Re: Union types?

Post by Smiffy » Tue Apr 16, 2019 8:17 am

Don't think so. Union types are overlaid structures. They both exist at the same address.
For example the code:-
union conv
{
float fVal;
byte bVal[4]
} u1;

Declares a float and 4 bytes at the same address. So you can store a float or 4 bytes into the same address, and access either via the u1 variable.

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

Re: Union types?

Post by adaneau » Tue Apr 16, 2019 9:06 am

Hi,

Couldnt this be done using struct? Union and struct (besides memory use) are basically the same thing.

BR
Alexandre

Smiffy
Posts: 29
Joined: Thu May 18, 2017 2:21 pm

Re: Union types?

Post by Smiffy » Tue Apr 16, 2019 9:49 am

Indeed it can. I have done it now. I have used a dyn_anytype and mixed different structures in it.
However, as per normal with programming, when one door open another slams in your face.
I have a list of structures now stored in a dyn_anytype and would like to sort them alphabetically by a string member which is in both structure types.
Structure A has a string "name" as its first member. Stricture B also has a string "name" as its first member.
I want to sort the structure list (dyn_anytype) by the name field.
dynSort and dynDynSort seem the likely candidates, but they seem unhappy to sort by string types.

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

Re: Union types?

Post by adaneau » Tue Apr 16, 2019 11:54 am

Hi,
Maybe with a mapping instead of dyn_anytype? So you can use the string as key and sort it

BR
Alexandre

Smiffy
Posts: 29
Joined: Thu May 18, 2017 2:21 pm

Re: Union types?

Post by Smiffy » Tue Apr 16, 2019 4:00 pm

Sorted it now. Sort will work with string types. However, it won't work with structure members. The solution was to have a dyn_dyn_anytype. The lower "array" would contain string (the name) at index 1, then Structure A at index 2, and Structure B at index 3 (constituting the "entry"). This effectively makes indexes 2 and 3 a union type definition. This dynmaic any type (individual "entry") can then nested in another dynamic any type for the amount of entries required. Then the function dynDynSort(EntriesList, 1); can be used to sort the entries by the name (string).

Post Reply
7 posts • Page 1 of 1