was successfully added to your cart.

Why do you teach Console Applications instead of Drag and Drop in WinForms?

Why do you teach Console Applications instead of Drag and Drop in WinForms?

By March 16, 2018 Ask Tim No Comments

Teaching C# vs. Teaching Magic

If you have watched any “Getting Started” C# tutorials, you have probably seen the drag and drop Hello World example. You have never seen Visual Studio or C# before, so the presenter shows you Windows Forms and shows how easy it is to drag and drop controls onto a form, then you double click a button, add a bit of code and bingo, you have a working application. Seems simple, right? Unfortunately, this has become the default way of teaching new C# developers. In fact, I teach C# at a local college. The book I have to teach out of starts off this way. So what’s the problem? As I see it, there are two main problems.

The first problem is that when you learn WinForms first, you are learning magic. For example, when you click on a button, why does it execute the code? Magic. If you access the text property from a textbox, how do those properties work? Magic. How does putting a new control on a form work? Magic. So why is magic a problem? Let me give you a great example that I hear from new developers all the time. Say you are creating a form that does just the Hello World example. You drag the button on the form and resize it. You go to double click on the button to create the spot in code to put your code but you accidentally click on the form instead of the button. It takes you to your code but you know that’s not right so you go back to your form and double click on the button. That creates the right code bits for you, you add your code, and run your application. Things look great. The problem is that you have that extra bit of code in your code behind (the event from double-clicking the form instead of the button). It looks a bit like this:

So, like any good programmer, you clean up that empty code bit. Cool. Now the code looks right. The next step is to add a textbox so you go back to your form designer and you get this scary message:

Now what? This is why I hate magic. This poor user’s magic just stopped working and they don’t know why. If you learned why WinForms works, you would know that it is based upon class instantiation, events (and event handlers), and partial classes. Putting all that knowledge together, you would know that the issue, based upon the exception message, is that there is an event handler that is wired up to a non-existent method. To fix it, just go to the other partial class for the form class (the one that Visual Studio built) and delete the event handler line like so:

See how much you need to know to really understand the magic? See why it is important? If you know how WinForms works, when you get an error, you will feel more confident to fix that problem. When you don’t know how WinForms works, you end up throwing out your application and starting over. Imagine how frustrating that is.

OK, so that is the first reason why I don’t like teaching WinForms for new developers. My second reason why I don’t like teaching in WinForms first is because there is a lot of work to do before you get to the actual code. Let me walk you through an example: Say you want to learn how to use if/else. So, if you do that in WinForms, you would need to rename Form1 (if you are simulating a real application at all), drag a button on the screen, then, in the event code put an if/else statement. However, now you need to display the results so you then have to learn about MessageBox. We have just generated a lot of magic that we have to ignore. Not the end of the world but let’s contrast that to a Console Application. You need to write a Console.ReadLine() at the end of your code. For inside the if/else, you need to use Console.WriteLine(). That’s it. See how simple that is? You get very little magic (just that the Console works and what the two methods do) and very little distracts you from your actual syntax piece that you are learning.

So, to summarize, WinForms are more complex and you need to understand some object oriented programming (OOP) principles in C# before you really understand what you are doing. Until then, it is magic and when magic breaks, people cry. By contrast, Console Applications are simple and get you right to the syntax you are working on.

Leave a Reply