943,962 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 1987
  • C# RSS
Feb 26th, 2006
0

C# Classes

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
augie0216 is offline Offline
30 posts
since Mar 2005
Feb 27th, 2006
0

Re: C# Classes

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

C# Syntax (Toggle Plain Text)
  1. private string name;
  2. private string phoneNumber;
  3. private DateTime birthday;
Reputation Points: 14
Solved Threads: 19
Posting Pro in Training
campkev is offline Offline
484 posts
since Jul 2005
Feb 28th, 2006
0

Re: C# Classes

Thanks for the help
Reputation Points: 10
Solved Threads: 0
Light Poster
augie0216 is offline Offline
30 posts
since Mar 2005
Feb 28th, 2006
0

Re: C# Classes

anytime. Good questions normally get good answers around here.
Reputation Points: 14
Solved Threads: 19
Posting Pro in Training
campkev is offline Offline
484 posts
since Jul 2005
Mar 5th, 2006
0

Re: C# Classes

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:
C# Syntax (Toggle Plain Text)
  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
Reputation Points: 26
Solved Threads: 11
Posting Whiz in Training
f1 fan is offline Offline
275 posts
since Jan 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: C# VS2005 - Installing deployed application to an specif path
Next Thread in C# Forum Timeline: Opening a notepad file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC