Data Types
Data Types Example
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
// Enum data type
enum Color { Red, Green, Blue }
contract Example {
// State variables
uint public number;
bool public isTrue;
string public message;
address public owner;
address payable public payee;
bytes32 public data;
fixed256x18 public fixedNumber;
Color public color;
// Struct data type
struct Person {
string name;
uint age;
uint[] favoriteNumbers;
}
// Array data type
uint[] public numbers;
// Mapping data type
mapping(address => uint) public balances;
// Constructor
constructor(uint _number, string memory _message, address _owner) {
number = _number;
message = _message;
owner = _owner;
color = Color.Red;
}
// Function that uses a struct
function addPerson(string memory _name, uint _age, uint[] memory _favoriteNumbers) public {
Person memory newPerson = Person({name: _name, age: _age, favoriteNumbers: _favoriteNumbers});
}
// Function that uses an array and a mapping
function addNumber(uint _number) public {
numbers.push(_number);
balances[msg.sender] += _number;
}
}
Data Types Example 2
Last updated