-1

Why can I not call another class's method with out parameter? For example:

class Program
{
    static void Main(string[] args)
    {
        int i =10;
        int j = OtherClass.Test(i);
    }
}

class OtherClass
{
    public static int Test(out int i)
    {
        i = 30;
        return i+15;
    }
}

I get "The best overloaded method match for 'ConsoleApplication2.OtherClass.Test(out int)' has some invalid arguments" error??
How can I do this? I need to call some general static methods with some out parameters.

Thanks

SethO
  • 2,703
  • 5
  • 28
  • 38
Moh Tarvirdi
  • 685
  • 1
  • 13
  • 25
  • 2
    `OtherClass.Test(out i);` Next time please use google or [msdn](http://msdn.microsoft.com/en-us/library/t3c3bfhx%28v=vs.71%29.aspx) first – Sriram Sakthivel May 28 '14 at 04:02
  • Oh my God!!!, forget it, Thanks, I confused between some complex implementations and forget a simple requirement, Thanks again – Moh Tarvirdi May 28 '14 at 04:07

2 Answers2

2

The caller program will be like below:

 class Program
 {
    static void Main(string[] args)
    {
       int i;
       int j = OtherClass.Test( out i);
    }
 }
Yogiraj
  • 1,952
  • 17
  • 29
2

You need to call your method like this:

int j = OtherClass.Test(out i);

You should also understand that by doing that you'll change the value of 'i' variable that you pass as a parameter to Test method.

More specifically: after execution of Test method the value of 'i' will be set to 30.

Oleg Gryb
  • 5,122
  • 1
  • 28
  • 40