Jump to content


Photo

Aliens Attack Problem


  • Please log in to reply
3 replies to this topic

#1 štef_bosanac

štef_bosanac

    New Member

  • Members
  • Pip
  • 2 posts

Posted 20 October 2013 - 12:20 PM

hello,

i keep getting this error while trying to do the exercise in the first chapter

after adding a few breakpoints, i believe the problem is caused while calling the AnimateEnemy method the second time...
also some App.g.i.cs file is generated every time the application crashes.

I'm pretty clueless on to fix this, and its getting really frustrating to be stuck on the first chapter :(

#2 AndrewStellman

AndrewStellman

    Andrew Stellman

  • O'Reilly Author
  • PipPipPipPipPipPipPipPipPipPipPip
  • 832 posts
  • Gender:Male
  • Location:Brooklyn, NY
  • Interests:Author of: "Head First C#", "Beautiful Teams", "Head First PMP", "Applied Software Project Management"

Posted 20 October 2013 - 06:48 PM

You'll get an ArgumentOutOfRange exception with the message "'maxValue' must be greater than zero" if you pass a negative number to the random.Next() method. My guess is that it's happening in the AddEnemy() method when this is being called:


random.Next((int)playArea.ActualHeight - 100)

I bet you have a typo somewhere that's setting playArea's height to something that's less than 100 the first time AddEnemy() or AnimateEnemy() is called. That would cause a negative number to be passed into random.Next(). Copy and paste the code from the working version we published (see the book page for links). Definitely download the working program and run it, sometimes it helps to see it running so you have a working version to compare your code with. It's probably just one typo!

Other things to check: make sure the Canvas control is named playArea (and not some other control), and also make sure its height is greater than 100, which would cause a negative number to be passed into random.Next().

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

#3 štef_bosanac

štef_bosanac

    New Member

  • Members
  • Pip
  • 2 posts

Posted 21 October 2013 - 02:00 AM

Yep, you are right, the canvas height was the problem, it was set to Auto (0) for some reason...
after changing to some random value and then setting it to auto again it worked :)

thank you for your help

#4 AndrewStellman

AndrewStellman

    Andrew Stellman

  • O'Reilly Author
  • PipPipPipPipPipPipPipPipPipPipPip
  • 832 posts
  • Gender:Male
  • Location:Brooklyn, NY
  • Interests:Author of: "Head First C#", "Beautiful Teams", "Head First PMP", "Applied Software Project Management"

Posted 21 October 2013 - 07:36 AM

Glad to help! And good luck with your C# learning!
Andrew Stellman
Author, Head First C#
Building Better Software -- http://www.stellman-greene.com




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users