NI TestStand

cancel
Showing results for 
Search instead for 
Did you mean: 

Get element from container

Solved!
Go to solution

I have created a container which contains numbers and strings elements. I would like to pass the element one by one to a vi. But I don't know how to extract the element respectively.

 

Is there a way to get the container's element one by one? Something like: Array[1], Array[i+1] will simply work for array, but what about container? I can't do Container[i]. It will return error.

 

Thanks a lot in advance!

0 Kudos
Message 1 of 9
(6,658 Views)

There are two options :

 

You can use an array of containers and use container[i].x

 

Other option is to create array of elments within a container .

Then you can use container.num[i] or container.str[i] to get the values.

 

Hope this helps.

0 Kudos
Message 2 of 9
(6,655 Views)

Yes I can use array of containers and use container[i].x  But how do I specify "x"? Can I use a loop index to stand for x then traverse all elements in a loop?

0 Kudos
Message 3 of 9
(6,653 Views)

Sorry i did not get your question correctly.

You want to read the elements within a container.

 

Please do the following : ( container name = cont)

 

Locals.num=RunState.Main.Locals.GetNumSubProperties("cont")  --- This gets the number of elements in your container

With this you can start a for loop for n sub elements.

 

In this loop - To get the each element name of the container use this :

Locals.str=RunState.Main.Locals.GetNthSubPropertyName("cont",Locals.i,0)

 

You can concat "cont." and locals.str to get the sub element name

Use this to get the value directly i.e. locals.x = cont.name

 

Hope this helps.

Message 4 of 9
(6,644 Views)

Hi 

By using your way I get the correct element number and element name. That's a great help. Thanks a lot!

 

But regarding the element value you were saying to use "locals.x = cont.name". The problem is that the name I get from "GetNthSubPropertyName" is a string. Then if I pass cont.name into locals.x, locals.x gets the string value "cont.name" rather than the real value of the container's element. It could be, in my case, a number or a string. 

 

Is there a kind of function like "GetNthSubPropertyValue" which could obtain the value of a local container, or do you have another way of achieving this?

0 Kudos
Message 5 of 9
(6,618 Views)
Solution
Accepted by topic author funin

 

 

Once you get container.element_name in a variable example : locals.tempstr ="cont.str1"

 

Use the below function to get the value.

 

Locals.str=RunState.main.Locals.GetValVariant(Locals.tempstr,0)

 

This works for both number and string - chosen variant for that purpose.

For numeric values you can use val(locals.str) to convert back to the numeric type.

 

 

There should be a way to get the type of variable.Will explore further and let you know.

 

Hope this helps.

 

0 Kudos
Message 6 of 9
(6,616 Views)

Super! that one also works. Thanks a lot!

0 Kudos
Message 7 of 9
(6,577 Views)

I am trying to access Array of Containers....How can I do that?

0 Kudos
Message 8 of 9
(3,515 Views)

Never mind...I used following step to do that - 

Locals.Value == RunState.Main.FileGlobals.GetValNumber("Name_Of_Array["+Str(index_value)+"]"+"Container_Name")

 

 

But please let me know if there is any other way...

0 Kudos
Message 9 of 9
(3,513 Views)