The first thing to realize is that a PSClass definition is a Powershell CmdLet. It is all Powershell syntax and plays by the rules of any script. New-PSClass is a CmdLet function with the following signature:

    New-PSClass [-ClassName] <string> [-Definition] <scriptblock> [-Inherit <string>]

New-PSClass returns a PSObject with a New Method, defined Static Methods, Properties, and Note Variables. There are other members documented in here.

The ClassName and Definiton are required. Because it's a CmdLet call, it won't automatically look for the script block unless you have an open bracket '{' or the Powershell continuation character `. It's a matter of style but I prefer the open bracket.

As a result the following scripts are equivalent:
$TestClass = New-PSClass Test {
  note MyVar 

  constructor { param ([string]$testVal)
    $this.MyVar = $testVal
  }
  
  method AppendMyVar { param([string]$moreVal)
    $this.MyVar += $moreVal
  }
}

and

$TestClass = New-PSClass Test `
{
  note MyVar 

  constructor `
  { 
    param ([string]$testVal)
    $this.MyVar = $testVal
  }
  
  method AppendMyVar `
  { 
    param([string]$moreVal)
    $this.MyVar += $moreVal
  }
}

Last edited Nov 4, 2009 at 3:46 PM by cashfoley, version 3

Comments

No comments yet.