# Question concerning Method in new Type/Class

**URL:** <https://forums.powershell.org/t/question-concerning-method-in-new-type-class/18368>\
**Category:** PowerShell Help\
**Created:** [January 17, 2022, 11:38am UTC](https://forums.powershell.org/t/question-concerning-method-in-new-type-class/18368 "2022-01-17T11:38:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![powgu](https://avatars.discourse-cdn.com/v4/letter/p/cab0a1/32.png) [@powgu](https://forums.powershell.org/u/powgu)\
**Post date:** [January 17, 2022, 11:38am UTC](https://forums.powershell.org/t/question-concerning-method-in-new-type-class/18368/1 "2022-01-17T11:38:08Z")

</div>

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:

```auto
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:

```auto
[MyClass] | Get-Member -MemberType *

```

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

Thank you.

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [January 17, 2022, 10:03pm UTC](https://forums.powershell.org/t/question-concerning-method-in-new-type-class/18368/2 "2022-01-17T22:03:25Z")

</div>

Hi, welcome to the forum 👋

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.

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

```

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

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

```

et voila:

```auto
   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;}

```

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [January 18, 2022, 1:41am UTC](https://forums.powershell.org/t/question-concerning-method-in-new-type-class/18368/3 "2022-01-18T01:41:50Z")

</div>

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.

```auto
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.

```auto
$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)

```

---

<div class="post-metadata">

**Author:** ![powgu](https://avatars.discourse-cdn.com/v4/letter/p/cab0a1/32.png) [@powgu](https://forums.powershell.org/u/powgu)\
**Post date:** [January 19, 2022, 5:54am UTC](https://forums.powershell.org/t/question-concerning-method-in-new-type-class/18368/4 "2022-01-19T05:54:30Z")

</div>

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:

```auto
[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?

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [January 19, 2022, 6:00am UTC](https://forums.powershell.org/t/question-concerning-method-in-new-type-class/18368/5 "2022-01-19T06:00:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:19pm UTC](https://forums.powershell.org/t/question-concerning-method-in-new-type-class/18368/6 "2024-05-16T20:19:51Z")

</div>


