Define a class with mutable variable

Category:
Defining Types
Description: Define a class with mutable variable
Code:
type BuilderState =
| None
| Active
| Completed

type Builder() =
let mutable state = None
member this.CurrentState
with get() = state

member this.Begin() =
state <- Active

member this.End() =
// complete the build process...
state <- Completed

let ClassSample2() =
let b = Builder()
printfn "current state = %A" b.CurrentState
b.Begin();
printfn "current state = %A" b.CurrentState
b.End();
printfn "current state = %A" b.CurrentState

Execution Result:
current state = None
current state = Active
current state = Completed

Last edited Sep 14, 2011 at 2:39 AM by ttliu2000, version 1

Comments

No comments yet.