[Tutorial] Splits & Arrays & For Each Loops MSVS 2012 07-13-2013, 12:28 AM
#1
Sagacity
Splits & Arrays Tutorial
Splits & Arrays Tutorial
Summary
This tutorial aims to teach you about arrays & splits, their usefulness, and how to use them. I personally never really understood arrays, or rather I never really took the time to look into them and ended up creating complex algorithms to parse information from a string, wasting countless time on something that could have been completed in a matter of seconds with much less effort while providing a more efficient solution.
I hope to provide you all with a greater understanding of splits and arrays. Please keep in mind that this is my first tutorial, so by no means is it a very polished thing, nor is it the best formatted, but here we go, and enjoy the show
![Smile Smile](https://sinister.ly/images/smilies/set/smile.png)
Array
An array is a systematic arrangement of objects, usually in rows and columns.
Uses
- Better Organizing Data
- Passing Variable Data In Line
- Easier To Process Web Strings
- More Efficient Than Complex Algorithms
Required Materials
- Microsoft Visual Studio ( I am using 2012 Pro )
- A basic understanding of .Net software development
- Common Sense
Alright, now that I passed along that (mostly) useless information, let's get started!
Step 1: A New Project
Firstly, you will need to make a new Visual Basic Windows Forms Application. You can name it anything you like, it does not affect the project.
Spoiler:
Step 2: Creating Our GUI
For this tutorial, we are going to use:
Textbox [1]
Button [2]
Drag out your form to make a rectangle to make it easier to deal with a larger text box. Feel free to name the form whatever you like, you will see mine saying "Split & Array Tutorial", along with the start up position being centered for ease of use.
Add the form controls with the text box being on top and the two buttons being below the text box. Change the first button's text to "Split By: |" and the second button's text to "Split By: ,".
Spoiler:
As you might have guessed, those are the two characters that I am going to show you how to split by in this tutorial, although you can split with a wide variety of variables with just changing the code to whichever character you would like to split by.
Next, you will have to rename the controls to:
Text Box: txtInput
Button 1: btnSplit
Button 2: btnSplit2
This is for making the code work without problems from this tutorial to you.
Now that we have the form designed, we are able to start coding this!
Step 3: Coding
Now we get to the main part of the tutorial, coding the actual program!
Go ahead and double click the first button (btnSplit). You should be brought to the code screen which should look exactly like (or very similar to) this:
Spoiler:
First off we need to declare our variable. Type:
Code:
Dim OurArray As Array
This piece of code declares our array for use to store data in. Without it we can't use the split data without generating the split every time we want to use it. It is much easier and more efficient to just declare a variable to store the data if you are going to be using it for any purpose other than just a one time process.
Our code should now look like:
Spoiler:
Since for this button we are splitting the text by using the "|" as a split sign, we need to use this bit of code to split the text from the text box into our array:
Code:
OurArray = txtInput.Text.Split("|")
Spoiler:
This piece of code does the actual splitting of the string and storing the data in our array that we declared a few seconds ago. That would be the code you would have to edit if you wished to split using a different character by replacing the | in "Split("|")" to whichever character you wished to use.
For this tutorial, all we are going to do is display a message box with each string in the array. In order to do that, we need a for each loop.
Code:
For Each ArrayItem In OurArray
Next
With that code, our program will automatically go in order down the array and do each action that we program in, with the current split string in OurArray temporarily set as the value of ArrayItem. Although ArrayItem is a variable, it does not need a specific declaration outside of the For Each statement.
Now, inside of the For Each loop, we just need to put this little bit of code that pops up a message box with the current string in the array is.
Code:
MessageBox.Show("The current string in the array is: " + ArrayItem, "Our Array's Contents", MessageBoxButtons.OK, MessageBoxIcon.Information)
Our program will now pop up a message box for each item in the array until there are no more strings in the array. But right now, the only way to know that the array is over is that message boxes stop popping up. How about we add a message box AFTER the For Each loop that will pop up once the array has been gone through letting us know how many strings there were in the array and that the array is done.
In order to count how many items there were in the array, we need to declare an integer right under the array declaration and set its value to 0.
Code:
Dim ArrayCount As Integer = 0
We also need to add the actual counting function inside the For Each loop, which would be:
Code:
ArrayCount += 1
That needs to be right inside of the For Each loop right on top. Now, we need to add the message box displaying the count and the fact that we are done processing our array.
So, right below the For Each loop's "Next", put a message box that looks like this:
Code:
MessageBox.Show("Array Processed! There were: " & ArrayCount & " strings in the array.", "Our Array's Contents", MessageBoxButtons.OK, MessageBoxIcon.Information)
Our total code should now look like this:
Spoiler:
Let's test out our code that we just wrote, shall we? Start up your program and put this string into the text box and click the "Split By: |" button.
Code:
Hello!|This Is Xanii|Welcome to my tutorial!|If you see all of this pop up one message at a time, congratulations!|You successfully wrote a program that splits a string and processes an array!
It should show up a message box at a time until it is done with the message.
Now, coding the second button that parses by commas! This part is extremely easy. Just copy the code for the button press event that we already have, and double click the second button and paste the code in. Change the "|" to "," in the split function and you are good to go!
The final code should look like this:
Spoiler:
Thanks for reading and PLEASE leave constructive criticism!