7

I am just starting out with Solidity and JavaScript and am trying to understand Enums. I have a simple contract

pragma solidity ^0.4.4;

contract SimpleEnum {
  enum SomeData {Channel}
  SomeData sd;

  function set_values(uint a) {
    sd.Channel = a
    return sd
  }
}

I am trying to figure out how I can set the Channel, but it does not seem to be working.

12

You have some errors, so consider reading the solidity docs. Also, remix is a convenient tool for coding and debugging and coding exercises. I modified your code, as follows:

pragma solidity ^0.4.4;

contract SimpleEnum {

  enum SomeData {DEFAULT,ONE,TWO}
  SomeData someData;

  function SimpleEnum(){
      someData = SomeData.DEFAULT;
  }

  function setValues(uint _value){
      require(uint(SomeData.TWO) >= _value);
      someData = SomeData(_value);
  }

  function getValue() constant returns (uint){
      return uint(someData);
  }
}

Hope it helps~

update

About Enums, described below:

enums are one way to create a user-defined type in Solidity. They are explicitly convertible to and from all integer types but implicit conversion is not allowed. The explicit conversions check the value ranges at runtime and a failure causes an exception. Enums needs at least one member.

Enum values are numbered in the order they're defined, starting at 0. If you want to get value of an enum, do uint(enum_variable).

  • thanks -- i realized that i am not even using the correct type for my problem. if i change this question to be about enums (which is what you helped me with) and not testing, can you make any edits necessary to bring it more in line with the new question? then i'll select this as an answer. – Wapiti Aug 10 '17 at 15:24
  • Yes, of course~ – BinGoBinBin Aug 10 '17 at 15:29
  • Awesome! Thanks. Like I said I'm just starting out. Do you mind changing the variable name to SomeData, the way I did? That is generic, and I think that it will be less confusing for another newbie that way. – Wapiti Aug 10 '17 at 15:51
  • had modified variable~ – BinGoBinBin Aug 10 '17 at 16:00
3

Refer enum documentation,

pragma solidity ^0.4.16;

contract test {
enum ActionChoices { GoLeft, GoRight, GoStraight, SitStill }
ActionChoices choice;
ActionChoices constant defaultChoice = ActionChoices.GoStraight;

function setGoStraight() public {
    choice = ActionChoices.GoStraight;
}

// Since enum types are not part of the ABI, the signature of "getChoice"
// will automatically be changed to "getChoice() returns (uint8)"
// for all matters external to Solidity. The integer type used is just
// large enough to hold all enum values, i.e. if you have more values,
// `uint16` will be used and so on.
function getChoice() public view returns (ActionChoices) {
    return choice;
}

function getDefaultChoice() public pure returns (uint) {
    return uint(defaultChoice);
}


}

From this official example, it turns out that function getChoice() returns the index of that action. So I guess enum is sort like list in python, which convert the values inside to the corresponding index.

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.