Question concerning Method in new Type/Class

Hello, this is my first post and I hope it is in the right category.

When I create a new Type with own constructor and method like:

class MyClass
{
[string]$name
[datetime]$startdate
[int]$id

MyClass([string]$name, [datetime]$startdate,[int]$id)
{
$this.name = $name
$this.startdate = $startdate
$this.id = $id
}
[timespan]GetPeriod()
{
$today = Get-Date
$period = $today - $this.startdate
return $period
}
}

And I then look at the methods with:

[MyClass] | Get-Member -MemberType *

The method “GetPeriod” does not appear. What do I miss - does anyone have an idea?

Thank you.

Hi, welcome to the forum :wave:

You don’t actually have a class yet, just a definition of a class.

You’re actually just piping your class definition to Get-Member and the output you’re seeing is the members of an instance of the System.RuntimeType class.

PS E:\Temp\Files> [MyClass] | Get-Member         
    TypeName: System.RuntimeType

To see the members of your class, first create an instance of it:

$instance = New-Object -TypeName MyClass -ArgumentList 'myFirstClass',(Get-Date),1
$instance | Get-Member

et voila:

   TypeName: MyClass

Name        MemberType Definition
----        ---------- ----------
Equals      Method     bool Equals(System.Object obj)
GetHashCode Method     int GetHashCode()
GetPeriod   Method     timespan GetPeriod()
GetType     Method     type GetType()
ToString    Method     string ToString()
id          Property   int id {get;set;}
name        Property   string name {get;set;}
startdate   Property   datetime startdate {get;set;}
1 Like

A couple of additional notes.

If you create a custom constructor, consider adding a parameterless constructor to create instance with no arguments.

You can use the static modifier for properties/methods that aren’t instance specific. The GetPeriod() method is a bad example but just for reference.

class MyClass{
    [string]$name
    [datetime]$startdate
    [int]$id

    MyClass(){}

    MyClass([string]$name, [datetime]$startdate,[int]$id){
        $this.name = $name
        $this.startdate = $startdate
        $this.id = $id
    }

    [timespan]GetPeriod(){
        $today = Get-Date
        $period = $today - $this.startdate
        return $period
    }

    static [timespan]GetPeriod($obj){
        $today = Get-Date
        $period = $today - $obj.startdate
        return $period
    }
}

Now you can use either of these to achieve the same result.

$instance = [MyClass]::new('Name',(Get-Date),123)
$instance.GetPeriod()

$instance = [MyClass]::new()
$instance.Name = 'Name'
$instance.StartDate = Get-Date
$instance.Id = 123
[myclass]::GetPeriod($instance)
2 Likes

Thank you very much for the answers. It works. Creating an actual instance let’s the method show up in Get-Member. I hadn’t even thought of that. It had assumed that Get-Member checks the definition, the recipe so to say.
I still wonder: When I pipe for example [DateTime] to Get-Member:

[DateTime] | Get-Member

ist shows me the methods of [DateTime]. When I do the same with [MyClass] it shows me quite a few methods as well just not the method GetPeriod that I had created in the class. Does someone have any idea why that is?

Did you miss the part about static members? Those are members of the class, where non static are members of the instantiation of the class.