O'Reilly Forums: A Day At The Races - Having A Lot Of Trouble - O'Reilly Forums

Jump to content

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

A Day At The Races - Having A Lot Of Trouble

#1 User is offline   TomB360 

  • New Member
  • Pip
  • Group: Members
  • Posts: 5
  • Joined: 02-April 14

Posted 07 August 2014 - 11:54 AM

I've worked through the book up to the first lab - A Day at the Races - and I'm stuck. I haven't got a clue where to start, all I've got is the code snippets from the book. Is it supposed to be that hard, or am I missing something? Do I need to go back and re-read the chapters I've already done?

This is the first time learning a programming language and I felt like I was doing ok up until this point.

#2 User is offline   AndrewStellman 

  • Andrew Stellman
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 787
  • Joined: 08-October 08
  • Gender:Male
  • Location:Brooklyn, NY
  • Interests:Author of: "Head First C#", "Beautiful Teams", "Head First PMP", "Applied Software Project Management"

Posted 08 August 2014 - 06:11 AM

Here are a few tips.

A good first step is adding the empty classes and setting up the form.

Gill in the fields and methods in the classes on pages 190 and 191 -- for methods that return a value like PlaceBet() and PayOut(), return a dummy value for now.

Then see if you can get your dogs to run across the form when you click the Race button. This is going to use a Timer, exactly like you did with LabelBouncer at the end of Chapter 4.

Now that you've got some code filled in, really go back to the class diagrams and see if you can figure out what's going on.

If you're still having trouble, you should definitely feel comfortable going back to past exercises and doing them again -- but this time, don't look at the answer at all while you're trying to solve them. There might just be one or two small conceptual roadblocks, and taking off the training wheels is a good way to figure that out.

If you're really stuck, move on to Chapter 5 and then come back to the lab. With programming problems, it's very common to walk away for a while, and then have that "a-ha!" moment while you're in the shower, riding a bike, shopping for groceries, etc.

I hope this helps!
Andrew Stellman
Author, Head First C#
Building Better Software -- http://www.stellman-greene.com

#3 User is offline   nieub morrison 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: Yesterday, 04:07 AM

Posted Yesterday, 04:45 AM

Hello everyone,

I have everything working in Day at the Races EXCEPT three infuriating problems!

1) when you click the bet button, it always say, "not enough cash" no matter how much cash the guys have!
See my code for the Guy.PlaceBet() method:

public bool PlaceBet(int BetAmount, int DogToWin)
if (Cash > BetAmount)
myBet = new Bet() {Amount = BetAmount, Dog = DogToWin, Bettor = this};
return true;
MessageBox.Show("not enough cash.");
return false;

2) So I tried simply commenting out the "if cash > betamount" and "else" stuff, to make it always return true,
but after getting rid of that stuff, the following NEW error comes up:

"An unhandled exception of type 'System.NullReferenceException' occurred in oreilly_ch4_lab1.exe
Additional information: Object reference not set to an instance of an object."

... and when I close that error window, the cursor automatically points to the following line of code:

MyLabel.Text = myBet.GetDescription();

which is within the following context inside the Guy class:

public RadioButton MyRadioButton; // My RadioButton
public Label MyLabel; // My Label

public void UpdateLabels()
if (myBet != null)
MyLabel.Text = myBet.GetDescription();
MyRadioButton.Text = Name + " has " + Cash + " bucks";

Why on EARTH is this saying it's not an instance of an object???
Isn't myBet an instance of the Bet class?

I have also gotten similar errors pointing to the public RadioButton MyRadioButton and public Label MyLabel;
aren't these also instances of the objects generated automatically by microsoft when you drag a radiobutton
and label control onto the form??? Or am I supposed to generate a new instance of these objects in Form1?

So then I tried initiating these objects inside the Guy class with the following code
(because the obnoxious "hints" provided by oreilly were only misleading):

public RadioButton MyRadioButton = new RadioButton();
public Label MyLabel = new Label();

After that, the annoying "no object reference" error goes away, but when I click
the bet button, nothing happens, and after the dogs race, the money amounts dont
change either.

I've attached my project if you need to look at it.
I'm drowning here, and Oreilly certainly isn't helping;

this chapter is kind of like lending a guiding hand to a blind man then taking it away at the last minute,
laughing as he steps into traffic. This stuff comes naturally to the folks at Oreilly, I guess, but not for me,
and I am desperately trying to learn.

Please please help!!!

Attached File(s)


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