What is the point of using set and get in C Sharp?
It seems variables are used differently in this language than in C++.
For some reason, you have to have a static variable defined like this:
public static uint Somenum
{
set { m_somenum = value; }
get { return m_somenum; }
}
and prior to this declaration, you need to have this:
public uint m_sumenum;
This seems to be the only way to expose a member of a class to other classes in C#.
The problem is that I seem to be doing this improperly because I get a compile error:
An object reference is required for the non-static field, metod, or property '.......m_somenum"
I think I see the problem. The problem is that I cannot use a static varable like this.
So you have to instantiate the class in order to set these members of the class.
So how would you do the equivalent of a global class in C Sharp?
Would I do it something like this:
public clase SomeClass
{
SomeClass someclass = new SomeClass();
public static uint Somenum
{
set { m_somenum = value; }
get { return m_somenum; }
}
}
Or perhaps this "new" needs to be outside of the class in order to work. So my next question is this. How and where would that command be such that it the internal set methods could be accessed by the other classes in the code?
Not really - firstly it would only be static if the variable was static - eg it was going to be on a per class basis, not a perinstance, that and you dont *HAVE* to use get and set properties to access it, you could just make it public so you would either have a
In this instance you could make 5 copies of "SomeClass" and 1 counter is shared between them all (ACounter) and 1 counter is per instance (OtherCounter)
so, you dont *need* to but its good practice on the simple grounds that it saves changing it later, and protects your variables if you wish from change.. and you can add validation etc..
In short your initial code with a static property for a instance based private variable fails, because the private variable isnt instanced so the static class cant use it.
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.