0

How do I create a constructor in solidity?

My goal is to have values initialized when the smart contract is deployed. As a java programmer, I default to a constructor to handle this.

contract NimGamev3 {

...

constructor(NimGamev3) internal {

...

}

...

}

I've read that you should use the constructor keyword to create a constructor to initialize values. This doesn't raise any errors when using remix. When I copy the code to Ethereum Wallet I get an error, "Could not compile source code. Expected identifier, got 'eth_compileSolidty' constructor(NimGamev3) internal {"

When I use a function to initialize variables, it is not called on when the contract is deployed, and if I make it public, it can be initialized multiple times which is not the intended function of the constructor.

Note: I am currently using 0.4.21

1

The use of the name constructor for the constructor is in the version 0.4.22 of Solidity. You are using 0.4.21, which doesn't implement it.

  • Changing my version of solidity in ethereum wallet does not fix the compile error. Same error message as well. (I changed it to "pragma solidity ^0.4.22" and to "...^0.4.23") – Fortune Apr 28 '18 at 22:23
  • Can you share your code? – Eli Drion Apr 28 '18 at 22:33
  • I reloaded the deploy contract page, and it might have just worked. Let me test this for a few more minutes and I will give you and updates. Thanks! – Fortune Apr 28 '18 at 22:37
  • Yes! This worked, the issue was ethereum wallet was not recompiling it after I changed the version of solidity. Thank you! – Fortune Apr 28 '18 at 22:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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