Page 1 of 1

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

Posted: Thu Sep 24, 2015 8:14 am
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!

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

Posted: Wed Sep 30, 2015 3:31 am
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;
}

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

Posted: Thu Oct 01, 2015 12:51 am
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 19719 times

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

Posted: Mon Oct 05, 2015 6:34 am
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.

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

Posted: Tue Oct 06, 2015 2:55 am
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!!

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

Posted: Tue Oct 06, 2015 8:01 am
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;
}

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

Posted: Fri Oct 09, 2015 3:26 am
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!