tags:

views:

387

answers:

5

I'm using a third party library. One of the methods requires passing an array via ref that will be populated with some info. Here's the definition for the method:

int GetPositionList(ref Array arrayPos)

How do I construct arrayPos so that this method will work? In the library's not so complete documentation it defines the method as such:

long GetPositionList(structSTIPosUpdate() arrayPos)

I've tried this but of course I'm getting errors:

System.Array position_list = new System.Array();
sti_position.GetPositionList(ref position_list);

Any ideas?

A: 

This may help you: http://msdn.microsoft.com/en-us/library/szasx730%28VS.80%29.aspx

...although I don't understand why an object needs to have the "ref" keyword. Objects are passed by reference, so this should work anyway, without making use of the ref keyword. For arrays like int[] or string I understand this...

Juri
Because it won't just populate the array, it will create a new one and replace the caller's reference with a reference to the new array. This is not possible without the ref or out keyword, which allow you to pass a reference to the reference itself (normally you pass a copy of a reference).
Rytmis
+1  A: 

You can use Array.CreateInstance

Lee
A: 

This works:

Array position_list = new int[]{1, 3, 5, 5,6};    
sti_position.GetPositionList(ref    position_list);
Dewfy
+1  A: 

To create an instance of an Array you can use the CreateInstance method:

Array a = Array.CreateInstance(typeof(Int32), 10);
GetPositionList(ref a);

The type, dimension and size of the array is something the library author should document. The GetPositionList may be badly designed and simply create a new Array for you essentially meaning that the library author should have used out instead of ref. In that case you can use a null array:

Array a = null;
GetPositionList(ref a);
Martin Liversage
+3  A: 

This is the Sterling Trader Pro ActiveX API, right? Did you create an Interop dll using tlbimp.exe? The GetPositionList API expects an array which will hold structs of type structSTIPositionUpdate. Normally an out modifier is used if the callee initializes the passed-in data, and ref if the data is to be initialized. According to the meaning of the API the modifier should be out, so that this should work:

structSTIPositionUpdate [] entries = new structSTIPositionUpdate[0]; // or null
num_entries_returned = GetPositionList(ref entries);

Alternatively, try creating an array of these structs which is big enough to hold the expected number of entries, then pass it to the function:

structSTIPositionUpdate [] entries = new structSTIPositionUpdate[100]; // say
num_entries_returned = GetPositionList(entries);

Update: If you are getting type mismatches with System.Array, try

System.Array entries = Array.CreateInstance(typeOf(structSTIPositionUpdate), N);

where N is the number of elements in the array.

Vinay Sajip
@Vinay - yes, it's the Sterling API. I get this when I try to do what you've described:The best overloaded method match for 'SterlingLib.ISTIPosition.GetPositionList(ref System.Array)' has some invalid argumentsandArgument '1': cannot convert from 'ref SterlingLib.structSTIPositionUpdate[]' to 'ref System.Array'
Dave
Dave, you accepted this answer did the create instance work for you?Unfortunately I'm working in Sterling as well and would like to trade nsome notes on order entry
Brandon Grossutti