i m using remix IDE to deploy a smart contract and i m passing a string[] which will contains candidatesnames like so ["alice","bob"] ....
this is my smart contract
pragma solidity ^0.4.18;
// We have to specify what version of compiler this code will compile with
contract Voting {
/* mapping field below is equivalent to an associative array or hash.
The key of the mapping is candidate name stored as type bytes32 and value is
an unsigned integer to store the vote count
*/
mapping (bytes32 => uint8) public votesReceived;
/* Solidity doesn't let you pass in an array of strings in the constructor (yet).
We will use an array of bytes32 instead to store the list of candidates
*/
bytes32[] public candidateList;
/* This is the constructor which will be called once when you
deploy the contract to the blockchain. When we deploy the contract,
we will pass an array of candidates who will be contesting in the election
*/
function Voting(string[] candidateNames) public {
for(uint i = 0; i < candidateNames.length; i++) {
candidateList[i]= stringToBytes32(candidateNames[i]);
}
}
function totalVotesFor(bytes32 candidate) view public returns (uint8) {
return votesReceived[candidate];
}
function stringToBytes32(string memory source) returns (bytes32 result) {
bytes memory tempEmptyStringTest = bytes(source);
if (tempEmptyStringTest.length == 0) {
return 0x0;
}
assembly {
result := mload(add(source, 32))
}
}
function voteForCandidate(bytes32 candidate) public {
votesReceived[candidate] += 1;
}
}
but i m having this error that i didn't know how to solve
UnimplementedFeatureError: Nested arrays not yet implemented.
Can anyone help me plz
As the error suggests, Solidity doesn't support passing in an array of arrays (a string
is just a bytes array).
From the Solidity docs:
Is it possible to return an array of strings (string[]) from a Solidity function?
Not yet, as this requires two levels of dynamic arrays (string is a dynamic array itself).
What you CAN do is change it to an array of bytes32 (which are strings) and send the array of hex for your parameters:
pragma solidity ^0.4.19;
contract Names {
bytes32[] public names;
function Names(bytes32[] _names) public {
names = _names;
}
function get(uint i) public constant returns (bytes32) {
return names[i];
}
}
Deploy with
["0x616c696365000000000000000000000000000000000000000000000000000000","0x626f620000000000000000000000000000000000000000000000000000000000"]
You can't pass a String[] in solidity but you can pass multiple string ! my idea is to pass your string (string1 string2 ,string3 ,string4....) and then convert the string to byte32 with this function
function stringToBytes32(string memory source)view public returns (bytes32 result) {
bytes memory tempEmptyStringTest = bytes(source);
if (tempEmptyStringTest.length == 0) {
return 0x0;
}
assembly {
result := mload(add(source, 32))
}
}
and then put your converted stringtobyte32 in a Byte32[] !
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With