Import PowerShell Classes from modules

This blog post is continuation of series of posts on understanding classes in PowerShell. Part 06 of series can be read at here. If you have defined PowerShell class inside a module, its not easy to import class in the current session. By default, it does not load classes. Let’s understand the details of it.

Import-Module does not import Classes

Import-Module has been a standard way of importing functions defined in a PowerShell module. It also comes with -verbose parameter so that you can see what has been imported. For understanding the case with classes, let’s define a simple PowerShell module HelloModule.psm1 as below code: Continue reading “Import PowerShell Classes from modules”

Understanding Classes in PowerShell – Part 06 – Explaining Inheritance further

This blog post is continuation of series of posts on understanding classes in PowerShell. Part 05 of series can be read at here. In previous blog post, we have learned about meaning of inheritance, define child class and parent class, overload methods, override methods of base class and calling base class members anyhow.

Effect of inheritance on constructors

When we discussed about inheritance, 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 created with a default (parameter-less) constructor. If we think about it, this makes sense after all. The purpose of a constructor is to  initialize the object associated with a class.  …Continue Reading

Understanding Classes in PowerShell – Part 05 – Explaining Inheritance

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

What is Inheritance

In many classes that you create, you will find that you often need the same properties and methods from another class that you created earlier. For example, if you have a base class called the Person class, and it contains LastName and FirstName properties and a Print method, you will find that for an Employee class, you need the same properties and methods. You may also need additional properties, such as EmployeeID and Salary. When you inherit from the Person class (the base class) you can add these properties to this new Employee class, and still have access to all of the properties in the Person class. Inheritance is about the ability for one class to define itself as having all the properties and methods of a particular class, and then extending the definition of the base class by adding additional properties and methods.

Why should we use Inheritance

Inheritance is desirable because you want to avoid writing the same code over and over again. If you have two separate classes, and each one …Continue Reading

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. …Continue Reading