# Inherited class returns base class from constructor

**URL:** <https://forums.powershell.org/t/inherited-class-returns-base-class-from-constructor/21409>\
**Category:** PowerShell Help\
**Created:** [January 19, 2023, 2:46pm UTC](https://forums.powershell.org/t/inherited-class-returns-base-class-from-constructor/21409 "2023-01-19T14:46:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chaos02](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/chaos02/32/4232_2.png) [@Chaos02](https://forums.powershell.org/u/Chaos02)\
**Post date:** [January 19, 2023, 2:46pm UTC](https://forums.powershell.org/t/inherited-class-returns-base-class-from-constructor/21409/1 "2023-01-19T14:46:02Z")

</div>

Hi - kinda new to .NET and Powershell integration with it.  
Please have a look at the following code. to my understanding it $bAttachment should contain an AttachmentLabel object with my custom properties. However it seems to return a standart issue [Forms.Button]::new(), without any properties set!  
I’ve checked via the VSCode extension that during construction my object actually contains all of the set properties. What am I doing wrong?

```powershell
using namespace System.Windows.Forms
using namespace System.Windows.DataFormats
using namespace PresentationFramework

[...]

class AttachmentLabel : System.Windows.Forms.Button {
    AttachmentLabel() {
        $this.Name = 'bAttachment'
        $this.TextAlign = [System.Drawing.ContentAlignment]::MiddleLeft
        $this.AutoSize = $True
        $this.AutoSizeMode = [System.Windows.Forms.AutoSizeMode]::GrowAndShrink
        $this.FlatAppearance.BorderColor = [Drawing.Color]::FromName('GrayText')
        $this.Add_Click({
            $global:mailContent.Attachments -= $(Get-Item $this.Text)
            $fLPAttachments.Controls.Remove($this)
            $this.Dispose()
        })
    }
    AttachmentLabel([string]$name, [string]$text) { # For some reason constructs AttachmentLabel fine and then returns new System.Windows.Forms.Button
        $this = $this::new()
        $this.Name = $name
        $this.Text = $text
    }
}

$fLPAttachments.Add_DragEnter({
        if ($_.Data.GetFormats($True) -ccontains [DataFormats]::FileDrop) {
            foreach ($a in $_.Data.GetData([DataFormats]::FileDrop)) {
                $attachment = Get-Item $a
                $bAttachment = [AttachmentLabel]::new("bAttachment-$($attachment.FullName)", "[X] $($attachment.Name)")
                $fLPAttachments.Controls.Add($bAttachment)
            }
        }
    })

```

---

<div class="post-metadata">

**Author:** ![Chaos02](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/chaos02/32/4232_2.png) [@Chaos02](https://forums.powershell.org/u/Chaos02)\
**Post date:** [January 20, 2023, 7:41am UTC](https://forums.powershell.org/t/inherited-class-returns-base-class-from-constructor/21409/2 "2023-01-20T07:41:59Z")

</div>

Update: I have found out about constructor chaining and that it’s not possible in PowerShell.  
I’ve resorted to this approach:

```powershell
class AttachmentLabel : System.Windows.Forms.Button {
    AttachmentLabel() {
        $this.Init()
    }
    AttachmentLabel([string]$name, [string]$text) { # For some reason constructs AttachmentLabel fine and then returns new System.Windows.Forms.Button
        $this.Init()
        $this.Name = $name
        $this.Text = $text
    }
    hidden Init() {
        $this.Name = 'bAttachment'
        $this.TextAlign = [System.Drawing.ContentAlignment]::MiddleLeft
        $this.AutoSize = $True
        $this.AutoSizeMode = [System.Windows.Forms.AutoSizeMode]::GrowAndShrink
        $this.FlatAppearance.BorderColor = [Drawing.Color]::FromName('GrayText')
        $this.Add_Click({
            $global:mailContent.Attachments -= $(Get-Item $this.Text)
            $fLPAttachments.Controls.Remove($this)
            $this.Dispose()
        })
    }
}

```

---

<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:16pm UTC](https://forums.powershell.org/t/inherited-class-returns-base-class-from-constructor/21409/3 "2024-05-16T20:16:05Z")

</div>

Closed on May 16, 2024, 8:16pm UTC


