0

I have those three files:

Glass.ps1

class Glass
{
    # Properties
    [int32]$Size
    [int32]$CurrentAmount

    # Constructors
    Glass () { }

    Glass ($Size, $Amount)
    {
        $this.Size = $Size
        $this.CurrentAmount = $Amount
    }

    # Methods   
    [Boolean] Fill ([int32]$volume)
    {
        Write-Host "Calling PARENT class"
        return $true
    }

    [Boolean] Drink ([int32]$amount)
    {
        return $true        
    }
}

AnyGlass.ps1

class AnyGlass : Glass
{
    [String]$Name

    AnyGlass () {
        $this.Size = 6                  # parent property
        $this.CurrentAmount = 0         # parent property
        $this.Name = "My glass"         # child property
    }

    # Methods overload
    [void] Fill ([int]$volume, [string]$Warning)
    {   
        Write-Host "Calling CHILD class"
        reurn $true
    }
}

runIncluded.ps1

. ".\Glass.ps1"
. ".\AnyGlass.ps1"

$glass = New-Object -TypeName Glass
$anyGlass = New-Object -TypeName AnyGlass
$anyGlass.Fill(1)

If I start new PS console and run runIncluded.ps1 then the output is as expected:

PS C:\Users\wakatana\Desktop\ps\> .\runIncluded.ps1
Calling PARENT s class
True

But If I change Write-Host "Calling PARENT class" e.g. to Write-Host "Calling PARENT class NEW VERSION" and I run runIncluded.ps1 again (from the same PS console) then I get the same output as previous. If I restart the console then it works just OK and i get Calling PARENT class NEW VERSION. If I put content of three mentioned file into one, then run it from console, then edit it, then run again, everything is working fine. Is it necessary to somehow reset the environment before running new scripts or what is the problem?

PS: To me this looks like broken PS, I guess this might be related to my other issue

My OS: Windows 10 Home - Latest Updates

Wakan Tanka
  • 7,542
  • 16
  • 69
  • 122
  • Can you double-check your ```AnyGlass.ps1``` example above? When I run that I get ```Invalid return statement within void method.``` because you've declared the method with return type ```[void]``` but you've got the line ```reurn $true``` in the definition (which should probably also be ```reTurn $true``` anyway...). – mclayton Dec 12 '19 at 16:45
  • 1
    Does this answer your question? [Remove Class from Memory in PowerShell](https://stackoverflow.com/questions/42838107/remove-class-from-memory-in-powershell) – Lance U. Matthews Dec 16 '19 at 23:25

0 Answers0