1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Smart Contract Development with Solidity

Constructor & Modifiers

Start course
Overview
Difficulty
Intermediate
Duration
3h
Students
49
Ratings
5/5
starstarstarstarstar
Description

This course provides a deep dive into the Solidity programming language which is used to implement smart contracts on blockchain platforms. This course will give you a practical understanding of Solidity and how to develop your own smart contracts.

Transcript

In this lecture, I want to talk about arrays because we need them at some point in the game that we are developing. But they come with a little bit of a cautionary note from my end because when you use arrays then you usually use them because you can iterate full arrays. But every time you use a for loop in solidity, then keep in mind that inside this for loop, you are using up gas and we are going to discuss this later when we talk about gas more in depth. But keep in mind that when you have an array, which scales with your user base that might get bigger and bigger than what users you have then at some point you cannot iterate through this array anymore because of the block gas limit. So, you only have a certain number of computational iterations or you have a certain number of computational operations you can do on the blockchain. But nevertheless, we are going to use arrays for our game in the form of we are holding a game map in array so that array will have a fixed size and we can write to certain parts of the other array. Now, let me give you an example of fixed size and dynamic size array because both of them are possible in solidity. The one would be and let's just make an unsigned integer array with five elements. And I'm going to make this public as well. So, you see a limitation of these automatic get functions and I just call this array fixed size. All right, let's deploy this. Let's run this, deploy. And you see I can get a specific element from that array from the automatic get function that is created in solidity, but I can't get the whole array. So, in our case, if I want to get the whole array then I would have to iterate five times through this array. So, I would have to get zero and the first or the second element with index one, third element with index two, fourth element with index three and fifth element with index four. So, the start is a 0-based index. In order to get the whole array, I would have to write my own get function. And as I told you before, I'm a huge friend of writing your own get functions because you're in control of how they behave  and I would just write get fixed array and I won't give them any index. I want to have the whole array and that is a view function because I'm accessing as storage variable. It's a public function. I want to access it from anywhere and it returns my uint [5], and now you will see that our solidity compiler will complain more than a second, and I just return the array fixed size. Now, let's read this error message here. Data location must be memory for return parameter  and function but none was given. Now, since solidity is 0.5.0. for reference types, which an array is. You have to tell solidity where you actually, where you want to store this when you're working with it. And there are free storage locations. The one is call data which is the one over here from the transaction, the input field, you can't modify this once the transaction is sent. The second one is memory which is like your RAM, and the third one is storage, which is like your hard drive where the storage cost is expensive. So, whenever you're working as a rule of thumb, whenever you're working with any values inside the function use the memory storage location. It's like your work house, it's much cheaper to work with it and it's not persistent, it's really just working for temporary with your variables until you either store them or you return them. In our case, I just want to return my uint [5] to my front end here. When I remove this one and redeploy the smart contract, then you see this one will return all my five values at once. Now, obviously they're all zero because they are initialized as the default value, but that's another story. Let's add a dynamic array and you can do this with just these brackets without any number inside. So, that would be a dynamic array. Make this public again array dynamic size and deploy this smart contract. And you see I can also with this public function, with this public keyword, the automatic get function gives me the ability to access an index. I'm going to try to access an index which is not yet done then it will tell me I can't access the virtual machine android, it can't do anything with this index, so I first have to push something onto this array. The next thing that we have is multidimensional arrays. Let's say we have an unsigned integer 2 with like a fixed size and the dynamic size array. Whereas be careful that this one is reversed, in the reversed notation, I will tell you in a second what I mean. Let's just return this and see what happens. So, I will just call this function. getDynamicAll () view public returns (uint[2]  [] memory){return arrayFixedDynamic; Let's see what happens. There's nothing inside yet. Let's put something in our dynamic array here and dynamic arrays you can increase and decrease the length and then write something to the appropriate position. Let's write a function that increases the length of this array and then write something onto the first and second element. Length, let's code it like this. And now we are going to... This is the length member of the array which gives you back the length of the current array and funny enough this one will be zero. And the reason for it is that the dynamic array is actually the first one over here and then the fixed size array with two elements is the second one. So, we have to increase the first array which is the dynamic array and then the second one is with two elements: zero and one. Let me give you an example of this fixedDynamic length right into the last element of this array. Let's write it like this and then... The second element, and now let's see what happens. So, first we get dynamic all it's empty and then we increase it and now we have one dynamic element and two elements in the first index and then we increase it again. So, we have now 123, 234, 123, 234. So, we have two arrays, two indexes in the first array with the dynamic one and then two again in the second one. Let me increase this again. So, it's more clear. So, this would be our first one, second one, third one and inside the first one we have 12, 12, 12, 12 and 12 and 12 keys. Now, that is actually already very complicated and here is a little coding challenge for you. We have this dynamic size array and why don't we have a very similar function, increased dynamic length where we are going to write something into this array or push something inside. If you want to do this coding challenge, then pause the video now. If you don't want to do this coding challenge, then just keep watching. All right, let's write this function, increase dynamic length or any function name you want. And we are going to write here in the last or in the newly created element, the #555. Let's see if this is working. This was the wrong one. We have together get the function as well. Actually, we don't we can just take the first element. Now it's 555 and the second element, there's nothing inside yet. Let's put something inside. Now it's working. So, you saw that first I clicked once, increased dynamic length that increased that put one element inside there with the index zero wrote 555 and then I tried to access index one. There was nothing inside yet. So, it gave us this error. And then I clicked, increased dynamic length again. So, we wrote something into the index one which is the second element of the array again 555. It's not very useful, but it's good for demonstration purposes. And then I tried to access the index one and then it worked. Now, the last thing I want to show you, it should be quite clear by now, but I still want to show it to you how you access the length and how you can return the length and let's return the length of the fixed dynamic size array. So, it's also very clear how this works. Let's just a function getFixedDynamicLength. There is a view in public function and I want to have the length of the first array and then the length of the array inside the first index here. And let me show you what I mean. So, we have two uint. And this one will return arrayFixedDynamic.length and the first element of the dynamic array length and let's deploy this. We close these ones and just work with the last one. So, get fixed dynamic length gives us an error because our first element here isn't set yet. And when I execute this increased fixed dynamic length function then the length of the first dynamic array of the dynamic array will be increased to one element and then I can execute this and I see that our outside array, where our first array or outside array is the dynamic array with the length one and our inside array, inside the first element of the dynamic array is the fixed size arrays. So, be careful with the ordering of how you specify the dynamic and the fixed part here if you work with multidimensional arrays. All right, that's it for this lecture. And in our course, we are going to work a little bit with arrays but actually better is to work with something called mappings, they work a little bit like hash maps, and that's something that we're going to discuss in the next lecture.

About the Author
Students
58
Courses
7

Tom is a CTO, senior back-end developer, and systems architect with over twenty years of hands-on development experience in a variety of languages and systems. He has a CS master's degree and has been working with Ethereum and blockchain technologies since 2016.

Covered Topics