Beruflich Dokumente
Kultur Dokumente
Your code goes between the Sub Main() and End Sub. In VB, lines
does not end with a semi-colon; compared to C# and most other
languages, VB is kept simple!
Comments
Organizing Code
When you build your program, certain files are created. It may seem
excessive, but they are actually just simple files and instructions for
your program and the Visual Studio IDE. Navigate to
C:\users\<yourname>\ Documents\Visual Studio\Syntax (or the
saved file location). Find the file called Syntax.sln and a folder called
Syntax. If you open the Syntax.sln (sln is a Visual Studio Solution
file) with notepad, you will see it has some global settings; we advise
you not edit anything!
In the Syntax folder there will be 3 folders: obj, My Project and bin.
There will also be two files: Module1.vb and Syntax.vbproj. The
Module1.vb is our source code for our program. Next, open up
Syntax.vbproj, which is a Visual Basic Project File. This is actually
an XML file which stores information about other file locations; do
NOT edit it.
Now open up the bin folder; inside is another folder called debug
which is where your program will output. If you double click
Syntax.exe the program will run. Go back and open up the My Project
folder, and focus on the file called AssemblyInfo.vb. This stores
information about the program such as author, copyright,
trademark, etc.
Variables
STRINGS
Example
Here are several ways in which you can manipulate strings. Create a
new VB Console Application and name it Strings. Next, type the
following:
Press F5 to debug and you should see the text thecodingguys. This
is a simple string. Comment out the above code (CTRL+K+C).
Escape Character
In cases where you need to escape quotation marks which get in the
way, see the following example:
If you run this (F5), you will get an error saying End of Statement
Expected. The problem is that VB thinks that the line ends at “Asim
Said:”, which is incorrect. So what we need to do is put four double
quotes around Hello World, as such:
Output
New Line
When you need a new line, in C# the new line is simply \n; however
in VB it is quite different. Comment out the above code and put this
below:
Concatenation
Concatenation means joining two things together, in this case joining
strings. It can be very useful when you want to link something
together to form a sentence. You use the & ( or + ) to form a
concatenation. For example:
String.Format
The string.format method is useful for formatting your text, and you
can format it in many ways. For example, say you wrote
Console.WriteLine (“Your balance is £5”); however, if you gave this
away to someone in the USA or Italy, they would see the British
pound symbol instead of their own country’s currency symbol. This
is where the .Net Framework becomes useful, as it can detect the
user’s country / regional settings and format the piece of text to
match those settings.
Example:
Now when you debug this (F5) you should see it print out 158 with
your country’s currency symbol in front of it. For more ways to format
strings, see the table below:
The 0 in the format key specifies the parameter you want to format,
followed by a colon and then the formatting type.
Example 2
This time we use two parameters.
Manipulating Strings
There are many built-in methods available to you, so you can format
strings in several ways. For example, you can format a string and
make it all uppercase, you can remove spaces, or you can simply
count the length of the string. In the next few examples, you will see
a few useful methods used to manipulate strings.
Example:
Replace
The replace method replaces a string with another string. It takes two
parameters, the old string and new string, and it can also take chars.
In the following example, the spaces in yourMessage are removed and
replaced with dashes.
Output:
Count
The count and length method return the length of the string.
Dates and times are also another data type in Visual Basic.
Sometimes you may need to print out the date in your application or
web application, like for example when a post was last edited. There
are many ways you can manipulate dates and times in Visual Basic,
and we will discuss the most common ones. Create a new VB console
application and name it Dates And Times. Then between Sub Main
and End Sub() put the following code:
This will output the date and time as such:
This will print out the date and time according to your PC. One thing
you should bear in mind is that since this is based on the .Net
Framework, the date and time will print out depending on the format
the computer uses and the time zone you are in. The above code
simply prints out the current date and time.
This prints out the date in a long format; it will not print out the time
as we have not told it to. We can also print “short date” by using
ToShortDateString. Try removing the ToLongDateString and
replacing it with ToShortDateString, or simply copy the following:
Extract date
Here we will get just the month and day on their own. Since the date
will be displayed as an integer, we will need to convert it to a string
and format it; for example:
Now this surprisingly outputs 2 (meaning Tuesday ), because
Tuesday is the second day of the week. However, if you want the word
“Tuesday” you need to format it. What we do is write
Day.Now.ToString(format) as follows:
This will print out the current day. You can do the same for month
by putting MMMM instead of dddd (case-sensitive). Getting the year
is also quite simple; we use this code:
This will print out the current year. Now we are going to focus on the
AddDays Method, which adds days to the current day. For example,
today is 17/07/2012. If you wanted to know what the day will be in
30 days, you would write:
Hour
Minutes
Seconds
You can also get the minutes and seconds as before. In this example,
after we have given the hours to advance by (which is 10), we also
add which time format we want. This is necessary, because without
it, it will print out the date and time.
BASIC MATH
Computers are good with numbers, so it’s obvious they can perform
mathematical calculations such as adding, subtracting, and square
root, as well as cos, sin, tan, and hyp. At times, problems need to be
solved, for example 2 + 6 * 5. You might think the answer to this is
40 ( 2 + 6 = 8 * 5 = 40 ), but in fact it is 32 (multiplication or division
are always done first). You can force 2 + 6 to be calculated first by
putting brackets around it like this:
The answer for this is 45. Since we had both division and
multiplication it starts from the left. Again, you can force 2 * 3 to be
done first by putting brackets around it, and then the answer would
be 5.
Division
With division you can format the result, for example:
It will output 1.8. We can force this to an integer value by using Ctype
function. Then it would look like this:
This time we use the Ctype function, and at the end specify the data
type (integer). The result is 2, as it is converted to the nearest whole
number.
Shorthand Math
You can use a much simpler way to do arithmetic:
In this case we assign the variable Shorthand the value 5 then add
8, but just move the operator to the left of the equal sign. This works
for addition, subtraction, and multiplication but not division, since
the result can be a decimal value.
Rounding:
This rounds to 9 (do not confuse this with Rnd, which is Random).
Equality Operators
Operator Description Example
= Is equal to x=y
if NOT x = y Then
NOT NOT equal to
End If
Relational Operators
Operator Description Example
Conditional Operators
Operator Description Example
Arithmetic Operators
Operator Description Example
/ Division Operator 10 / 5
* Multiplication Operator 10 * 8
Other Operators
Operator Description Example
These are all very common operators and it is important to learn all of them.