COLLECTION
The process of adding variables have already been described in another section. Adding collections follow similar steps with just a few changes.
Click on the Create button at the top corner of the screen to open the Data types window. At the bottom-left part of this window check the Collection box. Now, choose the data type you want to add and click Select.
In this example, we will be creating a collection of cars. That is why we need to add a collection of data type String. So, you will see the following block:
It is possible to rename the collection by clicking in the text box on the block you just created and inputting the name you want.
Now, use the following steps to create your collection:
1. Create the block for the variable that needs to be added to the collection. To do this, click on the Create button and select String. The variable is denoted as any random alphabet. In this case it is “i”. The next variable block that is added will be denoted as the next alphabet - “j”, and so on.
2. Set a value to the variable you created by right-clicking on the block and selecting “Set “i” variable from the drop-down menu to create its setter block.
3. Connect a value to the setter block. In our example, the values for the variables are the names of cars.
4. Now, navigate to the General tab on the left-side of the screen and select the “add to collection” block.
5. You can notice that the collection block has two slots. The first slot is for the variable. But first you need to get the value for the variable. To do this, right-click on the variable’s block and select Get “i” variable. This will create the getter block for the variable. Now, connect it to the first slot.
6. The second slot is for the car collection variable. First, you need to get the value for the collection variable before it can be connected to this slot. To do this, right-click on the cars collection block and select Get “cars” variable to create its getter block. Now, connect it to the second slot.
At this point, this is what the code should look like:
We have added Mazda to the collection of cars. If you want to add other names to this collection, you should repeat steps 1 to 6. For example:
We just added Toyota to the collection of cars. You can keep adding as many items to the list as required.
To add the function, click on the Objects button and navigate to the Functions tab. There, you will find already created functions. In our case, we will be adding the “newProduct” function that creates creates new products.
Connect the function and the car collection variable to the first and second slot of the add to collection block respectively.
Now, you can connect the name and the price of the car to the function.
Add multiple cars with their prices by repeating these steps above.
Removing items from a collection
You can remove a car from the collection by using the loop and if statements.
To do this, go to the loops tab on the left side of the screen and get the “for each” block. Now, connect the car collection variable to it.
Next, get the “simple if” and “compare” block from the logic tab. Then, connect the return true block to the simple if block. Once this is done, insert them into the for each block.
Notice that the return true block has two slots. In the first slot, insert the block for the name variable for each item in the collection. To get this block, right-click on the for each block and select Show “o” variable. In the Objects window, select Name and click get. Now, insert the block created into the first slot of the compare block. In the second slot, insert the name of the car you want to remove from the collection.
Now, go to the General tab and select the “remove from collection” block. Right-click on the for each block and select Get “o” variable to get its getter block. Insert it into the first slot of the remove from collection block. In the second slot, insert the block for the cars collection variable.
The final code should look like this:
So, this code will remove Mercedes from the list. Therefore, just Toyota, Mazda, and Volvo will be displayed.
Clearing all items from a collection
To clear all items from the collection, go to the General tab and get the clear collection block. Connect the car collection variable block into the slot of the clear collection block. Then, connect it to the rest of the code.
Any code that is inside the for each block will not be executed because everything from the collection has already been removed.