O'Reilly Forums: Chapter 5 - Do This P. 204 (Second Edition) - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

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

#1 User is offline   TJChap2840 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 04-May 11

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;
            }

        }
    }
}

0

#2 User is offline   MikeCost 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 22
  • Joined: 02-March 11

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.

This post has been edited by MikeCost: 05 May 2011 - 12:49 PM

0

#3 User is offline   TJChap2840 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 04-May 11

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
0

#4 User is offline   eaglecoug 

  • New Member
  • Pip
  • Group: Members
  • Posts: 4
  • Joined: 15-January 13

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.

This post has been edited by eaglecoug: 17 January 2013 - 05:47 PM

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users