0

i have declared a string variable in struct like

struct test{   
     string  testString;
  }

I have written a function to access the variable like

 function getteststring(uint256 index)public returns (string memory)
      {           
              return test[index].testString;            
      }

when i try to call this function using web3.js i get "0xc05205ee00000000000000000000000000000000000000000000000000000000000000c80000000000000000000000000000000000000000000000000000000000000001" , converting this to UTF8 fails Error: Invalid continuation byte

can anyone guide me on how to access this var and convert in readable format

1

test is a struct not an array. You simply can't write something like test[index].

If you want make an array of struct below code might help.

pragma solidity ^0.5.4;

contract A{
    struct test{
        string  testString;
    }
    test[] tests;

    constructor() public {
        tests.push(test("aaa"));
        tests.push(test("bbb"));
        tests.push(test("ccc"));
    }

    function getteststring(uint256 index)public view returns (string memory){
        return tests[index].testString;        
    }
}

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.