By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,883 Members | 1,952 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,883 IT Pros & Developers. It's quick & easy.

Using set and get in C Sharp

complete
P: 11
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:
Expand|Select|Wrap|Line Numbers
  1. public static uint Somenum
  2. {
  3.    set { m_somenum = value; }
  4.    get { return m_somenum; }
  5. }
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:
Expand|Select|Wrap|Line Numbers
  1. public clase SomeClass
  2. {
  3.     SomeClass someclass = new SomeClass();
  4.  public static uint Somenum
  5.  {
  6.     set { m_somenum = value; }
  7.     get { return m_somenum; }
  8.  }
  9. }
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?
Jan 13 '09 #1
Share this Question
Share on Google+
2 Replies


vekipeki
Expert 100+
P: 229
Please use CODE tags when posting. You should read the Posting guidelines before posting. Although the link seems to be broken right now? :)

The problem is that you are accessing a non-static field in a static property. It your property must be static, the your backing field must also be static.

Expand|Select|Wrap|Line Numbers
  1. private static int _number;
  2. public static int Number
  3. {
  4.      get { return _number; }
  5.      set { _number = value; }
  6. }
  7.  
Note that the _number field is private and static, accessible only withing the class containing it, while Number field is public, and exposed to the rest of the world.

For example, by using only a 'get' accessor, you can make your property read-only:

Expand|Select|Wrap|Line Numbers
  1. private static int _number;
  2. public static int Number
  3. {
  4.      get { return _number; } // there is no 'set' accessor
  5. }
  6.  
Jan 14 '09 #2

tlhintoq
Expert 2.5K+
P: 3,525
Get and Set can also be used to map a variable in one form or chunk of code to something else. In this example a form uses a local variable for preferences, but it is really just passing through the program level preference object (PrefObj)
Expand|Select|Wrap|Line Numbers
  1.         PrefObj myPreferences { get { return Program.myPrefs; } }
  2.  
Instead of using 100 references to Program.myPrefs in this form we only make one. The form only has references to the local variable 'myPreferences'. If you decide later that the preferences will come from someplace else, you only have to make 1 code change, not 100.

Get and Set also can run code. They can be used for range checking and help avoid nulls being passed around
Expand|Select|Wrap|Line Numbers
  1.  
  2. string bob = string.empty;
  3.  public string RangeCheck
  4.         {
  5.             get 
  6.             { 
  7.                 if (string.IsNullOrEmpty(bob)) return "Not yet set.";
  8.                 else return bob;
  9.             }
  10.             set { bob = value; }
  11.         }
  12.  
An empty field in a form looks like you are missing data. A field that clearly states "Not set" doesn't look like a bug.

You could apply this to integers as well. If someone tries to set a value to 500, but your program expects or requires a maximum of no more than 100...
Expand|Select|Wrap|Line Numbers
  1.  
  2. int bob = 0;
  3.  public int RangeCheck
  4.         {
  5.             get 
  6.             { 
  7.                 return bob;
  8.             }
  9.             set 
  10.             { 
  11.                   if (value > 100) bob = 100;
  12.                   if (value < 1) bob = 1;  
  13.              }
  14.         }
  15.  
Or maybe you want your code to be smart enough to actually react to values being set. In this example, when the value is set we also run the method that saves our values then we update our display.
Expand|Select|Wrap|Line Numbers
  1.         public string RangeCheck
  2.         {
  3.             get 
  4.             { 
  5.                 if (string.IsNullOrEmpty(bob)) return "Not yet set.";
  6.                 else return bob;
  7.             }
  8.             set { bob = value; SaveMethod(); UpdateDisplay(); }
  9.         }
  10.  
Now our code is a little smarter and a lot easier to maintain. We don't have to call the save and update methods from all 100 places we set this value. Just setting the value will do it for us, every time, every place. Giving us consistent behavior with less work.
Jan 14 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.