Modernizing .NETpad Step-By-Step, Part 2: Next Steps (Premium)

This time, we'll look at some basic code refactoring, add a few small features and other changes, and redesign .NETpad's menu system.
Separate application state variables from MainWindow
I wrote about my code refactoring efforts twice this past summer, most recently regarding what I think of as application state. Some of this is related to user settings that are loaded when the app starts, configured by the user while it runs, and saved again when the app closes. But some of it is temporary and only needed by the app while it's running.

In the original version of .NETpad, all application state is handled the same way: I created what I think of as global variables in the MainWindow class and then use them throughout the code. But those variables are only global to that window/class. And because .NETpad uses several sub-windows that need to access some of this data, it's tedious to so using that original design. So I moved all non-user setting variables related to application state out of MainWindow and put them in the App class (App.xaml.cs). And in updating the app, I also created a few more application state variables as well.

So let's get that started.

Open MainWindow.xaml.cs and examine the top of the MainWindow class code block, where you will see several "global" variables.

Three of these are tied to user settings that will be loaded at run-time and saved as needed. These will stay where they are.

The rest are runtime application state variables, and so those are moving to App.xaml.cs.

If you were to run the app now, it would fail hard: There is code all over MainWindow.xaml.cs that relies on those variables, and now that they are elsewhere, outside the scope of the MainWindow class, they are no longer visible or accessible. And so we need to change each reference to each of those five variables. As an example, the TextHasChanged variable is currently accessed like so:

if (TextHasChanged == false)

But this needs to change to the following:

if (App.TextHasChanged == false)

The App addition fully qualifies the variable name, making it visible to the code in MainWindow (and, later, other classes/windows). So we have to do a find/replace operation (Ctrl + H) throughout MainWindow.xaml.cs in which we replace TextHasChanged with App.TextHasChanged.

Then, do the same for DocumentName, MasterFontSize, FindTextString, and FindLastIndexFound.

Then, open App.xaml.cs and add the public static modifiers to each of the application state variables. Public makes each variable public to the entire app--visible and accessible from any class--while static ensures that only a single instance of that variable is created throughout the app.

Now, run the app, which builds the code and saves any unsaved files. It should just run normally. This is one goal of code refactoring: We've improved the code without impacting the user experience.

Before moving on, let's add a few more application state...

Gain unlimited access to Premium articles.

With technology shaping our everyday lives, how could we not dig deeper?

Thurrott Premium delivers an honest and thorough perspective about the technologies we use and rely on everyday. Discover deeper content as a Premium member.

Tagged with

Share post

Thurrott