Sie sind auf Seite 1von 13

VISUAL BASIC SYNTAX

Visual Basic has a very simple programming syntax. The


language is not case-sensitive, and it is designed for people new to
the programming world, making it a good place to start.
Go ahead and start a new VB Console Application and give it
the name Syntax. You should see the following code:

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

 Single line comments start with an apostrophe (‘)


 There are no block comments in VB
 XML comments start with three apostrophes; these are useful
for documentation purposes

Organizing Code

Organizing code is very important, and comments and regions are


very useful. It is considered good practice to group and comment your
code so it is easy for other developers to work on it. Comments should
provide a small description of that piece of code, and regions are
useful use to group code together. To create a region you use the
following syntax:
Files

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

Variables allow us to store and retrieve data. Variables are storage


locations and the data is held in the computer’s memory (RAM). In
Visual Basic, variables start with the Dim keyword.

VB supports a number of data types; the common ones are:


Variables can start with uppercase or lowercase characters.

STRINGS

 Strings are enclosed in double quotes ( “ “ )


 Strings are a data type
 Strings are a collection of characters

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:

This will print out “thecodingguys is awesome”. myName is a variable


we created earlier.

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.

In this example, the first parameter (myNumber) would appear as


£158 and the second one would appear as 25%.

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.

In the following examples we will be working with a string called


yourMessage which will have the text “Welcome to thecodingguys”,
and another string finalResult which is set to nothing.

For the sake of brevity we have omitted the


Console.WritelIne(finalResult), from the examples; however if you
want to add it, remember to add the Console.ReadLine() as well.

ToUpper and ToLower


The ToUpper and ToLower converts a string either to uppercase or
lowercase format.

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.

There are many more methods available.

DATE AND TIME

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.

Getting the date


Here we will just focus on getting the date and then format it in
several ways. We will do the same as before, but this time we will say
Date.Now. Copy the following:

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:

This will output 17/7/2012.

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:

If today is 17 July 2012, in 30 days it will be 16 August 2012. This


also works for AddHours, AddMinutes, AddYear, AddSeconds and so
on.

Hour

This will simply get the hour, no minutes or seconds.

Minutes

Seconds

As before, we can also add on hours, like in the following example:

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:

Now the answer you should get is 40.


Another example is when there is multiplication and division; which
one is done first? You start from the left to the right, for example:

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.

Other Math Functions


You can also find square roots, power and round. To do this, at the
top above Module Module1 put Imports System.Math. Then write the
following:

This finds the square root of 8, which is 2.828.......

Finding the power of 10 and 5:

The answer is 100,000.

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

< Less than if y < x ThenEnd If

> Greater Than if x > y ThenEnd If

<= Less than but equal to if x <= y ThenEnd If

>= Greater than but equal to if y >= x ThenEnd if

Conditional Operators
Operator Description Example

Compare two variables if a AND b e


AND If x AND y ThenEnd If
quals

Compare two variables if a OR b eq


OR If x or y ThenEnd If
uals

Arithmetic Operators
Operator Description Example

+ Addition Operator 5+5

/ Division Operator 10 / 5

* Multiplication Operator 10 * 8

Other Operators
Operator Description Example

& Concatenation “a” & “b”

These are all very common operators and it is important to learn all of them.

Das könnte Ihnen auch gefallen