0

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?

4

2 回答 2

3

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在当前上下文中不存在。

于 2013-05-03T10:53:49.743 回答
1

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

Innovator inn = new Innovator();

于 2013-05-03T10:54:47.693 回答