0

I want to use a function from another class within a new function which I will call from main. I am trying to do this as below, but get an error:

Error The name 'Class1' does not exist in the current context.

Actually, in my code I use different names, but its just to illustrate the structure and to make it easier to read for you.

public class Class1
{      
    public static int[] Function1()
    {
       // code to return value
    }
}


public class Class2
{ 
      public static int Function2()
      {
         int[] Variable = Class1.Function1();
         //other code using function1 value
      }
}
3
  • Are they in the same namespace? Same assembly? Commented Jun 26, 2012 at 2:08
  • Yeah- namespace was it, thanks. Commented Jun 26, 2012 at 2:27
  • 2
    Dang, I should have posted my answer first :) Commented Jun 26, 2012 at 2:32

1 Answer 1

5

Actually, in my code I use different names, but its just to illustrate the structure and to make it easier to read for you.

Unfortunately you've made it so easy to read that you have eliminated the problem entirely! The code you posted does not contain an error and is perfectly valid.

The error message is very clear; from wherever you are actually calling the code, "Class1" (or whatever it may be) is not in scope. This may be because it is in a different namespace. It may also be a simple typo in your class name. Does your code actually look something like this?

namespace Different 
{
    public class Class1
    {      
        public static int[] Function1()
        {
           // code to return value
        }
    }
}

namespace MyNamespace
{    
    class Program
    { 
          static void Main(string[] args)
          {
              // Error
              var arr = Class1.Function();

              // you need to use...
              var arr = Different.Class1.Function();
          }
    }
}

That's the best I got until you post the actual code.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, the namespace closing brace was between the two classes- thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.