NI TestStand

cancel
Showing results for 
Search instead for 
Did you mean: 

c# object reference

Hi,

I would create a simple library that use a object as reference stored on TestStand.

This is my code:

 

using NationalInstruments.TestStand.Interop.API;

namespace ClassLibrary1
{
public class Class1
{
public void InitLibrary(NationalInstruments.TestStand.Interop.API.PropertyObject p)
{
object classVal = 0;
p.SetValInterface("Locals.Test", 0, classVal);
}

public int AddValueMethod(NationalInstruments.TestStand.Interop.API.PropertyObject p,int a, int b)
{
object o = p.GetValInterface("Locals.Test", 0);
return a + b + Convert.ToInt32(o);
}
}
}

 

I create on TestStand a local variable "Locals.Test" as Object Reference.

 

but when I use it from TestStand I have this error:

-17500; Operation Failed.

An error occurred while calling .NET member "InitLibrary": Object of type 'ClassLibrary1.Class1' cannot be converted to type NationalInstruments.TestStand.Interop.API.PropertyObject

 

Where i wrong?

 

0 Kudos
Message 1 of 9
(4,151 Views)

By default, when you specify an object reference variable as an argument from TestStand (e.g. Locals.Test), that means to pass the reference it contains for the parameter. I suspect rather than passing SequenceContext for the second method call, you are perhaps trying to pass Locals.Test directly. If you wanted to do that you would need to change the code to use an empty string for the lookup string in the call to GetValueInterface and you would need to use the & operator when passing the argument (i.e. &Locals.Test) to indicate you want to pass the variable itself rather than the reference it contains.

 

One point I would make though is that you don't really have to be passing teststand data types at all. Why not just do this?

 

public void InitLibrary(out int classVal)
{
classVal = 0;
}

public int AddValueMethod(int myVal,int a, int b)
{
return a + b + myVal;
}

 

Or if you really want it to be type object then this:

 

public void InitLibrary(out object classVal)
{
classVal = 0;
}

public int AddValueMethod(object myVal,int a, int b)
{
return a + b + Convert.ToInt32(myVal);
}

 

Then on the TestStand side you can just specify the argument as Locals.Test rather than SequenceContext. In the first case you'd want it to be of type Number, while in the second example you would make it of type Object Reference.

 

Hope this helps,

-Doug

0 Kudos
Message 2 of 9
(4,134 Views)

Hi, I write this example for understand how write a library for teststand.
I have need create a library for comunication with the instrument (for example open on first time the ssh terminal and after use it several times) but I want that the code is implemented on my c# code. If I create a simple c# method when the measure is terminate I lost the Reference of my Class.
I thought that I could use the teststand global variables.
So I made this example

0 Kudos
Message 3 of 9
(4,128 Views)

You can use teststand variables. It should work like you are expecting. Based on the error you are getting, I suspect you just aren't passing the right argument to your methods from the TestStand side. If you can post some screenshots of how you have your method calls configured, maybe I can see if there is something wrong with how it is done.

0 Kudos
Message 4 of 9
(4,108 Views)

I attach the screenshot of mine settings and error. (the code is indicated above)

0 Kudos
Message 5 of 9
(4,086 Views)

I see the problem, you are storing the return value of the object constructor in Locals.Test. Remove Locals.Test from the constructor output item (just leave it blank). Really though, you could just make your methods static since you don't have any member data and then you wouldn't need a constructor.

 

Hope this helps,

-Doug

0 Kudos
Message 6 of 9
(4,070 Views)

Also, for the argument to your method you should be passing ThisContext, not Locals.Test. Keep in mind this is a bit of an unsual way to do things, which is what I was getting at in my first post, but it can work this way if that is what you want to do.

 

-Doug

0 Kudos
Message 7 of 9
(4,067 Views)

Hi, now I try with this code:

public Object InitLibraryObject()
{
int iClass = 3;
return iClass;
}

I changed the "NationalInstruments.TestStand.Interop.API.PropertyObject" with a "Object" and now work fine.

Is this a good way?

0 Kudos
Message 8 of 9
(4,060 Views)

I'm really not sure what you are trying to do. If you just want to return an integer, you don't need to use the Object data type, you can just use int and use a Number variable on the TestStand side to store it.

 

-Doug

0 Kudos
Message 9 of 9
(4,053 Views)