Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there null like thing in solidity

    struct buyer{        uint amount;        Status status;     }      mapping(address=>buyer) public buyers;     mapping(uint=>address) buyerIndex;     uint public buyerNum;     //Order a product.     function(){       uint doubleValue=value*2;       uint amount=msg.value/doubleValue;        if(buyers[msg.sender]==null){ //Error in this line       buyer abuyer=buyer({amount:amount,status:Status.Created}); //Error in this line       buyerNum++;       buyerIndex[buyerNum]=msg.sender;       buyers[msg.sender]=abuyer;     }else{       buyers[msg.sender].amount+=amount;     }       Order(msg.sender,amount*doubleValue,amount);   } 

If a buyer is not recorded in the buyer mapping, then buyerNum++; but I don't know how to tell whether a buyer is in the mapping

like image 207
Hao WU Avatar asked Jun 16 '16 07:06

Hao WU


People also ask

Is there a null in Solidity?

Unlike other languages(java, javascript), There is null or undefined in solidity. Values are assigned to zero bytes if there is no object.

What is the zero address in Solidity?

Within an Ethereum transaction, the zero-account is just a special case used to indicate that a new contract is being deployed. It is literally '0x0' set to the to field in the raw transaction.

Which is not a valid data type in Solidity?

Boolean: This data type accepts only two values True or False. Integer: This data type is used to store integer values, int and uint are used to declare signed and unsigned integers respectively. Fixed Point Numbers: These data types are not fully supported in solidity yet, as per the Solidity documentation.


1 Answers

In solidity every variable is set to 0 by default.

You should think of mappings as all possible combinations are set to 0 by default.

In your specific case I would use the following:

if (buyers[msg.sender].amount == 0) 
like image 168
Viktor Tabori Avatar answered Sep 27 '22 20:09

Viktor Tabori