Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

C#: horse racing

I am learning C#, I am working on Lab1 in the Head first C# book. The purpose of the lab is to create a horse racing simulator. The method below is in a guy class. I kept getting the error, NullReference Exception Unhandled, when I tried to use the GetDescription method from the Bet class. Bascially, I have a Guy class and a Bet class, and a method in the Guy class call a method in the Bet class, but that doesn't work. I tried to create an object, but that doesn't solve the problem. Can someone give me an idea? I am new to C# and object oriented.  See attached for codes.

------------------------------------------------------------------

Kudos and Accepted as Solution are welcome!
0 Kudos
Message 1 of 2
(6,429 Views)

@jyang72211 wrote:

I am learning C#, I am working on Lab1 in the Head first C# book. The purpose of the lab is to create a horse racing simulator. The method below is in a guy class. I kept getting the error, NullReference Exception Unhandled, when I tried to use the GetDescription method from the Bet class. Bascially, I have a Guy class and a Bet class, and a method in the Guy class call a method in the Bet class, but that doesn't work. I tried to create an object, but that doesn't solve the problem. Can someone give me an idea? I am new to C# and object oriented.  See attached for codes.


With the way you have written your code, I would try

            if (People[guyIndex].PlaceBet((int)betAmountBox.Value, (int)dogNumberBox.Value))
            {
                //Bet MyBet = new Bet() { Amount = (int)betAmountBox.Value, Dog = (int)dogNumberBox.Value, Bettor = People[guyIndex] };
                People[guyIndex].MyBet.Amount = (int)betAmountBox.Value;
                People[guyIndex].MyBet.Dog = (int)dogNumberBox.Value;
                People[guyIndex].MyBet.Bettor = People[guyIndex];
                People[guyIndex].UpdateLabels();
            }

 

Message 2 of 2
(6,389 Views)