• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
Question by starikcetin · Jun 27, 2015 at 11:49 PM · c#variableclassstatic

[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".

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

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;"

https://msdn.microsoft.com/en-us/library/sf0df423.aspx

Comment
starikcetin

People who like this

1 Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image starikcetin · Jun 28, 2015 at 01:06 AM 0
Share

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?

avatar image Wisteso · Jun 28, 2015 at 02:05 AM 1
Share

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.

avatar image starikcetin · Jun 28, 2015 at 02:16 AM 0
Share

Thanks for these useful informations.

Best wishes!

avatar image

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.

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image

Answer by DiegoSLTS · Jun 28, 2015 at 12:03 AM

No, you can't do that.

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

object assigns but does not show when added to list, or any other var 0 Answers

Multiple Cars not working 1 Answer

C# Accessing Variables from Class Variable 1 Answer

Distribute terrain in zones 3 Answers

Death Counter Resets When Reloading Scene. 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges