How can I check if an Contract variable is undefined in Solidity?
- 85,679
- 53
- 285
- 406
- 3,027
- 3
- 20
- 37
2 Answers
According to this
Memory-stored objects as local variables are correctly zero-initialised: Members of structs and elements of fixed-size arrays are recursively initialised, dynamic arrays are set to zero length. delete x assigns a new zero-initialised value to x.
There is no such undefined variable. All objects are zero-initalised.
By some Solidity coding conventions you add a boolean 'valid' element to any struct to indicate it is undefined (false is the initialized value).
Sometimes I see fields having zero reserved to mean undefined - e.g. treat 0 as an undefined address. You could also do that for a string, but string is often externally input so you have to make sure someone didn't send you a zero string.
I would advise using a field where zero is naturally meaningful like a separate bool or an existing address element in a struct.
- 4,271
- 1
- 22
- 48
-
1This is not useful for a pure Contract object isn't it? – arodriguezdonaire Mar 31 '16 at 14:31
-
what do you mean by a "pure Contract object"? – Paul S Mar 31 '16 at 19:44