NI Home > Community > NI Discussion Forums

NI TestStand

Reply
Member
funin
Posts: 8
0 Kudos
Accepted Solution

Get element from container

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!

Member
SunRS
Posts: 199
0 Kudos

Re: Get element from container

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.

Member
funin
Posts: 8
0 Kudos

Re: Get element from container

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?

Member
SunRS
Posts: 199
0 Kudos

Re: Get element from container

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.

Member
funin
Posts: 8
0 Kudos

Re: Get element from container

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?

Member
SunRS
Posts: 199
0 Kudos

Re: Get element from container

 

 

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.

 

Member
funin
Posts: 8
0 Kudos

Re: Get element from container

Super! that one also works. Thanks a lot!