3.1 Properties

Objectives

  1. Learn why we use properties

  2. Learn the syntax of writing a property

File Location

  • We will stay in the ClassesAndObjectsPractice project we created before and build off of that.

Description

Things to think about when writing a property:

  • Will it be public or private?

  • What data type will it use?

  • What will it be named?

  • Add the get and set accessors

Visualization

    public class House
    {
//       1      2      3           4
        public int squarefeet { get; set; }
    }
  1. Access Modifier

  2. Property DataType

  3. Name of Property

  4. Accessor

Accessor

Accessors are the second half of the syntax used when writing a property. It looks like this { get; set; }. This is a bigger topic you will learn more about later. For now, just know that these accessors are used to show that the private fields can be read, written, or manipulated.

When you are starting to write a property, a shortcut is: Type out prop then hit the tab key twice.

Sample Code

In the Donut.cs file, add these properties:

 class Donut
    {
        //Properties of the donut class
        public string Filling { get; set; }
        public int Price { get; set;}
        public string Type { get; set; }
        public bool IsSpecial { get; set; }
    }

In the Program.cs file, we've already instantiated 3 new donut objects (Doug's, Chris's and Nick's). Next we will define each of those donuts by assigning values for each property.

 class Program
    {
        static void Main(string[] args)
        {
            //Instantiating the objects
            Donut dougDonut = new Donut();
            Donut chrisDonut = new Donut();
            Donut nickDonut = new Donut();

            //Setting values to properties of objects
            dougDonut.Filling = "cherry";
            dougDonut.Price = 3;
            dougDonut.Type = "normal";
            dougDonut.IsSpecial = true;

            chrisDonut.Filling = "none";
            chrisDonut.Price = 2;
            chrisDonut.Type = "small";
            chrisDonut.IsSpecial = false;

            nickDonut.Filling = "chocolate";
            nickDonut.Price = 3;
            nickDonut.Type = "normal";
            nickDonut.IsSpecial = true;

            Console.WriteLine(nickDonut.Filling);
        }
    }

Discussion

  1. In our Donut class, we added 4 properties:

    • A Filling property that is public and will hold a string value

    • A Price property that is public and will hold an int value

    • A Type property that is public and will hold a string value

    • An IsSpecial property that ispublic and will hold a bool value (true or false)

  2. Then we moved to our Program file where we already have three objects: dougDonut, chrisDonut, and nickDonut

  3. We set values to these properties of the instantiated objects - i.e. nickDonut.Filling = "chocolate";

  4. You can test if these values were set by Console.WriteLine'ing the object's property Console.WriteLine(nickDonut.Filling);

Challenges

Bronze Challenge

  • Add a class called Hotel

  • Create three properties/variables:

    1. RoomNumber

    2. IsAvailable

    3. NumBeds

  • Create an object and give each property a value

Silver Challenge

  • Create a method that prints out the RoomNumber to a guest

  • For instance, "Your room number is C24".

Gold Challenge

  • Create a method that checks the availability of a room and prints the following:

    • "Yes sir (or miss, but NOT ma'am - no one wants to be called ma'am), room A23 is available." -> If it is available.

    • "I'm sorry sir (or miss), that room is not available." -> If it is not available.

Answers

Next: Methods

Last updated