2

Is there any way I can give a list of values (array or mapping) to the constructor of a contract?

In the style of the following code:

contract myContract {

    string[] public myArray;

    function myContract(string[] myArray) {
        myArray = myArray;
    }
}

I currently get the message from the online compiler:

Error: Internal type is not allowed for public or external functions.

jrbedard
  • 524
  • 1
  • 6
  • 15
Samuel Barnes
  • 881
  • 10
  • 26

1 Answers1

2

it works if you change to int,address (which doesn't have a dynamic size as the string)or bytesX (fixed byte array)

  string[] public myArray;

  function   myContract (int[] myArray)  {

  }

so to understand why there is 2 answers :

1-in the doc :

generic arrays of fixed and dynamic size are supported in calldata and storage with the following features: Index access, copying (from calldata to storage, inside storage, both including implicit type conversion), enlarging and shrinking and deleting. Not supported are memory-based arrays (i.e. usage in non-external functions or local variables)

2-

Solidity doesn't have a built-in way yet to deserialize string arrays. It can cheat when the contract is passing an array it created itself to itself, which is why it works with private functions. But if you want to take string arrays from the outside, you're going to have to handle the serialization and deserialization from a string (or bytes) yourself. (E.g., concatenating fixed-length items, or using null characters as separators, or using commas, etc.)

source: Is it impossible to use an array of strings as the argument to solidity function?

Badr Bellaj
  • 18,780
  • 4
  • 58
  • 75