[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

return array from dll

Posted on 2016-09-15
13
Medium Priority
?
62 Views
Last Modified: 2016-09-15
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();
0
Comment
Question by:mastiSoft
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +1
13 Comments
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 41799542
>> 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.
0
 
LVL 1

Author Comment

by:mastiSoft
ID: 41799555
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?
0
 
LVL 1

Author Comment

by:mastiSoft
ID: 41799557
if I try:   return arr;
then the error : cannot implicitly convert ushort[] to ushort.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 35

Expert Comment

by:sarabande
ID: 41799595
if I try:   return arr;
the return type must be ushort[].

Sara
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 41799600
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
0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 41799606
Hi mastiSoft;

Change the signature of the function and variable in the function to this.
 public static ushort[] GetSamplTrace ( int m = 16 ) {
            ushort[] arr = new ushort[16];

Open in new window

0
 
LVL 1

Author Comment

by:mastiSoft
ID: 41799635
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)
0
 
LVL 1

Author Closing Comment

by:mastiSoft
ID: 41799644
simple and beautiful solution
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 41799671
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.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 41799985
@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".
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 41800007
I can't always remember small details - I trusted the asker when they said their code compiled despite using that construct.  :-(
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 41800018
Its OK Andy we all fall into that as well.
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 41800041
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.
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question