• Advertisement
Sign in to follow this  

[.net] Using set and get in C Sharp

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
Unless I misunderstand you, I think you simply want to declare:

public static uint someNum;

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 this post


Link to post
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 this post


Link to post
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).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement