Solidity Basics

They @gmarcotte

Remix is great for beginners and to test quick stuff however you will not use it for building real projects.
You will use Truffle which is a development environment and testing framework, Ganache as local blockchain and Atom or VS as text editor.
All these tools are fully explained in Solidity 201 :slight_smile:

Happy learning,
Dani

2 Likes

@filip

Hello Filip!

I used remix before but now I donā€™t understand why the compile and deploy buttons on the left have disappear. How can i get them back?

Besides is not clear to me, when I create a file on remix where it is saved? (I didnā€™t create any remix profile) Also if i close it and reopen I see my files there but I would not like to lose them.

Hey Filip, no questions really. Iā€™m just chomping through the videos, following the instructions, and hoping that at some point in the future it will all fall into place. I must be learning something because I did alright with the quiz.
When do I get to build my ICO?
DOODESVILLE :slight_smile:

1 Like

Hello @enrico, hope you are great.

There should be an issue with your browser and remix, those buttons should always appear. Which browser are you using? can you please try to open remix in another browser?

Your remix files are saved locally on your browser data for some websites, i could suggest to back up all your code in github or make your own manual backup. If for any reason you have to clean your browser (data, cacheā€¦) you might lose your code too.

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

Carlos Z.

2 Likes

Hey @enrico,

You can also try clicking on the plug icon (just above the ā€˜?ā€™ youā€™ve drawn) which will open up the Plugin Manager. You should then be able to manually reactivate your Solidity Compiler, Deploy & Run Transations and Debugger plugins from there.

2 Likes

Hey up everyone, Thanks Filip for doing this course. I am a couple lessons in and it is by far the easiest one to follow. I get its due to actually learning Java, C++ a little in the other courses that things are actually making A LOT more sense to me now. Its funny that only 2 days ago i was tearing my hair out and expressing my saddend self to @Mauro hahaā€¦

My question is a simple one but nessesary for me to follow along 100%. On the setNumber function when i go to deploy it its not working. Is there an issue with my code shown bellow?

pragma solidity 0.5.12; 

contract HelloWorld{
    
    //STATE VARIABLES (DATA THAT IS IN THE CONTRACT STATE)
    string public message = "Hello Ya World"; //(persistant) //state VARIABLES
   
    uint[] public numbers = [1, 20, 45];
  

    //FUNTIONS 
    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 numbers [index];
         
             
         }    
         
         function setNumber(uint newNumber, uint index) public {

             numbers[index] = newNumber; 
             
         }
         
         function addNumber(uint newNumber) public {
             
            numbers.push(newNumber); 
         }
}

Sorry for ā€œThe Bibleā€

1 Like

Hi @Rob_McCourt,

Iā€™ve deployed your contract and the setNumber function is working perfectly.

Are you inputting the arguments (newNumber and index) in the right order when youā€™re calling the function? e.g. if you want to change the 2nd number (20) to 30, you would need to call it with:ā€‚30, 1
If you input the arguments in the wrong order, then you may find that your index is greater than 2, which would throw an error because the final element is currently at index 2.
Unlike JavaScript, Solidity wonā€™t let you add a new element to an array by assigning a value to an index number which doesnā€™t currently exist. To add additional values you need to use the push() array method.

I hope that helps you get your setNumber function working. Let us know if youā€™re still having problems, though.

2 Likes

Hey Filip,
I am back to the beginning again.
Why?
Because remix deleted my code.
I guess this is a monthly clean up or something.
Actually it suits me because I can rebuild the code and that is learning and more practice.
I was just about to do require() and assert() but had no code to work with.
So back to basics.
DOODESVILLE

2 Likes

Hello Filip,
Why am I getting a red mark on lines 18 and 21?

pragma solidity 0.5.12;

contract HelloWorldAgain{

string public message = "Hello World Once 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 numbers[index];
}
function setNumber(uint newNumber, uint index) public {
    numbers[index] = newNumber;
}

}

1 Like

OK, Iā€™m got right through this video second time around.
The compiler did not like ā€œnumbersā€ and gave me a red ticket - I replaced with ā€œnumberā€, then it worked.
DOODESVILLE

1 Like

Hey @doodesville, hope you are good.

Sad to hear this, i could advice you to create your own Github account and then create a repository for your Solidity code and use it has a backup. Remix sadly does store your code into a local cache folder, so if you delete the cache and data of your browser, you also lose your Remix code, so always have a backup of your code.

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

Carlos Z.

2 Likes

Hi there, i redone the contract and hadnt noticed when @filip setNumber he was using a , and not . as what i thought. I had the screen zoomed out to much, My code is working perfectly nowā€¦ Thanks for youre input by the way.

Love this course so far.

1 Like

Yes, you always need to make sure that when referencing a variable name (in this case the array variable) you use the exact same name. Be careful with plurals: you named your array variable number, instead of numbers, and so when you then referenced an array called numbers in the getNumber and setNumber functions, the compiler was telling you that it didnā€™t exist :wink:

1 Like

Can anyone help? i cant seem to resolve thisā€¦(P.S just started learning solidity.)

was following the step by step. help will be appreciated. thanks in advance!

1 Like

pragma solidity 0.5.12;

contract HelloWorld{

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

person[] people;

function createPerson(string memory name, uint age, uint height) public{
    Person memory newPerson;
    newPerson.id = people.legnth;
    newPerson.name = name;
    newPerson.age = age;
    newPerson. height = height;
    
   // people.push(person(people.legnth, name, age, height))
    
}

im getting this error at line 14. can someone help me:

browser/hello world.sol:14:5: DeclarationError: Identifier not found or not unique. person[] people; ^----^

1 Like

i just realized my mistake and fixed it. Cap P

1 Like

Hello @ImRONMAN, hope you are great!.

You have a space between people.push and (Person(..., just delete that space and then it should work.

This is a syntax error, to give you some kind of understanding, the action .push is a property for arrays (add elements), but in general terms, is an internal function of the programming language.
If we think in terms of how you have to invoke a function, the syntax is always the same (in most prog languanges).

Example:

// declaring a function
function myFunction(parameters){
//function body
}

//global variable, we will use it has a function parameter
var myVar = 0; //zero just an example, could be something else
//invoking the function
myFunction(myVar)

Check that i do not have any space on my invoke syntax, in most prog language is like that, so even .push can be seen as a function and you should treat it with the same syntax.

Hope you find this useful.
If you have any more questions, please let us know so we can help you! :slight_smile:

Carlos Z.

Hey everyone, On Mappings.

I have created a new file to do the code inā€¦ I have a parsing error on line 18ā€¦

Does this code look good?

pragma solidity 0.5.12;

contract HelloWorld{

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


mapping(address => Person) public people; 

function createPerson(string memory name, uint age, uint height); 
    address creator = msg.sender;
    
    //This creates a Person
    Person memory newPerson;
    newPerson.name= name;
    newPerson.age= age;
    newPerson.height = height;
    
    people[creator] = newPerson;
    }
    
    function getPerson() public returns(Person memory){
        
        
        
    }

@Mauro or @jon_m can you see the problem on this?

Is there a reason why this error comes up when i deploy my code?

Hey @wpcrypto,

Well done for working out the correction you needed yourself :ok_hand:

Person[] public people;

Here, we are defining our people array variable with the Person Data Type. If we want to store instances created from a struct in an array, the Data Type we define it with is the name of the struct (not the keyword struct). A similar thing happens in mappings to define the Data Types of the key and the data mapped to it, although the mapping definition does also open with the keyword mapping. This may be confusing at first, because if, for example, we were storing unsigned integers (a primitive data type) we would define the Data Type as uint:

uint[] public numbers;

We use a capital ā€˜Pā€™ because this is the convention for naming structs, but you will notice that it does also work if you use a lower case ā€˜pā€™ for the struct name i.e.

struct person { ... }    // 'P' changed to 'p' here 

person[] public people   // instead of changing 'p' to 'P' here

The important thing to note here is that, your original error was one of consistency, rather than not using a capital letter. Although, you are right that the best way to fix this inconsistency is to use capital letters (according to convention).

Note that if you use your alternative createPerson function code, ā€¦

ā€¦ then you also need to make the same change, because you are pushing a new instance of the Person struct to the people array (i.e. itā€™s the Person not the person struct). I also hope you realised that you need to correct your spelling of length, and also end the statement with a semi-colon, in order for it to work :wink:

Iā€™m sure you already realise, but if you use the longer method of creating a new instance (by defining the local variable newPerson, then you also need to add the following line in order to push it to the array:

people.push(newPerson);

Sorry, if Iā€™m just explaining what youā€™ve already worked out yourself. But I think your original problem raises an interesting issue, and so I think itā€™s good to include a clear explanation here in the forum for other students who are just starting to learn Solidity, as well.

1 Like