Professional Documents
Culture Documents
Mirë se vini në tutorialin e Visual Basic. Visual Basic (ose VB) është një gjuhë
programimi e zhvilluar nga Microsoft që shkon me .Net Framework. Me Visual Basic
ju mund të ndërtoni aplikacione për Windows, aplikacione të internetit dhe
aplikacione telefonike të Windowsit. Programet e zhvilluara në Visual Basic do të
funksionojnë vetëm në një Sistem Operativ të Windowsit. Visual Basic është i lehtë
për të mësuar, argëtues dhe i fuqishëm!
Sponsored Links
Kjo me siper është faqja e startimit të IDE-s: Në të djathtë është një paleta e
mjeteve dhe në të majtë janë projektet tuaja të fundit dhe projekti "Projekti i ri" si
dhe "Projekti i hapur". Në krye është një shirit standard i menusë.
Dritarja New Project
Kjo është dritarja e Projektit të Ri: Në të majtë ju keni instaluar gjuhët dhe në mes
keni modelin e tipit te programit qe do te zgjidhni per te ndertuar(Windows, web,
etj.). Shënim: Ju do të shihni vetëm Visual Basic nëse jeni duke përdorur edicionin
e shprehur. Ne do të bëjmë Aplikime ne Console në .Net Framework 4.0.
Hapesira e punes - WorkSpace
Më siper është hapësira e punës tuaj: Ky është një aplikacion me konsole. Ju keni
hapësirën tuaj të punës (ku shkruani kodin) dhe dritaret e gabimeve tuaja. Ju do të
shihni në hapësirën e punës se keni numra të rreshtit te shkruar me kode. Për të
shfaqur numrat e rreshtit, shkoni te Tools> Options> (expand) Text Editor > VB> në
anën e djathtë> Display, Line Numbers.
Permbledhje
Visual Studio IDE ka shumë funksione dhe ne ju rekomandojmë që të luani rreth tij
për të njohur veten. Ne do të përdorim vetëm disa nga këto karakteristika; në fakt
ne do të krijojmë vetëm Aplikacionet e Konsolisë kështu që do të ketë përdorim të
kufizuar të veçorive të përparuara.
Visual Basic - Sintaksa
Visual Basic ka një sintaksë shumë të thjeshtë të programimit. Gjuha nuk është e
ndjeshme ndaj çështjeve dhe është projektuar për njerëz të rinj në botën e
programimit, duke e bërë atë një mundesi të mirë për të filluar.
Shkoni përpara dhe filloni një aplikim të ri ne VB Console dhe jepini emrin Sintaksa.
Ju duhet të shihni kodin e mëposhtëm:
Module Module1
Sub Main()
End Sub
End Module
Kodi juaj duhet stuar në mes te instruksioneve Sub Main () dhe End Sub. Në VB,
linjat nuk përfundojnë me një pikepresje ; krahasuar me C ++ dhe shumicën e
gjuhëve të tjera, VB është i thjeshtë!
Komentet ne VB
• Komentet XML fillojnë me tre apostrofë; këto janë të dobishme për qëllime
dokumentimi
Organizimi i Kodeve
Organizimi i kodit është shumë i rëndësishëm, si dhe komentet dhe grupimi i
kodeve janë shumë të dobishme. Konsiderohet praktikë e mirë grupimi dhe
komentimi i kodit kështu që zhvilluesit e tjerë mund të punojnë në të. Komentet
duhet të japin një përshkrim të vogël të asaj pjese të kodit dhe grupimet janë
përdorim i dobishëm i kodeve së bashku. Për të krijuar një grupim ose rajon
“region” ju përdorni sintaksën e mëposhtme:
Console.ReadLine()
Shtyp F5, ose butonin Start dhe ka per tu shfaqur:
Shenja e pikes (angl. dot) quhet edhe Member Accessor (aksesues I anetarit)
Permbledhje
Visual Basic has a very simple programming syntax. Most of the code is easily understandable,
even if you are not familiar with programming. The language is not case-sensitive, inline
comments start with an apostrophe, and XML comments start with 3 apostrophes
Syntax
Dim <variable name> As <data type>
VB supports a number of data types; the common ones are:
Sponsored Links
Create a new VB Console Application and name it Variables. Between the Sub Main() and End
Sub, type the following:
Sub Main()
Console.WriteLine(myNumber)
Console.ReadLine()
End Sub
Hit F5 or click the green play icon on the toolbar. You should see the result 5.
Code Explained
First we use the Dim keyword to create the variable myNumber
Next we assign the value 5 to myNumber
We then use the Console.WriteLine command to print out myNumber
Finally, we use Console.ReadLine to read that line
String Variables are wrapped in quotation marks; comment out the above code, highlight it all
and press CTRL+K+C (to un-comment, CTRL+K+U). Next, type the following:
Dim name As String
name = "Asim"
Console.WriteLine(name)
Console.ReadLine()
This should print out Asim. This code works the same as above, but this time it was a string
data type. You can also add, subtract and perform other mathematical calculations. Under the
above code, write the following:
Dim x As Integer
Dim y As Integer
x = 32
y = 15
Summary
Variables start with the Dim keyword
Syntax
Dim stringName As String
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:
Sponsored Links
Sub Main()
Console.WriteLine(myName)
Console.ReadLine()
End Sub
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:
Console.WriteLine(text)
Console.ReadLine()
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:
Dim text As String
Console.WriteLine(text)
Console.ReadLine()
Output
Asim Said: "Hello World"
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
Dim myNumber As Double = 158
Console.ReadLine()
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:
Format Description
Example 2
This time we use two parameters.
Console.WriteLine(strFormat)
Console.ReadLine()
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 a number of
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.
Example
finalResult = yourMessage.ToUpper()
finalResult = yourMessage.ToLower()
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
Welcome-to-thecodingguys
Substring
The substring method can be used to return parts of a string. For example, from the
yourMessage string we can return the first 5 characters only. The substing method takes two
parameters: the start index and length index as integer.
Example
finalResult = yourMessage.Substring(0, 7)
The 0 specifies the start point, in this case it is at the beginning; the 7 specifies how many
characters we want from the starting point. To get all the characters you would do as follows:
Output
Welcome
Count
The count and length method return the length of the string.
finalResult = yourMessage.Count()
There are many more methods available.
StartsWith See if a string starts with a certain piece of text Boolean - true of false
EndsWith See if a string ends with a certain piece of text Boolean - true of false.
Summary
In this tutorial we showed you how to format and manipulate strings in Visual Basic.
17/07/2012 19:39:06
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.
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:
Console.WriteLine(Date.Now.DayOfWeek)
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:
Console.WriteLine(Date.Now.ToString("dddd"))
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:
Console.WriteLine(Date.Now.Year)
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:
Console.WriteLine(Date.Now.AddDays(30))
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.
Console.WriteLine(Date.Now.ToLongTimeString)
This will print out the current time, and it will print out the hours, minutes and seconds. You can
also use the ToShortTimeString method; this will only get the hour and minute. You also
may want to get the time in a 12 hour clock format. To do this:
Console.WriteLine(Date.Now.ToString("hh:mm:ss tt"))
This gets the time in 12 hour format, and also displays AM or PM.
Extract time
Like the date we can also extract the time and get the hours and minutes on their own. The
process is basically the same as above.
Hour
Console.WriteLine(Date.Now.Hour)
This will simply get the hour, no minutes or seconds.
Minutes
Console.WriteLine(Date.Now.Minute)
Seconds
Console.WriteLine(Date.Now.Second)
As before, we can also add on hours, like in the following example:
Console.WriteLine(Date.Now.AddHours(10).ToLongTimeString)
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.
Summary
Dates and time are easy to use in Visual Basic, and this tutorial gave you a starting point. There
are many ways you can format and manipulate the date. Date and time are a data type,
so remember if you are going to do some calculations based on the date and time it is best to
use DateTime.UtcNow.
Console.WriteLine((2 + 6) * 5)
Console.ReadLine()
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:
Console.WriteLine(30 / 2 * 3)
Console.ReadLine()
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:
Divide = 5
Console.WriteLine(9 / Divide)
Console.ReadLine()
It will output 1.8. We can force this to an integer value by using CType function. Then it would
look like this:
Dim Divide As Integer
Divide = 5
Shorthand Math
You can use a much simpler way to do arithmetic:
Shorthand = 5
Shorthand += 8
Console.WriteLine(Shorthand)
Console.ReadLine()
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.
Console.WriteLine(Pow(10, 5))
Console.ReadLine()
The answer is 100,000.
Rounding:
Console.WriteLine(Round(8.7))
Console.ReadLine()
This rounds to 9 (do not confuse this with Rnd, which is Random).
There are many more math functions that can be done with the .Net Framework. For more
information see the MSDN reference.
Summary
Multiplication and division are always done first
If there are both multiplication and division, start from left to right
For shorthand math put the operator on the left of the = sign
To use math functions such as sqrt, pow, round, make sure you import system.math
= 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.