copy a "Tag" array into a "C#" array

A forum devoted to the discussion of all topics having to do with scripting and other advanced programming using iX Developer.
Post Reply
ladin79
Posts: 19
Joined: Tue Nov 13, 2012 12:25 pm

copy a "Tag" array into a "C#" array

Post by ladin79 »

Hello,
how can i copy a "Tag" array into a "C#" array?
I tried so:
LocalFormArray = Globals.Tags.TagArray.Value;
LocalFormArray = Globals.Tags.TagArray[].Value;
LocalFormArray = Globals.Tags.TagArray.Values;
LocalFormArray = Globals.Tags.TagArray[].Values;

but none of these worked.

Thanks!

Greg M
Posts: 16
Joined: Tue Sep 08, 2015 1:32 pm

Re: copy a "Tag" array into a "C#" array

Post by Greg M »

Can you clarify exactly what you are trying to achieve and what type of array you are declaring in C#?

This will work but not sure if it's what you're looking for:

VariantValue[] cArray = new VariantValue[Globals.Tags.GlobalDataItems.Count];
int index = 0;
foreach (GlobalDataItem gdi in Globals.Tags.GlobalDataItems)
{
cArray[index++] = gdi.Value;
}

ladin79
Posts: 19
Joined: Tue Nov 13, 2012 12:25 pm

Re: copy a "Tag" array into a "C#" array

Post by ladin79 »

What I try to do, if possible, is to copy all the values contained in a Tag array
in a C# array, so declared:
float[] LocalFormArray = new float[8];
in just one code line.
For the moment I do so:
for (int i = 0; i < 8; i++)
{
LocalFormArray = Globals.Tags.TagArray .Value;
}
TagArrayDeclaration.png
TagArrayDeclaration.png (2.07 KiB) Viewed 20103 times

Greg M
Posts: 16
Joined: Tue Sep 08, 2015 1:32 pm

Re: copy a "Tag" array into a "C#" array

Post by Greg M »

If you're only doing it once, I can't see a better way to do it. If you are doing it multiple times you could create a function:

private float[] getArray(GlobalDataItem tag)
{
float[] resultArray;
try
{
resultArray = new float[tag.ArraySize];
for (int i = 0; i < tag.ArraySize; i++)
{
resultArray = tag.Value;
}
}
catch
{
resultArray = null;
}

return resultArray;
}

and call it like so:

float[] myArray = getArray(Globals.Tags.TagArray);

I think this should work.

ladin79
Posts: 19
Joined: Tue Nov 13, 2012 12:25 pm

Re: copy a "Tag" array into a "C#" array

Post by ladin79 »

Thanks a lot, that was the solution I was looking for.
About your previous post:

VariantValue [] = new CArray VariantValue [Globals.Tags.GlobalDataItems.Count];
int index = 0;
foreach (GlobalDataItem gdi in Globals.Tags.GlobalDataItems)
{
CArray [index ++] = gdi.Value;
}

can you do the same but only for GlobalDataItems which belong to a recipe?
Thanks!!

Greg M
Posts: 16
Joined: Tue Sep 08, 2015 1:32 pm

Re: copy a "Tag" array into a "C#" array

Post by Greg M »

This seems to work:

Tools.Recipe.RecipeItems ris = Globals.Recipe1.RecipeItems;
VariantValue[] cArray = new VariantValue[ris.Count];
int index = 0;
foreach (Tools.Recipe.RecipeItem ri in ris)
{
CArray[index++] = ((GlobalDataItem)ri.DataItem).Value;
}

ladin79
Posts: 19
Joined: Tue Nov 13, 2012 12:25 pm

Re: copy a "Tag" array into a "C#" array

Post by ladin79 »

Thanks for your help Greg, it works great!
I ask one more thing, if I can,
how can I create an event that will fire whenever a GlobalDataItam changes of value?
Thanks a lot in advance!

Post Reply