Lets talk first a little about programming and computers in general.
Primitive Data Types - Short VersionNumerical Primitive Data TypesByte type can hold numbers between 128 to 127
Short − 32,768 to 32,767
Int − 2,147,483,648 to 2,147,483,647
Long − 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
Double, and float are also data types these hold decimal values (IE: 9.333, 3.0, etc)Other Primitive Data TypesChar - This holds a single character, an example would be the letter 'a', it can not hold multiple letters or sentences.
Boolean - This holds a value of either True, Or False.
A String Data Type?A String which is not considered a primitive data type in most programming languages, is also very important to know. The reason it's not primitive is because internally strings are represented as multiple characters, which you could decompose into individual characters. As a requirement primitive data types must be predefined types of data. A String then, holds a string of letters or characters. Or maybe more easily spoken as a sequence of characters together as a variable *generally speaking* (EX: String would be "Gaming"). Remembering that things like a space ( ) and period (.) count as characters (as well as other things like "/", "'", ",", """, etc).
Some Common Practice / Naming conventionsGenerally their are a few common practices or ways that most programmers consider to be accepted. Not really sure how to word that correctly but perhaps as I go on you will understand. It's good to follow these practices. A good example is the short name for a Button would be "btn_name", where name is the name of the particular button. The default in visual basic for buttons is "Button1", naming the button "ButtonPusher" would be considered not as well formed. This goes for other things like the text box, where it should be named "txt_UserName" following some similar rules as are used in the Naming Conventions of Variables. The reason is so that you can retrieve useful information from the names based on regularities. Although it is not required and will not effect your code, It is generally recommended. Another important convention for variables is using casing to separate words for example:
Dim timeMins As Integer
Dim TimeUntiTheSecondComingOfAwsomeSauce As Integer
Where the second word starts with a capital letter indicating the separation of words. Also it is generally done that the first word will start out lowercase "tempTime" allowing for a more easy flow of the words, however this is sometimes ignored if their are three or more words involved.
It is important to follow these convection's to have nice and tidy neat code that is easy to read (even for your self) and flows well. Remember however that not all venerable are as easily understood from their name, for example an increment variable for a counter in perhaps a for loop may be called int i; where 'i' stands for increment. Count is sometimes used in place of this but when you are doing lots of coding these shortcuts, or tricks can be very time saving and easily understood once you get to using them. Since they are used all around it would be good to be familiarwith them in case you have to work with code that is not yours.
Commenting CodeCommenting code is also very important. It is recommended you do so where it should be applied. Comment's in code are lines of text that the compiler will ignore. Meaning that is will not effect your program. These lines are used to describe fragments of code, watermark your application, debug code, or remind you of where you stopped or what you are heading towards. Commented code is just simply Human readable text. Depending on the programing language the method to denote comments can be different. Since we are using Visual basic for not we will just deal with "'" or (') the apostrophe. An example of the comment code is shown below where the green text is the commented code.
Imports System.Net
Imports System.IO
'@Inject OH 4
'Rev 1.2 Dec 4th 2012
'Program checks your IP and displays it otherwise sends an error
'Attempt IP Get
Try
ipaddress = System.Text.Encoding.ASCII.GetString(( _
webclient.DownloadData("http://www.conjointgaming.com/codecalls/whatsip.php")))
lblIP.Text = ipaddress
Catch ex As Exception
'If cannot track users IP display error
MsgBox("You are not connected to the internet or the hostname is down.", MsgBoxStyle.Critical)
'Don't forget to close
Me.Close()
End Try
As you can see commenting can be used to state the creator of the application (which is only visible via a user having the source code, do not use this to let users know), when it was made and a small description on what the application does. Those areas of comment are generally only for yourself and others using your code. Commenting is important, when you start making a huge complex program, it's going to be very helpful to know what your code does and where it is. Commenting is pretty necessary however you don't need to over comment for example:
'@Inject OH 4
'Rev 1.2 Dec 4th 2012
'Program checks your IP and displays it otherwise sends an error
'Attempt IP Get
'Start of Try
Try
'YEAH GET TAT IP
ipaddress = System.Text.Encoding.ASCII.GetString(( _
'USIN TAT WATS MEH IP SAUCE
webclient.DownloadData("http://automation.whatismyip.com/n09230945.asp")))
'POP THAT BAD BOY INTO OUR TEXT BOX! YEHHHHHH
lblIP.Text = ipaddress
'But dog... what if something goes wrong?
Catch ex As Exception
'If cannot track users IP display error
MsgBox("You are not connected to the internet or the hostname is down.", MsgBoxStyle.Critical)
'Don't forget to close
Me.Close()
'Now end that bad boy!
End Try
'WHITE SPACE LOL
Although over commenting doesn't hurt it's a tad unnecessary and waist time. For now don't worry about it though, right now for getting use to it and since your new, comment as much as you need and want! Also in relation to the white space comment, white space ie no code just a blank line, does not effect your code and has no effect when compiled the same as commented code. Commenting can be used to disable fragments of code for debugging as well this is helpful when you think something might be breaking your program and you want to see what's going on. We'll take about this in more detail later.
Declaring VariablesTo start in visual basic lets declare a variable. Dim is what we always want to start with. So Dim timeSpent or Dim reallyCool, etc. After we name our variable we need to then go on to say "As" and define the type of variable we are creating such as Int, Boolean, etc. Example:
Dim numTest As Integer
As you can see in Visual basic we do not call it "numTest As Int" we are required to write out the whole word, which is a tad annoying and not really done with most of the programming languages you'll later deal with. However the IDE (integrated development environment, or the Visual Studio application you are programming within) has some nifty features as you type it displays a drop down of likely things you are heading towards in our case we can type int... and hit enter as it will be the first selected option on our drop down. This can at times get annoying when we hit enter and it changes our code into some random thing from the drop down but we'll get into that later as well.
Now we have created a numTest variable with type Int. We should initialize it! So lets go ahead and do that by typing on the next line:
numTest = 6
followed by the next line of code to display our new value
MsgBox(myNum)
Putting this into piratical use we could create a form add a button and make this it's code so we would have:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim numTest As Integer
numTest = 6
MsgBox(numTest)
End Sub
End Class
Lets assume however we are going to make numTest equal to 6 to start with and don't need to wait we can initialize it from the moment we declare it! Simple as, "Dim numTest As Integer = 33" if however we changed our first line to this but didn't remove "numTest = 6" the message box would still display 6, the number that numTest is actually equal too. Their isn't much different between how the other data type work minus the following examples:
Dim numTest As Boolean = True
Dim numTest2 As String = "This is a string"
Dim numTest3 As Char = "c"
Anyways that is it for this tutorial. In Tutorial number 4 we will be making another application and back to reading again in tutorial number 5.