Originally, this post was going to be about GUIs, but after waking up this morning and seeing a comment on Part 28, I figured that I’d address that comment in this post.
Paul Bendall writes:
But how does an ITPro without a developer background start to use MSDN and understand terminology such as static, override, ::Something?
Let’s demystify some of the things at MSDN so that maybe you guys without a developer background can make sense of it.
Terminology Elevator – Ground Floor – Objects
Objects are… um… objects in programming terminology. An object could be words, numbers, a computer, anything really. In Appendix B in our book, I talk about objects at a really high level. Let’s take a similar approach here. Let’s talk about a keyboard.
A keyboard is an object. It has keys – which means that a keyboard could have a collection of objects. Yes, collection can be a technical term referring to a grouping of like or related objects. Now let’s look at a keyboard key… what can you tell me about it?
- What color is the key?
- What character or characters are on the keys?
- How big or small is the key?
- Is the key enabled?
The answer to these questions would be called properties of the keyboard key. They describe the object in terms of adjectives and capabilities.
- Press the key.
- Release the key.
These actions show that the key can respond to events. When an event happens, you can trigger a piece of code to run. In some cases, you may be able to have code run before or after an event happens, depending on the object and event.
Now let’s look at the Caps Lock key. When you press it, the alphabetical keys TYPE CAPITAL LETTERS. This means that not only can this key respond to being pressed, but it also can perform an action called a method. Yes, this also means that the object may call a method while responding to an event.
So an object can have properties, methods, and events.
Terminology Elevator – Level 1 – Class, Static, and Constructor
A class is a container that holds the methods, events, and properties of an object. Using our keyboard key example, a class would be the collection of all of the properties, methods, and events that belong to a keyboard key.
If you find yourself using the New-Object cmdlet and getting the “Constructor not found.” error, that basically is telling you that you are working with a static class.
A constructor is a piece of code that states how to create a new object. This is the piece of code that gets run when an object is created with the New-Object cmdlet.
If the constructor isn’t in the class, then the class is considered static. If something is called static, this means that they are not created, destroyed, or changed – they are simply used.
In PowerShell, the :: operator is used to call out to static methods and properties. The syntax is as follows:
Yesterday, we looked at the System.Windows.Forms.SystemInformation class and some of its properties. There is also a TechNet article on using static classes and methods in PowerShell.
Terminology Elevator – Level 2 – Assemblies and Namespaces
Assemblies and namespaces are ways to organize code. An assembly is a physical way of organizing code – commonly in a DLL file. A namespace is a logical way of organizing code – grouping code for the same object or collection of objects together.
Looking at the top of the SystemInformation class page on MSDN, you can see that this particular class lives in the System.Windows.Forms.dll assembly. It also belongs to many namespaces – collectively, it belongs to System.Windows.Forms. However, Forms is a namespace that belongs to Windows, which is a namespace that belongs to the System namespace.
One thing you’ll notice with the naming scheme is that the namespaces are stacked like Russian matryoshka dolls – the largest namespace is on the outside, and the smaller namespaces are nested inside, all the way down to the class – which is like that tiny doll at the end.
Terminology Elevator – Observation Deck – Overrides
By default, there are actions that a command can do automatically. However, you may want to customize those actions, which can be done by overriding the action. Basically, if you write code that overrides the default behavior, your code supersedes the default code.
A lot of what you’ll see on MSDN is based on object-oriented programming concepts. Hopefully this post gives you a little idea as to what those terms mean and can help you on your way in using static classes and methods in PowerShell.