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?

John Willemse
  • 6,608
  • 7
  • 31
  • 45
M.K
  • 171
  • 1
  • 12
  • You must allocate an Innovator and pass the Reference to the Docs Method. public static void Main () { Test t = new Test(); Innovator inn = new Innovator(); // or get an Innovator somewhere string file_name1 = t.Docs(ref inn); } – Benjamin May 03 '13 at 10:54

2 Answers2

3

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.

Habib
  • 219,104
  • 29
  • 407
  • 436
  • Thank you. I tried this but there is a problem. I can' t call Innovator constructor in c# it is allowed only in Javascript. This is respective to the application. – M.K May 03 '13 at 11:24
  • second, I can use Innovator inn = this.getInnovator() . But inside Main() 'this' is not allowed. Is' t it? – M.K May 03 '13 at 11:26
  • ummm What ?? how do you have class `Innovater` then ? – Habib May 03 '13 at 11:26
  • You can't use `this` inside a static method, – Habib May 03 '13 at 11:27
  • I have been calling it using 'this'. This is for first time I' m trying to call from method. – M.K May 03 '13 at 11:30
  • @user2314238, see http://stackoverflow.com/questions/1360183/how-do-i-call-a-non-static-method-from-a-static-method-in-c – Habib May 03 '13 at 11:34
  • @user2314238, just try `new Test().getInnovator();` in your Main – Habib May 03 '13 at 11:59
1

You need to declare an Innovator instance in main():

Innovator inn = new Innovator();

JeffRSon
  • 10,404
  • 4
  • 26
  • 51