- Home /
[C#] Is there a way to call static variables directly, independent from parent class?
For example, if you have a static class that is named UtilityClass that has a public method named MethodA, you call the method as shown in the following example:
UtilityClass.MethodA();
(Taken from: Static Classes and Static Class Members (C# Programming Guide))
But, is there a way to call that variable "directly" like MethodA()
without adding UtiliyClass.
at beginning?
Thanks in advance.
Edit: I asked for variables, but i used a method for example above. Sorry about that. I mean this:
public static class A
{
public static int B;
}
I know i must call it like "A.B" but i want to use it directly as "B".
Answer by Wisteso · Jun 28, 2015 at 12:39 AM
It sounds like you're looking for the ability to "static import" a class. C# 6.0 will have this but Unity will not have that for some time.
http://stackoverflow.com/questions/7692826/static-imports-in-c-sharp
In the mean time you can use an alias to at least shorten the size... e.g. "using Diag = System.Diagnostics;"
Sorry for my mistake, i edited the question. So does "static import" also cover variables?
http://stackoverflow.com/a/24634374 -> this is what we are talking about, right?
Correct. Though I'm not sure if it covers static variables. It should... but since the feature is still part of a preview version its hard to say.
In Java, it does cover variables (members). http://javarevisited.blogspot.com/2012/10/what-is-static-import-in-java-5-example-tutorial.html
For now though, using an Alias should support anything since its basically just an alias to the namespace.
Answer by Baste · Jun 28, 2015 at 12:05 AM
No, except inside the class with the static method, or a subclass.
Whatever language you're using, it needs to know where the names of things comes from. I guess something like using-directives for individual methods could've existed, but that's not the case.
Your answer
Follow this Question
Related Questions
I´ve got a problem with my Coin System. It always shows 1 Coin. 2 Answers
Couroutines not working in static class 1 Answer
Access to a variable inside a C# class 2 Answers
how to acess Static variable in other scripts without extended functions? 2 Answers
Can't access a javascript static variable from c# script 1 Answer