❔ How to use a variable across an entire class
I'm wanting to use the
client
var (at the end of Awake) in the Enter key case, so I can post a status when I press enter without redefining the variables every keypress. https://paste.mod.gg/byljqcyiagkv/0BlazeBin - byljqcyiagkv
A tool for sharing your source code with the world!
6 Replies
Class fields are what you want
class fields are:
- private
- usually prefixed with
_
or m_
So, if you'd also want the authClient
to be a field, then write it as: private AuthenticationClient _authClient
You have to specify the type of the field, var
cannot be used.
class fields can be public:
However, it is strongly advised to avoid public fields, as you allow other classes to change something in your class without you knowing it.
That is where properties come in. It is a wonderful feature of C#, which allows you to:
- see who references a given property
- control who can read the property (get
its value)
- control who can write to the property (set
its value)
- run extra logic before the value of a property is read or written to, e.g., log, filter and validate values, execute more complex logicLearn more here: https://learn.microsoft.com/en-us/dotnet/csharp/properties
Properties in C#
Learn about C# properties, which include features for validation, computed values, lazy evaluation, and property changed notifications.
And here is the official documentation of C# fields: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/fields
Fields - C# Programming Guide
A field in C# is a variable of any type that is declared directly in a class or struct. Fields are members of their containing type.
Remember, reading documentation is an essential skill for any developer (you really don't want to be dependent on StackOverflow ....)
Yup the Mastonet authentication docs were actually pretty intuitive once I looked at it
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.