The book I'M reading "Head First C#" is trying to teach my about the use of properties, private vs public. But when viewing the following code, I still don't understand why the program is behaving the way it is.

The code shows 5 bags of feed no matter how many cows I have set in the numericUpDown1 value. But it looks like to me the line

BagsOfFeed = numberOfCows * FeedMultiplier; in the set property should set it back the way it should be anyway. There are the files.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Cow_Calculator
{
    public partial class Form1 : Form
    {
        Farmer farmer;
        public Form1()
        {
            InitializeComponent();
            farmer = new Farmer() { NumberOfCows = 15 };
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Console.WriteLine("I need {0} bags of feed for {1} cows", farmer.BagsOfFeed, farmer.NumberOfCows);
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            farmer.NumberOfCows = (int)numericUpDown1.Value;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            farmer.BagsOfFeed = 5;
        }
    }
}

Farmer class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Cow_Calculator
{
    public class Farmer
    {
        public int BagsOfFeed;
        public const int FeedMultiplier = 30;

        private int numberOfCows;
        public int NumberOfCows
        {
            get
            {
                return numberOfCows;
            }

            set
            {
                numberOfCows = value;
                BagsOfFeed = numberOfCows * FeedMultiplier;
            }
        }
    }
}

One last question. How in the world would I know that the creation of Farmer should be broken into two pieces?

Farmer farmer;

and then in the public Form1 method

farmer = new Farmer() { NumberOfCows = 15 };

And how would I know the first part needs to go within in
the From class and the second part needs to go within it's Form1 method just after InitializeComponent();

Thanks for any and all replies.

If you hit the set button and then you hit the calculate button it's going to set bags of feed to 5 (see your button2_click handler). In order for the program to change farmer.NumberOfCows on line 28 of the first file you must move the numeric up/down to a new value and not hit the set button. Otherwise you would undo what you just did.

In terms of the second issue, your Form1 is a class. Just like any other class it can have private variables. On line 14 you are declaring a Farmer variable that is a private member. You could, as far as I can tell, have combined lines 14 and 18 and left it where line 14 is (as Farmer farmer = new Farmer() {NumberOfCows = 15}; ). However, if you eliminated line 14 and tried to move the whole construct ( Farmer farmer = new Farmer() {NumberOfCows = 15}; )to line 18 farmer would be local to the Form1() constructor and invisible to the other methods which need to access it. The way you have it now allows the greatest flexibility and makes sure the farmer object is initialized only when Form1 is created.

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.