Link to home
Start Free TrialLog in
Avatar of mastiSoft
mastiSoft

asked on

return array from dll

hi.
I created dll that have to output an array from one of functions.
In program for dll no errors shows, but when I try to use the function in another program then it is impossible.
Here the code in dll:
  public static ushort GetSamplTrace(int m=16)
        {
            var arr = Array<ushort>.Empty(16)
            int pos;
            using (BinaryReader b = new BinaryReader(File.Open(dataPath, FileMode.Open)))
            {
                   pos = 29;
                    b.BaseStream.Seek(pos, SeekOrigin.Begin);
                    byte[] by = b.ReadBytes(2);
                    arr[0] = BitConverter.ToUInt16(by, 0);
                    for (int i=1;i<16;i++)
                    {
                        arr[i] = 0;
                    }

                return arr[16];
            }

        }

Here I try to use it:
                var arr = Array<ushort>.Empty(16);
                arr[16] =MyDll.fun.GetSamplTrace();
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

>> it is impossible.

Why?  What error message?


ps.  You return a ushort from the function but you talk of returning an array (NOT the same thing) in the question.
Avatar of mastiSoft
mastiSoft

ASKER

first problem was pointed to dll. return arr[16]
it seams that I try to return value from index 16 (it not exist because the size of array 16)
I changed code lite:
  public static ushort GetSamplTrace(string path, int size)
        {
            var arr = Array<ushort>.Empty(size);
            int pos;
            using (BinaryReader b = new BinaryReader(File.Open(dataPath, FileMode.Open)))
            {
           
                    pos = 29;
                    b.BaseStream.Seek(pos, SeekOrigin.Begin);
                    byte[] by = b.ReadBytes(2);
                    arr[0] = BitConverter.ToUInt16(by, 0);
                    for (int y=1;y<size;i++)
                    {
                        arr[y] = 0;
                    }

                    return arr[16];
            }

        }
No error but I know it will not work. How to return an array not only the one item from it?

Then when  I call the array from dll in another program how to do that?
if I try:   return arr;
then the error : cannot implicitly convert ushort[] to ushort.
if I try:   return arr;
the return type must be ushort[].

Sara
When you declare the function you declare what it will return, in this case you tell the compiler it will return a ushort.
public static ushort GetSamplTrace(string path, int size)

If you want to return an array you need to say so, try
public static Array<ushort> GetSamplTrace(string path, int size)
and then use return arr when the function is finished in the implementation.


ps.  Don't forget that arrays are zero based so an array with two members has, as valid, indexes of 0 and 1
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
To Andy:
now I have 2 errors not one as before
1. static types cannot be used as return type ( public static Array<ushort>GetSamplTrace(string path, int size)
2. cannot implicitly convert ushort[] to ushort. ( return arr)
simple and beautiful solution
Hmmm.
I suggested using Array<ushort> as the return because you had said your function compiled OK.
Trying it here the compiler objects to
          var arr = Array<ushort>.Empty(size);

so you would have to do as Sara first suggested and use ushort[] as the return type.
@Andy Array's are not generic types and you would get an error like this, " The non-generic type 'Array' cannot be used with type arguments".
I can't always remember small details - I trusted the asker when they said their code compiled despite using that construct.  :-(
Its OK Andy we all fall into that as well.
It didn't help that I would not use a collection class anyway for an array of a basic type.  Too much overhead for when a simple classical array would suffice, but it would have meant rather less changes to the code for the asker and, who knows, maybe they did require the functionality of a collection.