I have the following struct defined as a state variable and conditionally set at runtime.
Transaction private tx;
struct Transaction {
address to;
uint value;
bytes data;
}
I would like to check if the variable was set before executing a specific portion of my code.
if(tx == ??????){
//do something
}
What's the best way to check if the struct is set?
Edit: It is important to note that here the struct is not stored in a mapping. Similar questions on this stackexchange are asking for structs in mapping, here this is not the case.
Best Answer
Question is possibly a duplicate of this: What is the zero, empty or null value of a struct?
There are a few ways.
You can check a value if you're sure it implicitly indicates a set/not set condition.
You can be explicit if you prefer:
Then you can:
You'll want to use the explicit method in the case that 0 or empty has meaning your application; meaning you can't use 0 as an indication of valid/inValid.
Not sure it applies in this case, but elimination of doubt about this sort thing is a bonus feature if you buy into the idea of maintaining an index of valid keys to a mapped structure.
Hope it helps.
Update: Some general examples here: Blog: Simple Storage Patterns in Solidity