-3

I found a code online which i'm using in my Program, but to my surprise i found out there was a variable/function declared twice...

Now, If i'm to send any value to the DLL, which of the two would i send info to? One use out, while the second does not... See Code:

[DllImport("msman.dll", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi, ExactSpelling=false)]
        public static extern bool receive(int ID, double[] Bid, double[] Ask);

        public bool receive(int ID, out double first, out double second)
        {
            bool flag;
            double[] Array1st = new double[1];
            double[] Array2nd = new double[1];
            if (Form1.receive(ID, Array1st, Array2nd))
            {
                first = Array2nd[0];
                second = Array1st[0];
                flag = true;
            }
            else
            {
                second = 0;
                first = 0;
                flag = false;
            }
            return flag;
        }

And, why is it possible to declare two variables..

CollinD
  • 7,304
  • 2
  • 22
  • 45
ThisDude
  • 111
  • 2
  • 9
  • 2
    Although the names are the same, the parameters are not. That's how the compiler knows which one to use. – Lee Taylor Sep 25 '15 at 22:48
  • Do you mean i can send values to either of them? – ThisDude Sep 25 '15 at 22:50
  • 1
    possible duplicate of [What Does "Overloaded"/"Overload"/"Overloading" Mean?](http://stackoverflow.com/questions/289997/what-does-overloaded-overload-overloading-mean) – GSerg Sep 25 '15 at 22:51
  • 1
    @user2864740 I'm not sure `static` for valid overloading if the signature was otherwise identical. – BradleyDotNET Sep 25 '15 at 22:52
  • @GSerg, you're mistaken... This is not a duplicate of that link as they both request for two different things... – ThisDude Sep 25 '15 at 23:19

3 Answers3

6

My C# isn't great, but this looks like a standard case of method overloading.

Note the signature for each method

A: public static extern bool receive(int ID, double[] Bid, double[] Ask);
B: public bool receive(int ID, out double first, out double second)

A takes the following parameters: int, double[], double[] while B takes int, double, double. Note the difference in types. So when you call it, the compiler says "oh, you want to call receive with an int and two double arrays. Got it, here you go!" and serves up A

Example of how the call works:

int x = 1; double y = 1.0; double z = 2.0;
receive(x, y, z); // <-- this calls the second method (B).

int x = 1; double[] y = new double[1]; double[]z = new double[1];
y[0] = 1.0;
z[0] = 1.0;
receive(x, y, z); // <-- this calls the first method (A)
CollinD
  • 7,304
  • 2
  • 22
  • 45
  • Yes, absolutely. That's a straight case of method overloading. – Rahul Sep 25 '15 at 22:52
  • Wow!, your explanation rocks.. Coz, I'm looking for a way to send value to the function, which has been disturbing me and mind since... – ThisDude Sep 25 '15 at 22:54
1

Methods can be overloaded, that is, they can have the same name as long as the parameters differ (overloading on return type only is not allowed).

Hence this is valid:

void Foo(int x) { }
void Foo(char x) { }

Or, in your case:

bool receive(int ID, double[] Bid, double[] Ask);
bool receive(int ID, out double first, out double second)

Note that this is a standard language feature in many other languages, including C++.

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
1

Method overloading is object oriented concept where method can have same name and only parameters(signiture of method) are different.

  using System;

  class Test
  {
    static void receive(int x, double y)
    {
      Console.WriteLine("receive(int x, double y)");
    }

    static void receive(double x, int y)
    {
      Console.WriteLine("receive(double x, int y)");
    }

    static void Main()
    {
      receive(5, 10.2);
    }
 }

See here https://msdn.microsoft.com/en-us/library/aa691131(v=vs.71).aspx

Mohini Jamdade
  • 478
  • 1
  • 6
  • 17