Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
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
Add comment
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
1
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
Add comment · 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
0

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
Add comment · 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
0

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

No, you can't do that.

Comment
Add comment · 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.

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

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


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