1

I want to know how to initialize the structure created using 'struct' key word.

I can't see use of 'new' key word in Solidity documentation.

Some body please guide me.

Zulfi.

1 Answer 1

3

Like this:

struct Example {
    uint a;
    uint b;
    uint c;
}

Example public example;

function set1(uint _a, uint _b, uint _c) public {
    example.a = _a;
    example.b = _b;
    example.c = _c;
}

function set2(uint _a, uint _b, uint _c) public {
    example = Example(_a, _b, _c);
}

function set3(uint _a, uint _b, uint _c) public {
    example = Example({a: _a, b: _b, c: _c});
}
2
  • 1
    This means that there is no use of 'new' keyword for struct data type. Thanks for removing my confusion.
    – zak100
    Commented Mar 10, 2020 at 22:14
  • 2
    @zak100: No, the new keyword is used for instantiating a new contract, for example: MyContract myContract = new MyContract(1, 2, 3);. In other words, it is used for deploying a contract on-chain (i.e., from another contract) instead of off-chain (e.g., from a web3-based NodeJS script). Commented Mar 11, 2020 at 5:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.