[Android General] Beatsleigher:
What Does What Piece of Code Mean and do?

In this post, I'll go over what which piece of code does. The practical thing about Visual Basic, is that it uses a lot of words used in the English language. That means: If you can speak English fluently, you can code in Visual Basic quite decently.

But nevertheless, I'll go over each bit, and that bit by bit. Of course, if you have questions, I'm happy to answer.

What does 'Imports' Mean and do?​

Imports

In Visual Basic, as in pretty much every other programming language, has references it uses to communicate to the OS (Operating System).

But, although referenced data is there, it is not entirely available to each form. So you must import that data to the form, where it is needed.

You can imagine 'Import' as if you were importing freight from another country - With the only difference, that you're importing data.

If you're familiar with C++, #includes <stdio.h> is the same as if you were using Imports System.IO in Visual Basic.

If, Else, ElseIf and End If​

What will be explained here, is the If-Statement. Every programming and scripting language has an If-Statement - Even if it is used with a different name.

Basically, what an If-Statement does, is check whether specific criteria is met by a clause you typed.

For example:

If File.Exists("C:\HelloWorld.vb") Then
MessageBox.Show("The file exists!")
Else
MessageBox.Show("The file doesn't exist!")
End If

This piece of code checks if a specific file exists.

If it exists, it will throw a message box saying that the file exists. Else, it will throw a message box saying it doesn't exist. Make sense?

But then we have ElseIf.

Using ElseIf can make the code more precise.

For example: Imagine you have a form with a text box, and you want to determine whether that text box contains, say http:// or ftp://, you'd type something like this:

Dim Text As String = TextBox1.Text (We'll get to Dim in a moment)
If Text.Contains("http://") Then
MessageBox.Show("The text box contains http://")
ElseIf text.Contains("ftp://") Then
MessageBox.Show("The text box contains ftp://")
Else
MessageBoz.Show("The text box doesn't contain http:// or ftp://")
End If

End If basically just terminates the If-Statement. I don't have an example for this in C++, but I guess you guys are smart enough to get what I mean :)

What does Dim mean?​

This is probably the easiest thing to explain, in this entire tutorial: All Dim does and means, is Declare. It declares a variable with a type.

I think in C++, you'd write something like

int a = 16;

Where the equivalent in VB is:

Dim a As Integer = 16

Sure, it's a bit more to write, but the code is easier to understand. Which is all VB is about: Easy coding.

For Each X As String In... Whaa?​

Well, here we've gotten to a stage, which I only learned a few months ago, and I've been programming in Visual Basic for five years, now.

Basically, For Each is kind of like an If-Statement. It searches for specific criteria. If that criteria is met, the code will be executed.

I'll use an example from the program written above:

For Each Item As String In My.Computer.FileSystem.GetFiles("C:\Windows")

This searches for files (FileSystem.GetFiles("") ) and returns these to a variable (Item) as a string value.

Next

The Next statement tells the computer to move to the next piece of code.

And last but not least:

Shell? But wait.. I know that from somewhere, don't I?​

Yup, you do! Shell is just a command prompt or terminal (Whatever you prefer). All it does, is it executes commands as the computer's shell and it gets a bit more low-level as other commands.

For example:

Shell("")

This would execute a simple program, without any command line arguments (Command Line Args).

Shell("""adb.exe"" install")

This would execute a specific file (In this case adb.exe) and would add a command line arg. Which gives you more flexibility and it allows you to interact with the shell-executable.

But the Shell Function can do more than that. It is also still a part of the program, which means it can still tell the program what to do.

For example:

Shell("""ADB\adb.exe"" install " & ListBox1.SelectedItem.ToString, AppWinStyle.NormalFocus, True, 30000)

This piece of code executes adb.exe, with a command line arg, but adds to the shell (CMD) window.

AppWinStyle: This determines how the CMD window is shown. In this example, we used NormalFocus, which puts the CMD window in the foreground and focuses on it. So the user can immediately interact with it, if necessary.

Where True is: True or False determine whether the program should wait until the shell operation is completed, before moving on to the next step of code. And ultimately, this is also what the integer (Whole number) behind it is for. The number (Must be an integer!) determines how long the program should wait until the program should execute the next line of code, in milliseconds.

And that was that, I guess.

If you feel I've missed something out, or you don't understand something, fell free to let me know and I'll it it to the list.

I'll add the project to my GitHub, so you can all download it.

Once I have the time, I'll re-design the posts, but at the moment, I think it'll do :)

(Mods: If you think I should, I'll do it right away! :))