Solidity Basics

Hi @rostyslavdzhohola

  1. Solidity does not have any inbuilt function for data types conversions
    You can use this oracle: https://github.com/provable-things/ethereum-api/blob/master/oraclizeAPI_0.5.sol#L1045

and the function uint2string()
also refer: https://ethereum.stackexchange.com/questions/6591/conversion-of-uint-to-string

  1. For concatenating two string:
pragma solidity >=0.4.0 <0.6.0;

library ConcatHelper {
    function concat(bytes memory a, bytes memory b)
            internal pure returns (bytes memory) {
        return abi.encodePacked(a, b);
    }
}

source: Solidity Basics

2 Likes

Iā€™m on the HelloWorld contract lesson - it wonā€™t let me deploy. I keep getting a message at the bottom of the page that says: "creation of HelloWorl errored: TypeError: canā€™t convert undefined to object.

My code matches up - not sure what is going on. Any help?

Hiā€¦I dont see my contracts name on Hello Word??? wonder if its my Browserā€¦not sure.
I selected JavaScript VMā€¦but no contract name shows.

browser/HelloWold.sol:1:1: ParserError: Source file requires different compiler version (current compiler is 0.5.12+commit.7709ece9.Emscripten.clang - note that nightly builds are considered to be strictly less than the released version
pragma solidity 0.5.12+commit,
^ (Relevant source part starts here and spans across multiple lines).

Hi @vesta
Thanks for reaching out!
I too do not see any error in your smart contract.
You can try refreshing the browser or try another one.

I tried this contract and works for me :slight_smile:


pragma solidity 0.5.12;

contract HelloWorld {

    string public message = "Hello World";

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

@Maximus
Thanks for reaching out!
add in your top line of smart contract:


pragma solidity ^0.5.12;

The error says, you need different or upgrade version of the compiler.

Hope this answers the question.

1 Like

@Kristhofer_Drost1
Thanks for reaching out!
I am the new mentor, can you help me with the video reference?

Thank you

1 Like

I closed the browser down, then reopened - it finally worked. Thank you! :smiley:

1 Like

Thank you Taha

I forgot the ^ and then had to set my Compiler again. Now I can move on.

1 Like

@Taha Sorry if I havenā€™t answered more earlier. I have already checked and solved with Filip wrote in the video, which is programming with require() assert.

1 Like

Hey Filip, Iā€™m just wondering about minimizing code. In one of the earlier lessons you showed 2 examples of one line of code I think it was, one was minimized and the other was over 2 or 3 lines I think. What Iā€™m asking is, does solidity do some minimization in the background or is it good practice to do so in code?

@Dan_O_Kane
Solidity does not do any minimisation or optimisation of code. You will have to try to optimise your program which come with experience and lots of reading :slight_smile:

One hint is: You can check how to write solidity programs which would consume least amount of gas

reference:https://medium.com/coinmonks/8-ways-of-reducing-the-gas-consumption-of-your-smart-contracts-9a506b339c0a

1 Like

Thanks. It is helpful.

1 Like

people.push(Person(people.length, name, age, height));

I have another problem

@Maximus
Its completely fine to have errors.
So what error is remix or truffle showing?

If you can elaborate a bit?

Thanks

1 Like

people.push(Person(people.length, name, age, height));

@Maximus
Could you share the complete code?

1 Like

Hey Philip,

Enjoying the Solidity course so far just had one question. You showed that there are 2 ways to write out the createPerson function:

The short way


(eg. people.push(Person(people.length, name, age, height));

the long way

(eg. 
Person memory newPerson
       newPerson.id = people.length;
       newPerson.name = name; 
        etc
       people.push(newPerson);

Is there a reason that Person is used for the 1st method and newPerson for the 2nd way?

Thanks,

Good day @keithra1948.

Yes there is a difference between the two.

The first example (short way) creates a new structure [Person] object and immediately adds it to the array without assigning it to a variable first.
The second example (long way) he first creates a variable from structure [Person], and gave the variable the name ā€œnewPersonā€.
You will see that newPerson gets populated later on before being added to the array.

Hope this example makes sense:

Person memory newPerson = new Person(people.length, name, age, heigh);
people.push(newPerson);

I hope the above makes sense to you. Let me know if it doesnā€™t.

2 Likes

@keithra1948
Thanks for reaching out!
Yes, there are quite a few differences in the two examples you mentioned.

  1. First example is of storage which will store the properties of the struct in the EVM, eventually consumes more gas
  2. Second example is a typical memory called newPerson whose values are stored in run-time only and are removed once the contract execution is over hence consumes less gas.
    Finally, The newPerson details are pushed to the array in both cases.
3 Likes

@nielvosloo
You are right with the first example :+1:
The second example is a memory:slight_smile:

2 Likes