Noel Tompos
Noel Tompos

Reputation: 79

How to fix "Does not exist" when calling a static function from another class?

I want to call a static void function from another class, but it's said

The name [funcion name here] does not exist in the current context

Each class is in the same project, Framework 4.5.2 Its a public static void Function, in a public static class, don't see why it's not working.

The class where a function located, I want to call:

namespace Client.Modules
{
    public static class Login 
    {

        public static void Run()
        {
            // do something
        }
    }
}

The class where I want to call:

using Client.Modules;

namespace Client
{
    public class Main
    {
        Login.Run(); // here
    }
}

Upvotes: 3

Views: 605

Answers (1)

Konrad Rudolph
Konrad Rudolph

Reputation: 546123

public class Main
{
    Login.Run(); // here
}

That’s invalid: You can’t generally execute code outside methods. The only things that can go directly into classes are declarations. Put Login.Run() inside a method.

Upvotes: 12

Related Questions