473,406 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Using set and get in C Sharp

complete
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
2 28208
vekipeki
229 Expert 100+
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
3,525 Expert 2GB
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

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

Similar topics

0
by: Vi | last post by:
Some of you might be interested in this open source AutoUpdater for ..NET "Sharp AutoUpdater provides an auto-update feature for .NET applications. Using XML configuration files, Sharp...
0
by: Eugene | last post by:
9rays.net Report Sharp-Shooter 1.3 has been published! Report Sharp-Shooter is the most flexible .NET report engine available on the market. It's a suite of 100% managed .NET components that...
4
by: Hai Nguyen | last post by:
I'm learning C sharp and do not like vb much. I'm creatiing a wepage using panel to test myself. I tried to use these code below, which is written in VB, and to transform them to c sharp but I got...
23
by: arthur.mcginty | last post by:
Man I've grown tired with C#/.NET the last three days I spent trying to put a NULL value in a database. C# initializes all uninitialized properties to 0 (for numeric types) and didn't have until C#...
20
by: windandwaves | last post by:
Hi Folk I am a PHP programmer, but I like to learn c-sharp as it seems to be in hot demand around here. My questions are: - how does c-sharp relate to PHP - do you like c-sharp and its...
2
by: hilz | last post by:
Hi group, I am starting with C Sharp, and want to build a desktop application. I come from the Java world, where there are Rich Client Platforms that make life easier by providing most of the...
2
by: LostnCode | last post by:
Hi, Can anyone help, I need to convert his code from vbscript to sharp C# for use with ASP.Net2.0? This is my first time using a forum. I don't know anything about either coding language so...
16
by: mike3 | last post by:
(I'm xposting this to both comp.lang.c++ and comp.os.ms- windows.programmer.win32 since there's Windows material in here as well as questions related to standard C++. Not sure how that'd go over...
4
by: hussain3047 | last post by:
Hi i am a beginner in C sharp and trying to develop a wince 5.0 device application using C sharp. i am using a thread in the Form1 Load method which is continously running a method named...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.