3

I'm testing a very simple example

contract MyNewTest {

Student[] students;
mapping (string => uint) nametonumber;
mapping (uint => string) numbertoname;
uint counter;

struct Student
{
    string name;
    uint num;
}

function addStudent(string _name)
{
    uint _id;
    _id = students.length++;
    students[_id].name = _name;
    students[_id].num = _id;
    numbertoname[_id] = _name;
    nametonumber[_name] = _id;
}

function getLength() constant
returns (uint)
{
    return students.length;
}

function getStudentNumber(string name) constant
returns (uint)
{
    return nametonumber[name];
}

function getStudentName(uint number) constant
returns (string)
{
    return numbertoname[number];
}

function getStudentAtPosition(uint x) constant
returns (string)
{
    return students[x].name;
}

function getStudentNumberAtPosition(uint x) constant
returns (uint)
{
    return students[x].num;
}
}

Am running a private local blockchain with mining turned on.

I tried to call

mynewtest.addStudent("Joe")

then

mynewtest.getLength() 

it returns Zero although I added a new student.

However, inside the newStudent function if I remove either

numbertoname[_id] = _name;

or

nametonumber[_name] = _id;

everything works fine and when I add a student and do

mynewtest.getLength()

I get 1.

is there a constraint that I can't update two mappings in the same function? Does this code work for anybody?

Sebi
  • 5,294
  • 6
  • 27
  • 52

1 Answers1

2

I quickly tested in browser-solidity your code and was able to addStudent.

Your transaction to addStudent probably does not have enough gas: if you're using web3.js mynewtest.addStudent("Joe", {gas:2000000 ...}, ...)

eth
  • 85,679
  • 53
  • 285
  • 406