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

Recommended Answers

All 4 Replies

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

private string name;
private string phoneNumber;
private DateTime birthday;

Thanks for the help

anytime. Good questions normally get good answers around here.

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:

private string _name;
public string name
{
  get 
     {
       return _name;
      }
  set
    {
      //do some validation if you want
      _name = value;
      //do some other stuff if you need to based on the new value
    }
}

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.