in

Can’t compile simple contract. Doesn’t like when I put ‘public’

I’m following along with a video tutorial and have made this contract to understand different types of variables:

pragma solidity ^0.6.0;

contract MyContract {
// state variable
int public myInt = 1;
uint public myUint = 1;
uint256 public myUint256 = 1;
uint8 public myUint8 = 1;
string public myString = ‘Hello World’;
bytes32 public myBytes32 = ‘Hello World’;
address public myAddress = 0xDf9b1C53F80f465A60f56f0670B3C4EA92d56498;

// structure (like a class)
struct MyStruct {
uint myInt;
string myString;
}

MyStruct public myStruct = MyStruct(1, “hello world”);

// local variable
function getValue() public pure returns(uint) {
uint value = 1;
return value;

// array
uint[] public uintArray = [1, 2, 3];
string[] public stringArray = [‘Apple’, ‘Banana’, ‘Carrot’];
string[] public values;

// 2 dimensional array (array of arrays)
uint[][] array2D = [ [1, 2, 3], [4, 5, 6] ];

function addValue(string _value) public {
values.push(_value);
}

function valueCount() public view returns(uint) {
return values.length;
}

// mapping
}
}

I get this error:

contracts/Tutorial/MyContract.sol:27:12: ParserError: Expected ‘;’ but got ‘public’
uint[] public uintArray = [1, 2, 3];
^—-^

And if i remove `public` from all the array lines it continues but then throws this error:

contracts/Tutorial/MyContract.sol:34:14: ParserError: Expected ‘(‘ but got identifier
function addValue(string _value) public {
^——^

If I remove public from this function I get the following error:

contracts/Tutorial/MyContract.sol:34:14: ParserError: Expected ‘(‘ but got identifier
function addValue(string _value) {
^——^

Can arrays not be `public`? and what is wrong with that function? Am I missing something obvious?

What do you think?

10 Points
Upvote Downvote

Leave a Reply

Your email address will not be published. Required fields are marked *

GIPHY App Key not set. Please check settings

One Comment

Angry Mosquito | Fair Launch 11th Aug 6pm UTC | Liq locked | Ownership renounced | Charity token

IQIYI, Inc ($IQ) – DD – Part 3 – Now with sensible China talk