简体   繁体   中英

How can I use a method's (whose argument has been passed by ref) return value in Main()

I have the following C# code:

public class Test 
{ 
    public string Docs(ref Innovator inn) ///Innovator is an Object defined in the   framework of the application
    {  
        //// some code 
        string file_name = "filename";
        return file_name;
    }  

    public static void Main ()/// here I' m trying to use the above method' s return value inside main()
    {
         Test t = new Test();
         string file_name1 = t.Docs(ref inn); 
    }
}

This sample code is throwing some errors.

  1. 'inn' does' t exists in the current context,
  2. method has some invalid arguments.

Why is this?

1: 'inn' does' t exists in the current context,

You haven't defined inn anywhere in your code. It should be like:

Test t = new Test();
Innovater inn = new Innovator(); //declare and (instantiate)
string file_name1 = t.Docs(ref inn); 

Or You can get the inn from the framework something like:

Innovater inn = GetInnovaterFromTheFramework();

Where your method GetInnovaterFromTheFramework would return you the object from the framework.

The way you are passing the argument to the parameter with ref keyword is right, the only thing is that inn doesn't exist in the current context.

您需要在main()中声明一个Innovator实例:

Innovator inn = new Innovator();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM