Understanding Classes in PowerShell – Part 04 – Adding Methods

This blog post is continuation of series of posts on understanding classes in PowerShell. Part 03 of series can be read at here.

Adding Methods to PowerShell Classes

What are methods

Till now, we have discussed how to add or view properties for the classes and create instances of the classes. However, the objects which are representation of real-world objects, do not only have properties, they also do have actions associated with themselves. You can think of methods as representation of actions associated with an object. For example, a dog barks, chew, bite etc. or a car is driven, parked, accelerate etc. Alternatively, you can also think of it as a fancy way of defining functions inside a class. However, you would limit yourselves to a function which is appropriate to that class only.

Define Methods

A method is defined in the same way as a function and it can take arguments in the same as a function. Also, all methods must return a value. If a method does not return a value, the return type is [Void]. For any other data type, it should be clearly type hinted for each argument in the method header, e.g., [String] $Name.

For example, building on to our previous class Dog, we can define few methods as below:

Class Dog{
# Define properties
[String] $Name
[int] $Age
[String] $Breed
[String] $OwnerName
[String] $OwnerAddress
[DateTime] $RegistrationDate
[long] $RegistrationNumber

# Define static properties
static [int] $NumberOfLegs = 4

# Defines Bark()
[String] Bark(){
return "Woof! woof!"
}

# Defines Grow()
Grow([int]$value){
$this.Age = $this.Age + $value
}

}

Let’s examine the Bark() first:

# Defines Bark()
[String] Bark(){
return "Woof! woof!"
}

Since it returns a [String] data type, it has return data type mentioned before the method name. Also, since it does not need to accept any arguments for operation, there is nothing inside parenthesis.

For other method Grow():

# Defines Grow()
Grow([int]$value){
$this.Age = $this.Age + $value
}

Since it needs by what value the dog can grow, we have defined the arguments with parenthesis. Again, note that to access the properties with the current instance of the class, you will need to use $this.

Call Methods

The first way we can call methods, is to create an instance of the Class, store the returned object in a variable, and then call the method.

For example, to create an instance of the Dog object, first we need to create an instance:

$myDog = [Dog]::new()

And, then we call the bark() method:

$myDog.Bark()

PS C:\Windows\system32> $myDog.Bark()
Woof! woof!

The second way to call the method is to call the New static method, and then call the method from that object:

PS C:\Windows\system32> [Dog]::new().Bark()
Woof! woof!

A third way to call the method uses New-Object, and it is called as the “group and dot.” This is shown here:

PS C:\Windows\system32> (New-Object Dog).Bark()
Woof! woof!

To call the Grow() method, we can use similar syntax. Only thing we need to remember is that, we need to pass a argument value this time:

PS C:\Windows\system32> $myDog.Grow(1)

PS C:\Windows\system32> $myDog.Age
1

PS C:\Windows\system32> [Dog]::new().Grow(2)

and so on.

Static Methods

To define static methods, we need to use static modifier before method name. For example:

# Defines Bark() 
 static [String] Bark(){
 return "Woof! woof!"
 }

This would be a good choice for static as it remains same and is independent of a specific object. As you can see, static methods can also return data types as well. Again, to call static methods, we need to use :: operator:

$myDog::Bark()

Again, do note that the methods can be public or hidden as well.

One thought on “Understanding Classes in PowerShell – Part 04 – Adding Methods

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s