Jump to content


Photo

Chapter 5 - Do This P. 204 (Second Edition)


  • Please log in to reply
3 replies to this topic

#1 TJChap2840

TJChap2840

    New Member

  • Members
  • Pip
  • 2 posts

Posted 04 May 2011 - 11:32 PM

I'm doing the Chapter 5 - Do This on p.204 regarding the Number of Cows encapsulation practice.

I have gone through the "Do This" but the program still isn't running as your books says it should. On page 205, the book says that I should be able to change my NumericUpDown control and it will change the output after I press the Calculate button. My program is not doing that and I have checked everything multiple times.

My program doesn't seem to be setting the value of "numberOfCows" to anything other than its default, even after I have changed that value through the NumericUpDown. I'm baffled. I'm familiar with C++ and I understand what the properities of "public int NumberOfCows" should be doing, but it does not seem to be doing what it is supposed to.

Here is my Form1.cs:

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 Chap5_DoThis
{
    public partial class Form1 : Form
    {
        Farmer farmer;

        public Form1()
        {
            InitializeComponent();

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

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

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

Here is my Farmer.cs

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

namespace Chap5_DoThis
{
    class Farmer
    {
        public int BagsOfFeed;
        public const int FEED_MULTIPLIER = 30;

        private int numberOfCows;

        public int NumberOfCows
        {
            get
            {
                return numberOfCows;
            }

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

        }
    }
}


#2 MikeCost

MikeCost

    Active Member

  • Members
  • PipPip
  • 22 posts

Posted 05 May 2011 - 12:48 PM

Your code looks okay to me. I even copied it (your posted code) into a new project and it worked fine. Is it possible your control names somehow changed?

I suggest starting a new project and class and copying/pasting the code into the appropriate sections. Maybe something got out of whack.

Edited by MikeCost, 05 May 2011 - 12:49 PM.


#3 TJChap2840

TJChap2840

    New Member

  • Members
  • Pip
  • 2 posts

Posted 05 May 2011 - 02:06 PM

Thanks for the reply. That's what I was going to try. I checked the control names during my troubleshooting and they matched. I'm not really sure what was going one, but if it works for you then it should work for me in a new project. :P

#4 eaglecoug

eaglecoug

    New Member

  • Members
  • Pip
  • 4 posts

Posted 17 January 2013 - 05:46 PM

Same for me, where I wrote exactly what the book says to write. When I run the program, nothing happens when I click either button.

Edited by eaglecoug, 17 January 2013 - 05:47 PM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users