Solidity Basics

Thanks a lot for your reply. For the past days I have ben doing “Print as PDF” and save each program in a PDF file… Didn’t know about remixd, will check it out now. Cheers!

1 Like

Hi Filip!

How can we return a struct instance from a function? Trying to return people[index] like this does not compile

pragma solidity 0.5.12;

contract HelloWorld {

struct Person {
        uint id;
        string name;
        uint age;
        uint height;
}

Person[] public people;

function getPerson(uint index) public view returns (Person) {
        return people[index]; 
    }
}

A post was merged into an existing topic: Solidity basics (by @jon_m) - Optimization / gas / Type

6 posts were merged into an existing topic: Solidity basics (by @jon_m) - Optimization / gas / Type

Hi @0xinu

Returning a structure was a “non supported feature” in some beta version of solc 0.5.x compiler .
I m not sure it is supported by the version 0.6.x
Actually the best way to achieve this is to return the structure elements:

function getPerson(uint index) public view returns (uint, string, uint, uint) {
     Person memory p = people[index];
     return (p.id ,p.name, p.age, p.height);
    }

Hello Filip. Can you help…i am on the ARRAY module in Remix. I have followed your code with the
"getNumber, setNumber is it for eg setNumber 10.1 or 10,1 etc…i have tried all variances and it won’t work. I am on the 5.12 version which you suggested and it compiled ok so i am baffled here anyone?

Thanks

@NetworkP post your code please

1 Like

Dani, thanks for reaching out, hope all is well and you are staying safe.

Below is my code you requested. I am currently coding HTML CSS and JScript which i am doing well at building websites and apps. I recently just days ago decided i’ll start backend programming also in order to naturally link a smart contract to the websites but have come across a stumble.

pragma solidity 0.5.12;

contract HelloWorld{
string public message=“Hello World again”;

uint[] public number = [1,20,45];

function getMessage() public view returns(string memory){
return message;
}

function setMessage(string memory  newMessage) public {
    message = newMessage;
}  

function getNumber(uint index) public view returns(uint){
return number[index];
}

function setNumber(uint newNumber, uint index) public {
    number[index] = newNumber;
}

}

    When i compile it, the "getNumber" function works but the "setNumber" function doesn't 

Thanks again.

Hi @NetworkP

It works if the index exist, so you can change the number for index 0, 1 , 2

But if you want to add a new element in your array you will have to push it to the array.


uint[] public number = [1, 2, 3];

function getNumber(uint index) public view returns(uint){
    return number[index];
}

// Only works for the existing index 0,1,2
function setNumberWithIndex(uint newNumber, uint index) public {
    number[index] = newNumber;
}

/*
* Allow you to append a new number to the array 
*  so after this call you can modify index 3
*/
function setNumber(uint newNumber) public {
    number.push(newNumber);
}

Hi gabba

Thanks for reaching out. Maybe i need to explain myself more clearly. The code compiles and works but for some reason only the GetNumber function works but the SetNumber function doesn’t.

I have tried 10.1 for eg and then tried a comma instead of a fullstop but still no luck.

My code is below and thanks.

pragma solidity 0.5.12;

contract HelloWorld{
string public message=“Hello World again”;

uint[] public number = [1,20,45];

function getMessage() public view returns(string memory){
return message;
}

function setMessage(string memory  newMessage) public {
    message = newMessage;
}  

function getNumber(uint index) public view returns(uint){
return number[index];
}

function setNumber(uint newNumber, uint index) public {
    number[index] = newNumber;
}

}

Oh ok, i didn’t get this was the issue, there is no floating number in solidity.
So you will have to enter 1010 and assume in your frontend you are dealing with decimals :slight_smile:

1 Like

Hi Gabba. Strange one. It’s not the code in Solidity itself it’s the constructs after you have compiled the program. Where you test that the code works. The “get” and “set” constructs that we build via the code. “Arrays” Lesson. I’m not sure i am clear on my explanation. I’ll try and mail filip direct but thanks for reaching out. :slight_smile:

I received this error msg: browser/HelloWorld.sol:22:24: TypeError: Member “lenght” not found or not visible after argument-dependent lookup in struct HelloWorld.Person storage ref[] storage ref.
newPerson.id = people.lenght;
^-----------^
I tried both methods ending with same error
my code is

pragma solidity 0.5.12; 

contract HelloWorld{
    
    //struct can consist of string, integer(uint), address
    //defines structure of this person with these properties
    struct Person{
        uint id;
        string name;
        uint age;
        uint height;
    }   
       // address walletAddress;
        
    
    //create and save instances in a array
    Person[] public people;
    function createPerson(string memory name, uint age, uint height) public{  
        //people.push( Person(people.lenght,name, age, height) );
        Person memory newPerson;
        newPerson.id = people.lenght;
        newPerson.name = name;
        newPerson.age = age;
        newPerson.height = height;
        people.push(newPerson);
    
    } 
}

You’ve spelt length incorrectly in both methods. If you correct this then that should sort it.

4 Likes

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