Summary: Ed Wilson, Microsoft Scripting Guy, talks about using the new Windows PowerShell 5.0 class feature in Windows 10 and doing constructor overloading.
Microsoft Scripting Guy, Ed Wilson, is here. Today I want to talk about overloaded constructors. I know, I know, I know…
It sounds like some sort of engineering failure. But that is not the case. In fact, constructor overloading makes your code much more flexible, and even easier to read—when you learn the trick. And today we are going to unmask the secrets of constructor overloading with Windows PowerShell 5.0 classes in Windows 10.
This is way cool stuff, and soon you will be able to entertain friends at night, regal workshop audiences in real-time, and maybe even get more work done sooner and with less effort. After all, that is the real payoff. So let’s get started.
What’s an overload anyway?
An overload is more than one way of doing something. So for example, when I call a method and pass one or more parameters to it, that calls for an overloaded method definition. This means in the code, I need to be able to handle one or more ways of doing things. It also means that when I call that method, I have more than one way of calling the method.
A simple example is the GetProcessesByName method from the System.Diagnostics.Process class. It has two overloads: one in which I pass only the process name and the other where I pass the process name and the name of the computer. This overload definition gives me the ability to return processes locally or remotely. Here are the overload definitions:
PS C:\Users\mredw> [System.Diagnostics.Process]::GetProcessesByName
OverloadDefinitions
-------------------
static System.Diagnostics.Process[] GetProcessesByName(string processName)
static System.Diagnostics.Process[] GetProcessesByName(string processName, string
machineName)
Just as I can overload a method definition, I can also overload the constructor. The constructor is what is used to create a new instance of a class.
Remember that when I have a new instance of a class I have created an object. Also remember that a class itself is simply a template for creating objects. I talk about this in my Introduction to PowerShell 5 Classes blog post and in Introduction to PowerShell 5 classes—The Video.
There are also different ways I can create a new instance of a class. As an example, consider the System.DateTime class that represents an instance of date and time. I can create an instance of this class in many different ways. To find these ways, I use the static New method, for example:
[datetime]::new
Here is an image of the overload output:
Creating overloaded constructors
To create a constructor (overloaded or otherwise) in a Windows PowerShell 5.0 class, I need to remember that I am really creating a method. The method has the same name as the class name. When I do this, I have a constructor.
For example, I want to add a constructor to my Car class (the simple class I talked about in PowerShell 5: Create Simple Class).
Note You also need to understand creating methods because, after all, a constructor is really just a method. For information about methods, please refer to Adding Methods to a PowerShell 5 Class.
Here is the basic Car class script:
Class Car
{
[String]$vin
static [int]$numberOfWheels = 4
[int]$numberOfDoors
[datetime]$year
[String]$model
}
Now I add a method with the name of Car. Here is the syntax:
Name of Class
A pair of parentheses for the input
A script block that does something
All cars have a VIN, so it makes sense to have a VIN input when creating an instance of the class. Here is my constructor:
Car ([string]$vin)
{$this.vin = $vin}
The complete script now looks like this:
Class Car
{
[String]$vin
static [int]$numberOfWheels = 4
[int]$numberOfDoors
[datetime]$year
[String]$model
Car ([string]$vin)
{$this.vin = $vin}
}
When I run it and load the class, I can now create a new instance of the class by using my constructor. The syntax is shown here:
PS C:\> [car]::new(1234)
vin numberOfDoors year model
--- ------------- ---- -----
1234 0 1/1/0001 12:00:00 AM
To overload this constructor, I add additional variations—each using the same name. Windows PowerShell will figure out what I want to do. Here I add overloads to my constructor:
Class Car
{
[String]$vin
static [int]$numberOfWheels = 4
[int]$numberOfDoors
[datetime]$year
[String]$model
Car ([string]$vin)
{$this.vin = $vin}
Car ([string]$vin, [string]$model)
{$this.vin = $vin
$this.model = $model}
Car ([string]$vin, [string]$model, [datetime]$year)
{$this.vin = $vin
$this.model = $model
$this.year = $year}
Car ([string]$vin, [string]$model, [datetime]$year, [int]$numberOfDoors)
{$this.vin = $vin
$this.model = $model
$this.year = $year
$this.numberOfDoors = $numberOfDoors}
}
The first thing I want to do is to see what overloads I have. This is shown here:
PS C:\> [car]::new
OverloadDefinitions
-------------------
Car new(string vin)
Car new(string vin, string model)
Car new(string vin, string model, datetime year)
Car new(string vin, string model, datetime year, int numberOfDoors)
This means I can create the car in a variety of methods. Here are a few examples:
PS C:\> [car]::new(1234)
vin numberOfDoors year model
--- ------------- ---- -----
1234 0 1/1/0001 12:00:00 AM
PS C:\> [car]::new(1234, "chevy", "1/2/2015",3)
vin numberOfDoors year model
--- ------------- ---- -----
1234 3 1/2/2015 12:00:00 AM chevy
That is all there is to using overloaded constructors in PowerShell 5.0. Windows PowerShell 5.0 Class Week 2 will continue tomorrow when I will talk about more way cool stuff.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy