Reputation: 21
I have a short question. Why can't I change the value of the var Test in a if statement?
if Status == 1{
var Test = 1
}
else{
var Test = 2
}
println(Test) // Error: Use of unresolved identifier 'Test'
Upvotes: 0
Views: 1844
Reputation: 25595
because Test
is out of your scope. Test
is defined in two different if(){}
scopes.
Declaring Test
outside of the if()
scope will allow you to access it in a broader scope.
var Test :Int
if Status == 1{
Test = 1
}
else{
Test = 2
}
println(Test)
EDIT: A undeclared variable (Test) cannot be inferred from, therefore recommend to specify variable type (:=Int for integer). If there is any other type of value, an error will be displayed.
Upvotes: 7