Dave
Dave

Reputation: 5644

Passing Argument To Method that Requires "ref System.Array"

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?

Upvotes: 1

Views: 3303

Answers (6)

Eldar
Eldar

Reputation: 5237

I also using Sterling Trader API. I use this code:

private structSTIPositionUpdate[] PositionList {
        get {
            Array arrayPos = null;
            _position.GetPositionList(ref arrayPos);
            return (structSTIPositionUpdate[]) arrayPos;
        }
    }

Upvotes: 0

Vinay Sajip
Vinay Sajip

Reputation: 99485

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.

Upvotes: 3

Martin Liversage
Martin Liversage

Reputation: 106916

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);

Upvotes: 2

Dewfy
Dewfy

Reputation: 23644

This works:

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

Upvotes: 0

Lee
Lee

Reputation: 144206

You can use Array.CreateInstance

Upvotes: 1

Juri
Juri

Reputation: 32920

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...

Upvotes: 0

Related Questions