# [.net] Using set and get in C Sharp

This topic is 3568 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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?

##### Share on other sites
Unless I misunderstand you, I think you simply want to declare:

What you referred to as a "static variable" is actually a static property with a backing variable. You can use that design if you like (it is often recommended in fact, for various reasons), but a variable declared as above will do the job if you're simply using the class for global storage.

Now if you just want a certain class to be accessible only statically (i.e. no instances can be created), then it's also advisable to declare it as such:

public static class SomeClass
{
// ...
}

##### Share on other sites
If you're using C# 3.0 (Visual Studio 2008) you do not need to declare the backing field for trivial properties - you can simply declare them thusly:
public int SomeNum { get; set; }
Public fields should not be exposed; even if your property is a trivial one you can at least add additional functionality at a later date without breaking code using your library. Properties also support different access modifiers for the getter and setter, and show up automatically in the PropertyGrid control.

##### Share on other sites
The point of get/set is so that you can refactor them more easily with additional behavior. Here is a common pattern:

class A{  public event EventHandler TextChanged;  string text;  public string Text  {    get { return text; }    set    {      if (value == text)        return;      text = value;            OnTextChanged(null);    }  }  protected virtual void OnTextChanged(EventArgs ea)  {    if (TextChanged != null)      TextChanged(this, ea);  }}

The 'static' thing was already described well enough by Fingon, so I won't go over that again. Events and properties can both be static if you want. Virtual functions can't (because virtual is only meaningful on instances of a class).

1. 1
Rutin
36
2. 2
3. 3
4. 4
5. 5

• 11
• 10
• 13
• 98
• 11
• ### Forum Statistics

• Total Topics
632974
• Total Posts
3009654
• ### Who's Online (See full list)

There are no registered users currently online

×