Last active
December 12, 2017 20:41
-
-
Save sdoubleday/957efac8f61523423b64e31ea20e9fa8 to your computer and use it in GitHub Desktop.
PowerShell Demo - Why Does Child Constructor Fail When Base Class Default Constructor Has Logic In It
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <# | |
| A brief example to answer the question: | |
| In Powershell, why does my child constructor fail when I have logic in my base class default constructor? | |
| Or more plaintively: | |
| powershell why doesn't my child constructor work | |
| powershell why doesn't my child constructor fire | |
| powershell why doesn't my child constructor override base class constructors | |
| I tested this in version: 5.1.14393.1532 | |
| In brief: because the base class default constructor fires before the child constructor (presumably, this is why "they" say you need to have a default constructor on a base class before you can inherit from it). | |
| For a demo, import this module and run: | |
| mydemo | |
| #> | |
| class mybaseclass { | |
| [int]$myproperty | |
| mybaseclass () { | |
| write-verbose -Verbose -Message "mybaseclass default constructor" | |
| IF ( @($PSBoundParameters.Values).Count -gt 1 ) {Throw "Default constructor on base class throws an error if @(PSBoundParameters.Values).Count is greater than 1"} | |
| } | |
| mybaseclass ([int]$thing) { | |
| write-verbose -Verbose -Message "mybaseclass one param constructor: thing" | |
| $this.myproperty = $thing | |
| } | |
| mybaseclass ([int]$thing,[int]$multiplier) { | |
| write-verbose -Verbose -Message "mychildclass two param constructor: thing * multiplier" | |
| $this.myproperty = $thing * $multiplier | |
| } | |
| } | |
| class mychildclass : mybaseclass { | |
| [int]$myproperty | |
| mychildclass () { | |
| write-verbose -Verbose -Message "mychildclass default constructor" | |
| } | |
| mychildclass ([int]$thing) { | |
| write-verbose -Verbose -Message "mychildclass one param constructor: thing * 2" | |
| $this.myproperty = $thing * 2 | |
| } | |
| mychildclass ([int]$thing,[int]$multiplier) { | |
| write-verbose -Verbose -Message "mychildclass two param constructor: thing * multiplier * 2" | |
| $this.myproperty = $thing * $multiplier * 2 | |
| } | |
| } | |
| function mydemo { | |
| Write-Verbose -Verbose 'Base class empty - result should be 0' | |
| ([mybaseclass]::NEW() ).myproperty | |
| Write-Verbose -Verbose 'Base class one - result should be 2' | |
| ([mybaseclass]::NEW(2) ).myproperty | |
| Write-Verbose -Verbose 'Base class two - result should be 10' | |
| ([mybaseclass]::NEW(2,5) ).myproperty | |
| Write-Verbose -Verbose 'Child class empty - observe the way the default constructor on the base class is fired first.' | |
| ([mychildclass]::NEW() ).myproperty | |
| Write-Verbose -Verbose 'Child class one - result should be 4 - observe the way the default constructor on the base class is fired first, even though this has a different number of parameters (i.e., it DOES NOT fire [mybaseclass]::NEW([int]$thing).' | |
| ([mychildclass]::NEW(2) ).myproperty | |
| Write-Verbose -Verbose 'Child class two - notice that because there is validation logic in the default constructor, this fails, even though it seems like it should be overloading the constructor.' | |
| ([mychildclass]::NEW(2,5) ).myproperty | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment