繁体   English   中英

如何在Main()中使用方法(其参数已由ref传递)的返回值

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

我有以下C#代码:

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); 
    }
}

此示例代码引发了一些错误。

  1. “ inn”在当前上下文中不存在,
  2. 方法有一些无效的参数。

为什么是这样?

1:“ inn”在当前上下文中不存在,

您尚未在代码中的任何地方定义inn 应该是这样的:

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

或者,您可以从框架中获取inn ,例如:

Innovater inn = GetInnovaterFromTheFramework();

您的方法GetInnovaterFromTheFramework将在框架中返回对象。

ref关键字将参数传递给参数的方式是正确的,唯一的事情是inn在当前上下文中不存在。

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

Innovator inn = new Innovator();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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