5

Is there any way to tell the compiler to use a static type instead of a variable when your project doesn't use namespaces?

For instance, I have a class called User with various static and non-static methods. Let's say one of the static methods is called GetUser().

I'm trying to call that User.GetUser() method from a method that also has a variable in scope (inherited from base class) called User. However, the compiler complains saying that it can't find User.GetUser() because it thinks I'm referring to the User variable that's in scope.

If this project used namespaces, I could just do ns1.User.GetUser(), but that's not feasible in this case. Is there a way that I can tell the compiler that I'm referring to the User type instead of the User variable?

1
  • 1
    ... Can you rename your variable? I generally recommend against using capitalized variable names. This is why. Commented Jan 14, 2013 at 18:51

4 Answers 4

9

You can use:

global::User.GetUser()

Or a using directive to alias the type:

using UserType = User;

...

UserType.GetUser();

I'd strongly encourage you to use namespaces though :)

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

Comments

6

Can you write global::User.GetUser()?

See global

Comments

4
  1. Use global::User.GetUser().

  2. Use an alias: using UserClass = User;

  3. Rename the Variable.

  4. Rename the type.

  5. Reduce the scope of the variable such that it's no longer in scope where you're using it.

Comments

2

Alternatively, you can use an alias for your static class. In your using directives, you can add:

using StaticUser = User;

Then there would be no more ambiguity.

Comments

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.