This blog post is continuation of series of posts on understanding classes in PowerShell. Part 01 of series can be read at here.
What is an Constructor
Constructor
is an type of method or function which is called only when the object is created. It is never called after that no matter how many times you called the method or function.
The most common use of the constructor is to initialize properties related to the Object. As in .NET, the constructor must use the same name as the class.
For example, building on our previous example of class Dog, we can build a constructor as:
Class Dog{ # Define properties [String] $Name [String] $Breed [String] $OwnerName [String] $OwnerAddress [DateTime] $RegistrationDate [long] $RegistrationNumber # Define static properties static [int] $NumberOfLegs = 4 #Defines Constructor Dog([String] $Name, [String] $Breed, [String] $OwnerName, [String]$OwnerAddress, [DateTime]$RegistrationDate) { $this.Breed = $Breed $this.Name = $Name $this.OwnerName = $OwnerName $this.OwnerAddress = $OwnerAddress $this.RegistrationDate = $RegistrationDate } }
We can create the object like this:
# Using New-Object. Parameters for Argument list are positional and required by the constructor.
$myDog = New-Object -TypeName Dog -ArgumentList "Lucy","English Springer Spaniel","Cynthia G. Dumond","3105 Valley Lane, Austin, TX 78701","2017-12-12"
Or we can also use the static new()
method:
$myDog2 = [Dog]::new("Lucy","English Springer Spaniel","Cynthia G. Dumond","3105 Valley Lane, Austin, TX 78701","2017-12-12")
In both cases, the object will be initialized and you can see the values as below:
PS C:\Windows\system32> $mydog2 Name : Lucy Breed : English Springer Spaniel OwnerName : Cynthia G. Dumond OwnerAddress : 3105 Valley Lane, Austin, TX 78701 RegistrationDate : 12/12/2017 12:00:00 AM RegistrationNumber : 0 PS C:\Windows\system32> $myDog Name : Lucy Breed : English Springer Spaniel OwnerName : Cynthia G. Dumond OwnerAddress : 3105 Valley Lane, Austin, TX 78701 RegistrationDate : 12/12/2017 12:00:00 AM RegistrationNumber : 0
Just like .NET again, a constructor that takes no parameters is called a default constructor. Default constructors are invoked whenever an object is instantiated by using the new
operator and no arguments are provided to new
. For example, consider the below class:
Class Dog{ # Define properties [String] $Name [String] $Breed [String] $OwnerName [String] $OwnerAddress [DateTime] $RegistrationDate [long] $RegistrationNumber # Define static properties static [int] $NumberOfLegs = 4 }
Since we have not defined any constructor explicitly, a default constructor will be automatically created. The job of this constructor is to initialize object properties to their default values. In such a case, we can create object using:
$myDog = [Dog]::new() # or $myDog = New-Object -TypeName Dog
This is what we have seen in the previous post.
Do note that if we have defined a constructor explicitly, it will not go ahead and create a default constructor. Considering our first example of this blog post, $myDog3 = [Dog]::new()
will fail with following error:
Cannot find an overload for "new" and the argument count: "0". At line:1 char:1 + $myDog3 = [Dog]::new() + ~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodException + FullyQualifiedErrorId : MethodCountCouldNotFindBest
To overcome, We then need to define it explicitly like below:
Class Dog{ # Define properties [String] $Name [String] $Breed [String] $OwnerName [String] $OwnerAddress [DateTime] $RegistrationDate [long] $RegistrationNumber # Define static properties static [int] $NumberOfLegs = 4 #Defines Constructor Dog([String] $Name, [String] $Breed, [String] $OwnerName, [String]$OwnerAddress, [DateTime]$RegistrationDate) { $this.Breed = $Breed $this.Name = $Name $this.OwnerName = $OwnerName $this.OwnerAddress = $OwnerAddress $this.RegistrationDate = $RegistrationDate } #Defines default Constructor Dog() { $this.Breed = "" $this.Name = "" $this.OwnerName = "" $this.OwnerAddress = "" $this.RegistrationDate = "01/01/0001 12:00:00 AM" $this.RegistrationNumber = 0 } } $myDog3 = [Dog]::new() $myDog3
This will run fine.
What is $this
The $this
variable describes the current instance of the object. It is thought of like $_ for classes.
- If a property is not static, the syntax $this.PropertyName is used to reference the instance property.
- To refer to methods simply use the method name or $this.MethodName().
Do note that a static method or property cannot use $this.
[…] This blog post is continuation of series of posts on understanding classes in PowerShell. Part 02 of series can be read at here. […]
LikeLike
[…] we said that all properties and methods of a base class are inherited. However, constructors (check here on what are constructors and how to define them), are not inherited by a child class. Instead, like all PowerShell classes, the new class is […]
LikeLike