This is a coin counting game that demonstrates the use of generic collections. Instead using arrays or arraylists collections are a better fit in most cases. Plus I’m finding out that in my collections classes my add, remove, sort, etc.., algorithms don’t have to be as complicated. It’s making for some cleaner code. This combined with the knowledge of events, event handlers, delegates, and lambda expressions things are moving right along. When the game is started a random amount is selected. The players job is to use the amount buttons provided to match that amount. There are mouse button event handlers in place to remove the money from the screen by clicking on them. I’m trying to create more classes that do the work behind the scenes away from my MainForm code.
It is inevitable at some point in coding that you will come across the need to handle errors and exceptions. You can take care of most predictable scenarios but checking variables and such. However users will always find ways to work with your programs in ways you never intended or saw coming. And in doing this they will introduce errors that you will need to handle. That’s where try/catch/finally comes into play. I made a simple text file reader to demonstrate the ability to throw custom errors based off the ReadAllText() method. This gives a small glimpse into catching and handling exceptions.
I have to admit that since figuring out how Lambda expressions work I’ve been using them quite a bit. This and becoming a fan of ArrayList and generic collections over arrays. But for this program I wanted concentrate on the usage of lambda expressions being used with event arguments. The goal of the game is to click on each target before the timer runs out. If you get 4 batches of hits total you progress to the next level. In later versions I’ll implement the ability to select the player level manually.
Delegates and events. This was a hard concept for me to grasp when I first starting studying this. Actually let me be more specific. The Delegates part wasn’t that bad. Understanding how Events work, how they’re constructed, triggered, etc… Specifically custom events as you will see from the source. For example double-clicking on the Form_Load event to have Visual Studio create the event for you automatically is easy. But, for example, constructing menu clicks to custom event handlers to custom methods that fire off in a different class was a hard concept to grasp. However once I FINALLY understood all of this everything started falling into place. And I do use what I’ve learned a log in my other programs. If done right it just makes for cleaner, more understanding code.
Also if you notice I start using the ArrayList collection and you will see other collection methods moving forward as I’m learning about them. Don’t get me wrong Arrays are fun and have their place. But other collection methods can be used also.
I wanted to make a change to the Temperature Conversion program I did earlier to demonstrate how Structs work. Structs are a value data type. Unlike a class which is a reference data type. One of the main things I noticed is you don’t need to create an instance to use a struct. However they are limiting which may or may not be good depending on what you want to accomplish.