Vb.net
Vb.net
VB.NET stands for Visual Basic.NET, and it is a computer programming language developed by Microsoft. It
was first released in 2002 to replace Visual Basic 6. VB.NET is an object-oriented programming language. This
means that it supports the features of object-oriented programming which include encapsulation, polymorphism,
abstraction, and inheritance.
Visual Basic .ASP NET runs on the .NET framework, which means that it has full access to the .NET libraries.
It is a very productive tool for rapid creation of a wide range of Web, Windows, Office, and Mobile applications
that have been built on the .NET framework.
The language was designed in such a way that it is easy to understand to both novice and advanced programmers.
Since VB.NET relies on the .NET framework, programs written in the language run with much reliability and
scalability. With VB.NET, you can create applications that are fully object-oriented, similar to the ones created
in other languages like C++, Java, or C#. Programs written in VB.NET can also interoperate well with programs
written in Visual C++, Visual C#, and Visual J#. VB.NET treats everything as an object.
It is true that VB.NET is an evolved version of Visual Basic 6, but it’s not compatible with it. If you write your
code in Visual Basic 6, you cannot compile it under VB.NET.
History of VB.NET
VB.NET Features
VB.NET comes loaded with numerous features that have made it a popular programming language amongst
programmers worldwide. These features include the following:
VB.NET is not case sensitive like other languages such as C++ and Java.
It is an object-oriented programming language. It treats everything as an object.
Automatic code formatting, XML designer, improved object browser etc.
Garbage collection is automated.
Support for Boolean conditions for decision making.
Simple multithreading, allowing your apps to deal with multiple tasks simultaneously.
Simple generics.
A standard library.
Events management.
References. You should reference an external object that is to be used in a VB.NET application.
Attributes, which are tags for providing additional information regarding elements that have been
defined within a program.
Windows Forms- you can inherit your form from an already existing form.
Advantages of VB.NET
The following are the pros/benefits you will enjoy for coding in VB.NET:
Disadvantages of VB.NET
VB.NET cannot handle pointers directly. This is a significant disadvantage since pointers are much
necessary for programming. Any additional coding will lead to many CPU cycles, requiring more
processing time. Your application will become slow.
VB.NET is easy to learn. This has led to a large talent pool. Hence, it may be challenging to secure a
job as a VB.NET programmer.
Summary:
VB.NET is also known as Visual Basic.NET. It stands for Visual Basic .Network Enabled Technologies. It
is a simple, high-level, object-oriented programming language developed by Microsoft in 2002. It is a successor
of Visual Basic 6.0, which is implemented on the Microsoft .NET Framework. With this language, you can
develop a fully object-oriented application that is similar to an application created through another language
such as C++, Java, or C#.
Feature of VB.NET
Visual Basic
Visual Basic (VB) is a programming language developed by Microsoft in 1992. The purpose of this language
is to develop an application that can run on different versions of the Windows operating system. A Visual Basic
evolved from Basic Language; Basic language is easier to read than other languages. The final version of Visual
Basic was released in 1998. Microsoft then launched a Visual Basic DotNet ('VB.NET') language, which is
much better than Visual Basic in all aspects such as performance, reliability, working environment, easy to
build, and debugging an application.
In VB.NET, data is handled using the ADO.net Data Connectivity and handling are done through DAO,
protocol. RDO, and ADO (ActiveX Data Object) protocol,
Object does not support default property. The Object support default property of virtual basic.
Sadly, Microsoft have decided to do away with the nice start up page from previous versions. Instead, you'll see
something like this:
Any projects you create will appear on the left, under Open recent, and you can simply click on your file to
open it. Click on Create a new project, the fourth box on the right in the image above.
On the Create a new project dialogue box. You'll see this screen:
At the top, click the dropdown list for Language. Then select Visual Basic:
From the list that appears, select Windows Form App (.NET Framework). Then click Next:
When you click Next, you'll see this screen, Configure your new project:
In the Project name box at the top, type My First Project. (It will probably say WindowsApplication1.)
You can choose a location to save your projects. We've created a folder called Projects, in the Documents\Visual
Studio 2019 folder.
The Solution name box will get filled in for you. So just click the Create button.
Eventually, a new project will be created. You should see a screen like the one below:
So, if you already have your VB NET software open, you can get rid of the current project by clicking File from
the menu bar. From the dropdown menu, choose Close Solution. You will be returned to the Start Page. Click
File > New Project from the menu bars. 2015 and 2017 users will get this dialog box:
When you get the dialogue box popping up, choose Windows Forms App or Windows Forms Application at
the top. Then change the Name from WindowsApp to Variables:
Type Variables as the Project Name. You can change your Location to save your project elsewhere.
If you look at the Solution Explorer at the top, you'll see the name of the project has changed to the Name you
gave it.
The name of the Project is now Variables - the same name as the folder that is created for you to hold all your
project files.
Now that you have a new Project started, we can discuss Variables. We'll do that in the next section.
How to Create Variables in VB .NET
With Visual Basic, and most programming languages, what you are doing is storing things in the computer's
memory, and manipulating this store. If you want to add two numbers together, you put the numbers into storage
areas and "tell" Visual Basic to add them up. But you can't do this without variables.
So a variable is a storage area of the computer's memory. Think of it like this: a variable is an empty cardboard
box. Now, imagine you have a very large room, and in this room you have a whole lot of empty cardboard
boxes. Each empty cardboard box is a single variable. To add two numbers together, write the first number on
a piece of paper and put the piece of paper into an empty box. Write the second number on a piece of paper and
put this second piece of paper in a different cardboard box.
Now, out of all your thousands of empty cardboard boxes two of them contain pieces of paper with numbers on
them. To help you remember which of the thousands of boxes hold your numbers, put a sticky label on each of
the two boxes. Write "number1" on the first sticky label, and "number2" on the second label.
What have we just done? Well, we've created a large memory area (the room and the cardboard boxes), and
we've set up two of the boxes to hold our numbers (two variables). We've also given each of these variables a
name (the sticky labels) so that we can remember where they are.
number1 = 3
number2 = 5
That's code from Visual Basic Net. It's VB's way of setting up (or declaring) variables.
Dim
Short for Dimension. It's a type of variable. You declare (or "tell" Visual Basic) that you are setting up
a variable with this word. We'll meet other types of variables later, but for now just remember to start
your variable declarations with Dim.
number1
This is the cardboard box and the sticky label all in one. This is a variable. In other words, our storage
area. After the Dim word, Visual Basic is looking for the name of your variable. You can call your
variable almost anything you like, but there are a few reserved words that VB won't allow. It's good
practice to give your variables a name appropriate to what is going in the variable.
As Integer
We're telling Visual Basic that the variable is going to be a number (integer). Well meet alternatives to
Integer later.
Number1 = 3
The equals sign is not actually an equals sign. The = sign means assign a value of. In other words, here
is where you put something in your variable. We're telling Visual Basic to assign a value of 3 to the
variable called number1. Think back to the piece of paper going into the cardboard box. Well, this is the
programming equivalent of writing a value on a piece of paper
Now that you have a basic idea of what variables are, let's write a little piece of code to test them out. First,
though, let's have our first look at the coding window.
To make life easier, we're going to put a button on our form. When our button is clicked, a little message box
will pop up. Fortunately, there's no coding to write for a button, and very little at all for a message box.
Instead of double clicking the Button tool in the toolbox to add the control to the form, we'll explore another
way to do it.
With your Form displayed in the Visual Basic Design environment, do the following:
1. Click on the Button tool in the toolbox with the left hand mouse button, but click only once
2. Move your mouse to a blank area of your form - the mouse pointer will turn into a cross
3. Press and hold down the left mouse button
4. Drag across the form with the button held down
5. Let go of the mouse button when you're happy with the size
6. A Button is drawn
You can use the above method to draw most of the controls onto the form - labels, Buttons, textboxes, etc.
The Button control, just like all the other controls we've seen so far, has a list of properties. One of these
properties is the Text property. At the moment, your button will say "Button 1". You can change that to anything
you like.
Now add a Textbox to your form using one of the methods outlined (either double-click, or draw).
VB Button Code
To get our first look at the code window, double click your Button control. The code window will appear, and
will look something like this:
The part to concentrate on for the moment is where your cursor is flashing on and off. Because you double-
clicked the Button control, the cursor will be flashing between the lines Private Sub … and End Sub.
End Sub
The part of the code we're interested in is highlighted in red in the code above. Notice, too, that the underscore
character ( _ ) has been used to spread the code over more than one line. You can do this in your own code, too,
if it becomes to long:
Private
Private means that no other part of the programme can see this code except for our button
Sub
Short for Subroutine. The "Sub" word tells VB that some code follows, and that it needs to be executed
Button1
This is the name of our button. You might think that we've just erased the word "Button1" when we
changed the Text property, so why does VB insist that it's still called Button1? We'll, the Name property
of the control is the important one. If you change the Name property, VB will change this button name
for you
_Click ( )
This is something called an Event. In other words, when the button is clicked, the Click Event will fire,
and the code we're going to write will be executed
End Sub
The subroutine ends right here. This signifies the end of our code
Things like buttons, textboxes, and labels are all things that you can add to your Forms. They are know as
Controls, and are kept in the Toolbox for ease of use.
The Toolbox can be found on the left of the screen. In the picture below, you can see the toolbox icon next to
Form1 (the Community 2015 to 2019 editions have an extra tab called Server Explorer. 2019 users might not
see the Data Source tab):
To display all the tools, click on the Toolbox tab. You'll see the following appear:
There are lots of categories of tools available. The toolbox you'll be working with first is the Common Controls
toolbox. To see the tools, click on the plus or arrow symbol next to Common Controls. You'll see a long list
of tools:
As you can see, there are an awful lot of tools to choose from! For this first section, we'll only be using the
Button, the TextBox and the Label.
If you want to keep the toolbox displayed, click the Pin icon next to the X. To close the toolbox, simply move
your mouse away. Or just click the word Toolbox again.
In the Visual Basic NET design time environment, the first thing to concentrate on is that strange, big square in
the top left. That's called a form. It's actually the pretty bit of your programme, the part that others will see when
they launch your masterpiece. Granted, it doesn't look too attractive at the moment, but you'll soon discover
ways to lick it into shape.
Congratulations! You have now created your very first programme. It should look like this:
Click the Red X on the form to stop it from running. You will then be returned to the software environment.
If you look at the bottom of the screen, you'll see an Output Window:
You can get rid of this by clicking the pin or the cross on the right-hand side:
If you compare the form when you create a new project with the one above when its running, you'll see that
they look very similar. But the one above is actually a real programme, something you could package and sell
to unsuspecting village idiots.
So what's going on? Why the two different views? Well, Visual Basic has two distinct environments, a Design
environment and a Debug environment. Design Time is where you get to play about with the form, spruce it
up, add textboxes, and buttons, and labels (and code, of course ); Debug is where you can test your programme
and see how well it performs. Or doesn't perform, as is usually the case.
But don't worry about the terminology, for the time being. Just be aware that there's a two step process to VB
programming: designing and debugging.
So, let's get on and do some designing! Before we can start designing a form, though, we need some tools. And
where are tools kept? In a toolbox!
You've probably noticed the area to the right of the design environment, the area with all the textboxes in a grid,
the one that has those daunting names like "AccessibleDescription", "AccessibleName", "AccessibleRole".
That's the Properties box.
Click anywhere on the form that is not a label or a textbox, somewhere on the form's grey areas. The form
should have the little sizing handles now, indicating that the form is selected.
On the right of the design environment there should be the following Properties box:
If your Properties box says "Textbox1 Textbox" or "Label1 Label" then you haven't yet selected the Form. Click
away from the textbox or label until the Properties box reads Form1
What you are looking at is a list of the properties that a form has: Name , BackColor, Font, Image, Text, etc.
Just to the right of these properties are the values for them. These values are the default values, and can be
changed. We're going to change the value of the Text property.
First, you might want to display the list of Properties in a more accessible form. You can display the list
properties alphabetically. To do that, click the Alphabetic icon at the top of the Properties box, as in the image
below:
This will make the properties easier to find.
Before we change any in the Properties box, let's clear up what we mean by "Property".
What is a Property?
Those controls you added to the form (textboxes and labels), and the form itself, are called control objects. You
can think of controls as things, something solid that you can pick up and move about. Controls (things) have
properties. If your television were a control, it too would have properties: an On/Off button property, a colour
property, a volume property, and a ... well, what other properties would your television have? Think about it.
The properties of your television will have values. The On/Off button would have just two values - On or Off.
The volume property could have a range of values, from zero to ten, for example. If the value of the volume
property was set to ten, the loudest value, then you'd probably have some very angry neighbours!
In VB.NET, you can change a property of a control from the Properties Box. (You can also change a property
using code, which you'll do quite a lot.) If we go back to our Form object, and the properties and values it has,
we can see how to change them using the Properties Box. We'll change only one of these values for now - the
value of the Text property . So, locate the word "Text" in the Property box, as in the image below
"Text" is a Property of Form1. Don't be confused by the word "Form1" next to the word "Text". All this means
is that the current value of the Text property is set to the word "Form1". This is the default.
Click inside the area next to "Text", and delete the word "Form1" by hitting the backspace key on your
keyboard
When "Form1" has been deleted, type the words "My First Form"
Click back on the form itself (the one with the labels and textboxes), or hit the return key on your
keyboard
The words "My First Form" will appear as white text on a blue background at the top of the form
When you've correctly changed the Text property, your Form will then look like this one:
As you can see, your new text has gone at the top of the form, in white on the blue background.
So the Text Property of a form is for setting the caption you want to display in the title bar at the top.
Changing the values of some properties is fairly easy. We'll now change the Text properties of our labels, and
the Text properties of our Textboxes.
Click on Label1 so that it has the sizing handles, and is therefore selected. Examine the Property box for the
Label:
You can see that the Label control has quite a few different properties to the Form control. Think back to your
television as an control. It will have different buttons and knobs to your DVD Player control. A label has
different "buttons and knobs" to the Form's "buttons and knobs".
But the Label has a lot of properties that are the same. The Text property of a Label does what you'd expect it
to do: adds text to your label. We'll do that now:
1. With label1 selected, click inside the area next to "Text", and delete the word "Label1" by hitting the
backspace key on your keyboard
2. Type in the words "First Name"
3. Click back onto the grey form, or hit the return key on your keyboard
4. Label1 has now changed its text caption to read "First Name"
5. If you've made a typing error, go back to the first step above and try again
6. Your form should now look like this:
Now, change the Text property of the other two labels. Change them to these values:
What you should notice is that the labels resize themselves, after you press the enter key to commit the changes.
You may need to drag your labels to the left a bit. But when you're finished, your form should look like ours
below:
The form might look a little squashed, though. Is there anything we can do to make it bigger? Well, it just so
happens there is.
The Form can be resized just like the Label and the textboxes. Click anywhere on the form that is not a textbox
or a label. If you look closely around the Form's edges, you'll notice our old friends the sizing handles. To make
the form bigger, just stretch them like you did the labels and the textboxes. Play around with the size of the
form until you're happy with it. You can now reposition and resize the textboxes and labels so that things don't
look too squashed. Your form might look like this one:
Click on Debug > Start to have a look at your programme. Or Press F5 on your keyboard. Click Debug > Stop
Debugging to get back to the design environment. (Or press Shift + F5, or just click the red X at the top right
of the form.)
At the moment, our form looks a little bland. Time to liven it up with a splash of colour.
Changing the colour of the Form means we have to change one of its properties - the BackColor property.
So click anywhere on the form that is not a textbox or a label. If you do it right, you should see the sizing handles
around the edges of the grey form. The Property Box on the right will read "Form1", and that indicates that you
have indeed selected the form. When the Form is selected you can change its properties.
To change the colour of the Form, click the word "BackColor" in the Property Box. Next, click the black down-
pointing arrow to the right. A drop-down box will appear.
The default colour is the one selected - Control. This is on the System Tab. The System colours are to set
whatever colour scheme the user has opted for when setting up their computers. For example, you can use the
Display Properties dialogue box in Windows XP to change how things like menus and buttons look. Someone
who is colour-blind might have changed his or her settings in order to see things better on the computer screen.
If you stick with the System colours then a colour-blind user of your programme would not have any problems
seeing your master work.
As you can see in the image above, you can choose the colour of the Active Caption. The Active Caption is the
one you set earlier when you changed the text to "My First Form". The Active Caption is blue on my computer,
and the Active Caption Text is white. It might be different on yours.
If you want to choose a colour that is not a System colour, click the Custom Tab. You'll then see this:
Click on any of the Colours in the colour palette and the background colour of your form will change. We've
gone for a pinkish colour.
You can also select the Web Tab. When you do, you'll see a list of Web-Safe colours to choose from. A Web-
Safe colour is one that displays correctly in a web browser, regardless of which computer being used (that's the
theory, anyway). You might want to use a Web-Safe colour if you're designing a project for the internet. But
you can choose one even if you're not.
To change the colour of the labels, click on a label to select it. Look in the Property box to see if it reads Label.
If so, you can now go ahead and change the BackColor property of the Label in exactly the same way that we
changed the BackColor property for our Form.
Change the colour of the other two labels to anything you like. To change the colour of more than one Label at
a time, click on one Label to select it. Now, hold down the "Ctrl" key on your keyboard and click another Label.
You'll see that two Labels now have sizing handles around them. Click the third Label with the "Ctrl" key held
down, and all three Labels will be selected. You can change the BackColor property of all three at once.
If you want to change the Font size of the Labels and Textboxes, select a control. Let's start with Label1.
1. So click on Label 1
2. Scroll down the Property Box until you see Font
3. Click on the word "Font" to highlight it
4. MS Sans Serif is the default Font
Notice that the Font property has a cross next to it. This indicates that the property is expandable. Click the
cross to see the following:
Notice that the Font property has a cross next to it. This indicates that the property is expandable. Click the
cross to see the following:
As you can see, you can change a lot of Font properties from here: the Name of the font, its Size, whether is
should be Bold or not, etc. You can also click the square box with the three dots in it. This brings up a dialogue
box where you can change the font properties in the same place.
Font: Arial
Font Style: Bold
Font Size: 10
Change the Font of the three Textboxes so that they are the same as the Labels.
When you're finished, you should have a form that looks a little more like a real programme. Time now to save
your work. Click below to see how to do this in VB .NET.
If you have a look in the top right of the Design Environment, you'll see the Solution Explorer. (If you can't see
it, click View > Solution Explorer from the menu at the top.)
The Solution Explorer shows you all the files you have in your project (Notice that the name of your project is
at the top of the tree - My First Project).
At first glance, it looks as though there are not many files in the project. But click the Show All Files icon,
circled below:
When you click Show All Files, the Solution Explorer will look something like this:
When you save your project, you are saving all these files.
To save your work, click File > Save All. The files are then saved in the Document folder on your computer,
inside of a folder called Visual Studio 2019 (or whatever version you have). Double click inside of the Visual
Studio 2019 folder and you'll see a folder called Projects. Your current project is inside of this folder. (For 2019
users, this was the folder structure we recommended when first creating a new project.)
To actually save your work as you go along, just click File > Save All from the menu bar. Or press Ctrl + Shift
+ S on your keyboard. Or click the icon in the Toolbar (the stack of floppy disks). If you save often then you
won't lose any of your work if anything goes wrong with your computer.
In the next section, we'll get down and do a bit of actual programming. It's a gentle introduction, so nothing to
get too worried about!
The textbox gets added to the top left position of your form. To move it down, hold your mouse over the textbox
and drag to a new position:
Notice the small squares around the textbox when you let go of the left mouse button:
The small squares are sizing handles. Move your mouse over one of them. The mouse pointer turns into an
extended line with arrowheads. Hold your left mouse button down and drag outwards. The textbox is resized.
Play around with the sizing handles until you're happy with the size of your textbox.
One thing you will notice is that you can't make the size any higher, but you can make it wider. The reason why
you can't make it any higher is because the default action of a textbox is to have it contain only a single line of
text. If it's only going to contain one line of text, Microsoft reasoned, there's no reason why you should be able
to change its height. A textbox can only be made higher if it's set to contain multiple lines of text. You'll see
how to do this soon.
1. Create two more textboxes by double clicking on the textbox icon in the toolbar (Or Right-click on the
selected textbox and choose Copy. Then Right-click on the Form and choose Paste.)
2. Resize them to the same size as your first one
3. Line them up one below the other with space in between
4. Try to create something that looks like the one below
Let's add some labels near the textboxes so that your users will know what they are for.
Create two more labels, and position them to the left of the textboxes. You should now have a form like this
one:
To see what your Form looks like as a programme, click Debug > Start from the menu bar. Or press F5 on
your keyboard:
To stop the programme from running, you can do one of the following:
You can also click the Stop button on the VB toolbars at the top, as in the image below:
You can also start you project without debugging. Simply click the Start Without Debugging option on the
Debug menu. You won't get any of the Diagnostic Tools, but your form will be quicker to start up.
To stop your form from running, click the X in the top right of the form. This will take you back to the Design
environment.
All right, we're getting somewhere. We now have a form with textboxes and labels, something that looks like a
form people can fill in. But those labels are not exactly descriptive, and our textboxes have the default text in
them. So how can we enter our own text for the labels, and get rid of that default text for the textboxes?
In the previous section, you just designed a form and had your first look at the code window. We'll add some
code right now.
Click your mouse on the blank line after Private Sub Button1_Click, etc, but before End Sub. Type the
following code:
number1 = 3
number2 = 5
MessageBox.Show( answer )
After typing all that, your code window should now look like this:
In Visual Studio Community 2019, you may also see lines that say references. These will be grey. It's part of a
new addition called CodeLens. If you don't want to see these lines, click Tools on the menu bar at the top. From
the Tools menu, click on Options at the bottom. You'll see a dialogue box appear. Expand the Text Editor
item, then All Languages. Select CodeLens. Deselect the Enable CodeLens option:
Before we explore what's happening with the code, save your work and then click Debug > Start Debugging
(or Start without debugging) from the Visual Basic Menu, or press F5 on your keyboard. This will launch your
programme. Click the Button once, and you should get the following:
Stop your programming, and return to the Design Environment. If you can't see your code, you can click the
Tabs at the top of the Window, as in the image below:
Click the "Form1.vb [Design]" tab to see your Form.
OK, what happened there? Well, what happened is we've just wrote a programme to add two numbers together,
and we displayed the result by using a Message Box - you very first real programme! But let's break that code
down a little more.
1. First, we started with the Dim word, indicating to Visual Basic that we wanted to set up a variable
2. Then we gave the variable a name (number1)
3. Next, we "told" VB that what is going inside the variable is a number (As Integer)
4. Two more variable were set up in the same way, number2 and answer
1. Told Visual Basic that what is going into the first variable was the number 3, and what is going into the
second variable was the number 5. To put something into a variable, you use the equals ( = ) sign. But
it's not really an equals sign - it's an assignment operator. You are assigning the value of 3 to the variable
called number1
number1 = 3
number2 = 5
The next part is a little bit more complicated, but not too complicated. What we wanted to do was to add two
numbers together. So we said
number1 + number2
Visual Basic already knows how to add up: all we need to do is "tell" it to add up. We do the "telling" in the
traditional, mathematical way - with the plus sign (+). What Visual Basic will do is to look at what we've stored
inside number1, and look at what's inside number2. It's sees the 3, sees the five, and also sees the plus sign.
Then Visual basic adds them up for you.
Except we also did something else. We said to Visual Basic "When you've finished adding up the two variables
number1 and number2, store the result in that other variable we set up, which is called answer."
means: "Add the variable called number1 to the variable called number2. Then store the result in the variable
called answer."
Think of it as working from the right-hand side of the equals sign first. Then when you have the answer, assign
it the variable on the left of the equals sign.
The final part of the programme used Visual Basic's in-built Message Box. We'll learn a more about the Message
Box later. For now, think of it as a handy way to display results.
Message boxes are quite handy when you want to display the result of some code. But we have a textbox on the
form, and we might as well use that.
So delete the line: MessageBox.Show( answer ). Type the word Textbox1, then type a full stop. You should
see a drop-down box appear. This is a list of the Properties and Methods that the Textbox can use.
Scroll down until you see the word "Text". Double click the Text property and the drop-down box will
disappear. (This drop-down box is known as IntelliSense, and is very handy. It means you can just select a
property or method from the list without having to type anything.)
The Text property you have chosen is the same Text property that you set from the Properties Window earlier.
Here, we're setting the property with our code; before, we set it at design time. But the result is the same - the
Text property of the textbox will be set to a value of our choosing.
To set a value, type an equals sign, then type a value for the Text property. We want the contents of the variable
called answer to appear in the textbox. So the rest of the code is just this:
Textbox1.Text = answer
OK, time for your first exercises. They're not too painful, and hopefully they'll giver you a better idea of what
variables are. And besides, programming is about doing, not talking. So off we go!
Exercise
Delete the values 3 and 5 and replace them with numbers of your own
Exercise
Delete the plus sign in between number1 and number2, and replace them with each of the following in turn
Exercise A
Set up another Integer variable. Give it the name number3. Assign a value of 10 to this new variable. Multiply
the value of your new variable by the variable called answer. Display the result in your textbox.
Answer to Exercise A
(Another way to assign values to variables is when you first set them up. You can do this:
number3 = 10
It's up to you which method you use. But the objective is the same - to assign a value to a variable.)
String Variables
So we've learnt something about variables, what they are and how to set one up. We learnt about the word
"integer", and that integer variables held numbers. But what if we don't want numbers? After all, our first Form
asked users to type in their First Name and Last Name. Names are not numbers, so what do we do then? Well
that's where Strings come in.
What is a String? Actually a string is nothing more than text. And if we want Visual Basic to store text we need
to use the word "String". To set up a variable to hold text we need to use As String and not As Integer. If the
information we want to store in our variables is a First Name and a Last Name, we can set up two variables like
this.
Again we've started with the Dim word. Then we've called the first variable FirstName. Finally, we've ended
the line by telling Visual Basic that we want to store text in the variable - As String.
So we've set up the variables. But there is nothing stored in them yet. We store something in a variable with the
equals sign ( = ). Let's store a first name and a last name in them
FirstName = "Bill"
LastName = "Gates"
Here, we said to Visual Basic "Store the word 'Bill' into the variable FirstName and store the word 'Gates' into
the variable called LastName. But pay attention to the quotation marks surrounding the two words. We didn't
say Bill, we said "Bill". Visual Basic needs the two double quotation marks before it can identify your text,
your String.
So remember: if you're storing text in a variable, don't forget the quotation marks!
To test all this out, add a new Button to your Form. Set the Text property of the Button to "String Test". Your
Form would then look like this (it will look rather grey and dull in newer version of Visual Studio Express):
Double click your new button, and add the following code:
Dim FirstName As String
Dim LastName As String
Dim FullName As String
FirstName = "Bill"
LastName = "Gates"
Textbox1.Text = FullName
Your code window should now look like this: (You can put in an underscore character to break up long lines in
your code.)
Concatenation in VB NET
In the two lines of code above that one, we stored the string "Bill" and the string "Gates" into two variables.
What we're doing now is joining those two variables together. We do this with the ampersand symbol ( & ).
The ampersand is used to join strings together. It's called Concatenation.
Once Visual Basic has joined the two strings together (or concatenated them), we're saying "store the result in
the variable called FullName". After that, we tell VB to display the result in our Textbox.
So, once you've typed the code, start your programme and test it out.
Once the programme is running, Click the Button and see what happens. You should have a Form that looks
something like this one:
The textbox displays the text stored in our variables, "Bill" and "Gates". We joined them together with the
ampersand ( & ). But as you can see, the two words are actually joined as one. We can add a bit of space between
the two words by using another ampersand. Change this line FullName = FirstName & LastName to this:
What we're saying here is join this lot together: the variable called FirstName and a single blank space and the
variable called LastName. When you've finished concatenating it all, store the result in the variable FullName.
Notice that we don't surround FirstName and LastName with quotation marks. This is because these two are
already string variables; we stored "Bill" into FirstName and "Gates" LastName. So VB already knows that they
are text.
Exercise
Remove one of the ampersand symbols (&) from this line in your code:
Move your cursor down a line or two. You should see that part of your code has a wiggly blue or red line under
it:
VB is telling you that it has problems with this line of code. If you hold your mouse over the wiggly blueor red
line, VB tries to provide an explanation:
The explanations VB provides are sometimes enigmatic. But you will know that there is a problem. If you run
the code, you'll get this popping up at you:
Click the NO button. Put the ampersand back in, and all will be well.
Exercise B
Amend your code so that the textbox reads Gates Bill when the Command button is clicked.
Answer to Exercise B
Exercise C
Add another string variable to your code. The variable should hold a middle name. Display the first name, the
middle name and the last name in the textbox.
Answer to Exercise C
Points to remember:
1. Your variable names cannot include spaces. So the variable MiddleName would be all right, but Middle
Name will get you an error message
2. When you're putting text into your new variable, don't forget the two double quotes
3. Remember to put in enough ampersands in your FullName = line of code