C# Classes

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2005
Posts: 30
Reputation: augie0216 is an unknown quantity at this point 
Solved Threads: 0
augie0216's Avatar
augie0216 augie0216 is offline Offline
Light Poster

C# Classes

 
0
  #1
Feb 26th, 2006
I'm doing this in visual C#.net.I'm a nwebie so bare with me.
I'm creating a class I need to declare three fields: name,phone number, and birthday (month,day, and year). I'm not sure how to declare them. Is this correct

private string name
private string phone number
private int birthday
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 483
Reputation: campkev is an unknown quantity at this point 
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: C# Classes

 
0
  #2
Feb 27th, 2006
can't have spaces so phone number needs to be changed to phoneNumber
also, you probably want to use DateTime instead of int for the birthday. Good luck and keep at it

  1. private string name;
  2. private string phoneNumber;
  3. private DateTime birthday;
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 30
Reputation: augie0216 is an unknown quantity at this point 
Solved Threads: 0
augie0216's Avatar
augie0216 augie0216 is offline Offline
Light Poster

Re: C# Classes

 
0
  #3
Feb 28th, 2006
Thanks for the help
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 483
Reputation: campkev is an unknown quantity at this point 
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: C# Classes

 
0
  #4
Feb 28th, 2006
anytime. Good questions normally get good answers around here.
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 275
Reputation: f1 fan is an unknown quantity at this point 
Solved Threads: 11
f1 fan f1 fan is offline Offline
Posting Whiz in Training

Re: C# Classes

 
0
  #5
Mar 5th, 2006
maybe consider using a property for each of these. It would appear that you will want something outside of your class to change the values so you dont want to make your fields public (or internal) as you have no control over the values in there.
To create a property you need a private field and a property for each as follows:
  1. private string _name;
  2. public string name
  3. {
  4. get
  5. {
  6. return _name;
  7. }
  8. set
  9. {
  10. //do some validation if you want
  11. _name = value;
  12. //do some other stuff if you need to based on the new value
  13. }
  14. }
You have full control when someone changes the value now.
I use them alot as private properties for just in time instantiation (the field is populated the first time the property is used so it isnt taking up memory.
Also you can make it read only by not using the set part of the property (leave it out altogether)
You can also have different accessors so the get part can be public and the set part can be private or internal.

Hope it helps you
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC