简体   繁体   English

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

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

I have the following C# code: 我有以下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); 
    }
}

This sample code is throwing some errors. 此示例代码引发了一些错误。

  1. 'inn' does' t exists in the current context, “ inn”在当前上下文中不存在,
  2. method has some invalid arguments. 方法有一些无效的参数。

Why is this? 为什么是这样?

1: 'inn' does' t exists in the current context, 1:“ inn”在当前上下文中不存在,

You haven't defined inn anywhere in your code. 您尚未在代码中的任何地方定义inn 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: 或者,您可以从框架中获取inn ,例如:

Innovater inn = GetInnovaterFromTheFramework();

Where your method GetInnovaterFromTheFramework would return you the object from the framework. 您的方法GetInnovaterFromTheFramework将在框架中返回对象。

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. ref关键字将参数传递给参数的方式是正确的,唯一的事情是inn在当前上下文中不存在。

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

Innovator inn = new Innovator();

暂无
暂无

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

相关问题 我如何传递变量名称,而不是它的值。 然后在传递后使用它的值? - How can I pass a variables name, not its value. Then use its value after it has been passed? 如何获取已传递给方法的类 - how can I get which class has been passed to method 如何从 Roslyn 的另一个方法中作为参数传递的方法获取返回类型? - How can I obtain return type from method passed as an argument in another method with Roslyn? 如何使用Tuple &lt;&gt;返回值,而不是在方法中使用&#39;out&#39;参数? - How can I use Tuple<> to return value instead of using 'out' argument in method? 如何在另一个方法中使用 main() 方法中定义的值 - How can i use a value defined in the main() method in another method 如何创建一个方法,其参数是一个函数,其参数可以是来自扩展给定类的任何类的对象? - How can I create a method whose argument is a function whose argument can be an object from any class extending a given class? 犀牛如何模拟返回传入参数的方法? - How can I rhino mock a method that returns the passed in argument? 设置方法以返回作为参数传递的函数的返回值 - Setup a method to return the return value of a function passed as an argument 如何使用NUnit测试带有out或ref参数的方法? - How can I use NUnit to test a method with out or ref parameters? Rhino Mocks - 通过多次调用模拟返回值发生变化(即使传递相同参数时)的方法 - Rhino Mocks - mocking a method whose return value changes (even when passed the same parameter) with multiple calls
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM