Solidity Basics

I do not understand why bool comes back false at age 65…

if(age >= 65){
           newPerson.senior = true;
       }
       else{
           newPerson.senior = false;
       }
        
    
    } 
    
    function getPerson() public view returns(string memory name, uint age, uint height, bool senior){
        address creator = msg.sender;
        return (people[creator].name, people[creator].age, people[creator].height, people[creator].senior);
        
    }
}
  • 0: string: name Georg
    *** 1: uint256: age 65**
    *** 2: uint256: height 156**
    *** 3: bool: senior false**

Hi @cherrybluemoon
Are you using the exact same contract as in the previous message ?
You need to add

bool senior;

In your structure.
Your condition should works.

Yes, bool senior is un the struct and function getPerson

contract HelloWorld{
    
    //struct can consist of string, integer(uint), address
    //defines structure of this person with these properties
    struct Person{
        
        string name;
        uint age;
        uint height;
        bool senior;
    }   
       // address walletAddress;
        
    
    //mapping address 
    mapping(address => Person) private people;
    
    function createPerson(string memory name, uint age, uint height) public{
        
        
        Person memory newPerson;
        newPerson.name = name;
        newPerson.age = age;
        newPerson.height = height;
        //how to assign a newPerson in the mapping
        //when input address will get back newPerson
        
        
        if(age >= 65){
           newPerson.senior = true;
       }
       else{
           newPerson.senior = false;
       }
       insertPerson(newPerson);
       
    }
    //internal private
        function insertPerson(Person memory newPerson) private{
           address creator = msg.sender;
           people[creator] = newPerson;
    
    } 
    
    function getPerson() public view returns(string memory name, uint age, uint height, bool senior){
        address creator = msg.sender;
        return (people[creator].name, people[creator].age, people[creator].height, people[creator].senior);
        
    }
}

@cherrybluemoon
I just tried your code in remix and it’s working

createPerson

name: qwe
age: 65
height: 123
transact

getPerson

  • 0: string: name qwe
  • 1: uint256: age 65
  • 2: uint256: height 123
  • 3: bool: senior true
2 Likes

@gabba
Ok, Thanks for helping. I misread my own code. I thought I wrote if(age>=60), but actual was if(age>=65)
Probably a lack of sleep…

What helped me to understand this, is that it seems really similar to when you create a class in JavaScript, and then use that class as a template to construct multiple object instances, thereby saving you having to write lots of repetitve code e.g.

class Person {
   constructor(name, age) {
      this.name = name;
      this.age = age;
      this.senior = age >= 65;
   }
}

const personA = new Person('Bob', 36); 
const personB = new Person('Alice', 68);

console.log(personA);  // Person { name: 'Bob', age: 36, senior: false }
console.log(personB);  // Person { name: 'Alice', age: 68, senior: true }

console.log(
   `${personA.name} ${personA.senior ? 'is a senior' : 'isn\'t a senior'}`
);
                       // -->  Bob isn't a senior

console.log(
   `${personB.name} ${personB.senior ? 'is a senior' : 'isn\'t a senior'}`
);
                       // -->  Alice is a senior

I do realise that classes aren’t covered in the JavaScript for Blockchain Developers course, and so some people may not be familiar with these; however, even though there are differences in syntax, would you agree that parallels can be drawn between how classes in JavaScript and structs in Solidity work?

2 Likes

Hi all,

Does anybody know how come my code isn’t correct?

Looks like solidity doesn’t like ". lenght " with either the first method or the second in the comment //.

1 Like

Hi Luca.

I see you have misspelled the word length.
That’s probably why it throws the error. :wink:

Ivo

2 Likes

Oh for f… sake ahahahah Thank you very much!
I coudn’t understand why! it is a bit embarrasing tho ahaha nevermind

2 Likes

Haha. We have all been there and done that… And we will all do it again and again…
So don’t worry, cuz nobody really cares, we are just glad It was you this time and not one of us.:crazy_face:

Keep up the good work.

Ivo

3 Likes

Screen Shot 2020-04-04 at 23.34.08

Hello, i cant see the error with this, can someone help. It says its on line 23 but it thought i needed braces there.

1 Like

what does the error message in remix say? the red x…

I’ll give u one clue. Indentation. Your error is not at line 23, it starts at line 14 i think.
I believe solidity needs the indentations to be correct to run. I might be wrong but it’s worth a try I think. :wink:

Good luck.
Ivo

1 Like

Hi @Mark1
Solidity doesn’t take the indentation in consideration, but indeed look at where you are openning your functions and where you are closing it.

You will see that a line 14 you are closing the function setMessage, using a better indentation will help you to see that you are not closing your contract bracket. This is your error.

3 Likes

Thank you, will take a look.

Thank you. Was doing this in bed last night, was like spot the difference game with mine and @filip code :slight_smile:

1 Like

OMG! Filip! This is awesome to have this utility “remix” to build smart contracts. It was in 2017 when I started reading about (blockchain) smart contracts (POW/POS). Now, I get my first opportunity to learn what its all about. Thanks Ivan on Tech Academy!

1 Like

Does anyone know how to backup smart contracts written in Remix locally?

2 Likes

Hi David.

That’s right, In Remix we can open files form local storage, but not save to local storage, It’s strange I know… So my solution to just copy&paste the code into Atom editor.

That’s how I do it. I hope this helps you!

Ivo

2 Likes

Thanks Ivo, good idea.

1 Like

I cannot get the remix IDE section to color code my program text like in your videos. It is all black only even with .sol extension

2 Likes