11

How can I check if an Contract variable is undefined in Solidity?

eth
  • 85,679
  • 53
  • 285
  • 406
arodriguezdonaire
  • 3,027
  • 3
  • 20
  • 37

2 Answers2

8

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.

Loi.Luu
  • 2,103
  • 3
  • 13
  • 16
4

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.

Paul S
  • 4,271
  • 1
  • 22
  • 48