code/vegetable-class.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
[cmdletbinding()]
Param()
#region class definitions

#only add the class definition if it doesn't already exist in the current session
Write-Verbose "Defining PSTeachingTools.PSVegetable class"
Try {
     [void]([PSTeachingTools.PSVegetable].name)
     Write-Verbose "The class already exists in this session."
}
Catch {
    Write-Verbose "Adding class definition"
    Add-Type -path $PsscriptRoot\psteachingtools.cs
}

#this is the previous PowerShell class definition of the vegetable class
<#
 
#enumerations for a few of the class properties
Enum Status {
    Raw
    Boiled
    Steamed
    Sauteed
    Fried
    Baked
    Roasted
    Grilled
}
 
Enum VegColor {
    green
    red
    white
    yellow
    orange
    purple
    brown
}
 
#a class to define a new type of object
Class Vegetable {
 
    #properties
    [string]$Name
    [int]$Count = (Get-Random -minimum 1 -maximum 20)
    [int]$UPC
    [Status]$CookedState
    [boolean]$IsRoot
    [boolean]$IsPeeled
    [VegColor]$Color
 
    #methods
    [void]Peel() {
        $this.IsPeeled = $True
    }
 
    [void]Prepare([status]$State) {
        $this.CookedState = $State
    }
 
    #constructors
    Vegetable ([string]$Name, [boolean]$IsRoot, [vegcolor]$Color, [int]$UPC) {
        $this.name = $Name
        $this.IsRoot = $IsRoot
        $this.Color = $Color
        $this.upc = $UPC
    }
 
    #an empty constructor
    Vegetable () { }
}
 
#endregion
#>