Contracts, Functions, State Variables, Getters & Setters

As for your first comment, about the create button, I still don’t understand the issue. Nobody has explained it to me. The button seems to have been renamed to deploy as I previously said and you doesn’t seem to have a problem deploying the contracts, am I right?

For your second issue, the problem is that you have an old compiler version. You can do 2 things to fix it, either use the “constant” modifier instead of “view”. Or select a newer compile version under the compile tab, one that is over 0.4.15 I believe.

1 Like

Correct. deploying is not an issue. using “constant” instead of “view” is solving the issue. although selecting a compiler even over 0.4.15 still doesnt allow me to use view. for simplicity, I will continue to use 0.4.0 and hope it doesnt require much else.

Thank you filip!

1 Like

I was actually wrong in my answer, it is version 16 and above. I tried now with v0.4.16+commit.d7661dd9, and that worked for me. Did you select the compiler version in the dropdown list in the compile tab?

1 Like

Yes, this is working well.

1 Like

whats the problem with my code I’m getting and error message
browser/new.sol:31:1: ParserError: Expected pragma, import directive or contract/interface/library definition.
}pragma solidity ^0.4.0;

contract Person{
string name;
uint age;

function getName() view returns (string) {
    return name;
}

function getAge() view returns(uint){
    return age;
}
function setName(string _name){
    name = _name;
}

function setAge(uint _age){
    age = _age;
}

}

You have an error at the beginning of the code. Before pragma you have a squirly bracket. Remove that and you will be fine.

pragma solidity ^0.4.0;

contract Person{

string namePerson = "Inital Name";
int agePerson = 0;

function getName() view returns(string,int) {
    return (namePerson,agePerson);
}

function setOutput(string _nameOutput,int _ageOutput){
    namePerson=_nameOutput;
    agePerson=_ageOutput;
}

}

1 Like

Here is my contract

pragma solidity ^0.4.0;

contract Age{

uint Age = 1;
string Name = "Name";

function setName (string _Name) returns (string){
    Name = _Name;
}

function setAge (uint _Age) returns (uint){
    Age = _Age;
}

function getName (string)view returns (string){
    return (Name);
}

function getAge (uint) returns (uint){
    return (Age);
}

}

Couldn’t use “view” because of this error

You have forgot to add a name to the argument to the getName function. It should look something like this:

function getName (string _name) view returns (string){
    return (Name);
}

Actually it was like that but no difference error with view hasn’t disappeared

I can see now that you have multiple errors in the code. As for the error with “view”: replace it with constant. It does the same thing but is compatible with the pragma version you are using.

But you shouldn’t have any arguments at all for the getter functions, that doesn’t make sense. You also need to rename Age to something else, since it can’t have the same name as the contract. You can use age, with a non-capital a.

1 Like

Ok I understand this, Thank You!

I also had problems using “view”. I solved it by changing to version 0.4.25.

This is the code for the Name and Age contract…

pragma solidity ^0.4.25;

contract Person{

    string myName = "Fernando";
    uint myAge = 60;

    function getName() view returns (string){
        return myName;
   }   
    function setName(string _myName){
        myName = _myName;
    }

    function getAge() view returns (uint){
        return myAge;
    }   
    function setAge(uint _myAge){
        myAge = _myAge;
   }
}

Great initiative. I hope to update the solidity chapter very soon with new videos. So that we can work with the latest version.

pragma solidity ^0.4.0;

/// @title AllAboutMe
/// @notice Lesson 1 Response
/// @dev Xactant
contract AllAboutMe {
string myName = “NOT SET”;
uint myAge = 0;

function name() view public returns(string) {
    return myName;
}

function age() view public returns(uint) {
    return myAge;
}

function setName(string _name) {
    myName = _name;
}

function setAge(uint _age) {
    require(_age > 0);
    
    myAge = _age;
}

}

Ran into the same issue - in Remix set the compiler version to at least 0.4.16 - will still compile with pragma solidity ^0.4.0;

I couldn’t get it to work at first while I thought the code was good. When I put in my age and name (with quotes) it just returned my default value (1 and test). Then I realized there was an “arrow down” behind the input variables and saw the button “transact”. When I filled in the variables then I had to push the “transact” button and only then my function returned the right value. I didn’t see this in the video, is there a setting that I have to set so I don’t have to do this ?
Oh and my code only compiles with version 0.4.16, not with 0.4.17 or later, I tried different ones.

pragma solidity ^0.4.0;

contract FirstAssignment {

string myName=“test”;
uint myAge=1;

function GetMyName() view public returns (string) {
return myName;
}

function GetMyAge() view public returns (uint) {
return myAge;
}

function SetMyName(string _myName) {
myName = _myName;
}

function SetMyAge(uint _myAge) {
myAge = _myAge;
}

}

pragma solidity 0.5.12;

contract helloWorld{
    string public message = "helloWorld" ;
    
     function getMessage() public view returns(string memory){
        return message;
    }
}
 function setMessage(string memory newMessage) public {
     message = newMessage;
 }
    

I just started the course and came across an error while on the 2nd video for smart contract programing 101.

the error is “browser/HELLOworld.sol:10:2: ParserError: Expected pragma, import directive or contract/interface/library definition. function setMessage(string memory newMessage) public {
^------^”

what am I missing/didn’t do?

any help is appreciated :slight_smile:

Thanks Everyone!

Hello sir, your setMessage function is outside of the square brackets {} of you contract.

All your logic should be inside contract helloWorld{...}

If you have any more questions, please let us know so we can help you! :slight_smile:

Carlos Z.