Visual Basic for Very Bright Kids - Part 3 - The Visual Basic Language

February 2007

When one learns to read and write a new natural language, one would usually cover at least:

  • Vocabulary - the words of the language

  • Grammar - how to put the words together correctly, in the accepted way. This includes things like proper sentence structure and punctuation.

  • Comprehension - understanding the meaning behind what is written down. To comprehend means to understand.

Bb330923.bb283db4-e679-43ed-b263-634de6930076(en-US,VS.80).png

The same is roughly true of learning to read and write a computer language. There are some basic words and other bits which are the pieces to be used in your programs. You put these together in specific ways to make something similar to sentences, with punctuation marks. All of this is for the purpose of conveying some meaning to the computer.

We won't break up our lessons into precisely the same headings above, but you will see those aspects come through in the following chapters.

Vocabulary

Just as in natural languages, Visual Basic has a certain number of words that are directly a part of the language, but there are additional words that can be legally included from outside the language.

In English you may write "There is a Gorilla named Juju that lives in the Ugandan jungle." Most of these words are English words - you'll find them in a dictionary of the English language. But the words "Juju" and "Ugandan" are not - they are proper nouns. Does that mean the sentence is not correct? No - it is quite legal to use words from:

  • the list of all names of humans and animals

  • the official list of country names

  • ... and lots of other word lists

Similarly, we will mention words that are directly part of the Visual Basic language. But that doesn't mean these are the only words that you'll use in your programs. Depending upon what it is that you're talking about, you will most likely use many extra words that are borrowed from other lists, libraries, etc. In particular, Microsoft has written a vast library of code called the .NET Framework Class Library, which you will definitely be using in your Visual Basic programs. It has many class names, field names and method names that are not internally part of Visual Basic, but you can certainly use these names in your Visual Basic programs.

You yourself will also define new classes, fields and methods and give them your own names - these then become words that you can legally use in Visual Basic programs. This is like you making up a new name, "SpangleFoot" for your pet - you're then legally allowed to use that name in English sentences.

Here are some of the built-in words of the language - some you need to know in order to write basic programs. These are all mentioned in this book.

  • Class

  • Do

  • Else

  • False

  • For

  • If

  • Integer

  • New

  • Nothing

  • Private

  • Protected

  • Public

  • Return

  • Shared

  • String

  • Me

  • True

  • Imports

  • Loop

  • While

This is not a complete list, but don't worry - there are not many Visual Basic words beyond this list. Computer languages tend to have far less words than natural languages.

What do you do with the words of a language? You put them together in well-structured sentences, according to certain rules. That's called grammar. That's what the rest of the chapters in this section are about.

Some General Grammar Rules

In this chapter, we cover some general information about grammar in Visual Basic. In technical terms, this is often referred to as syntax.

Capital and Small Letters

The Visual Basic language is generally not case sensitive, although it will transform keywords into a standard case configuration (first letter is capital and the rest are small letters in most cases) and force the case of variable names to conform to the case of the name given during declaration.

Built-in Words

The keywords listed in the previous chapter always have to be written exactly as they are shown there.

If you ever write the word "IF" instead of "if", it will simply get converted to "If".

Names That You Make Up Yourself

Now what about words that you've made up yourself? Suppose for example, you have created a class called "myClass" which has a field called "numberOfSpots".

Class myClass
      Dim numberOfSpots As Integer
End Class

Do small versus capital letters matter? The answer is Yes and No.

Yes because whenever you talk about these names in a Visual Basic program, you must write them exactly as you originally defined them. The following, for example, will pass since "x.NUMBEROFSPOTS" will automatically be converted to "x.numberOfSpots".

Class myClass
      ' Field
 
      Dim numberOfSpots As Integer
 
      ' Method
 
      Public Sub Main()
            Dim x As MyClass = New MyClass() 
            x.NUMBEROFSPOTS = 365
      End Sub
End Class

The above code will be converted to the code shown below:

Class myClass
      ' Field
 
      Dim numberOfSpots As Integer
 
      ' Method
 
      Public  Sub Main()
            Dim x As myClass =  New myClass() 
            x.numberOfSpots= 365
      End Sub
End Class

No because if you choose to write a class name/field name/method name/etc. with a mixture of small and big letters say NUMBEROFSPOTS, then you cannot have another class name/field name/method name/etc. as numberofspots or NuMbErOfSpOtS, etc).

Statements

In English, we speak of sentences, statements, phrases, etc. All of these are different ways of putting words together. Each of these is ended with a particular punctuation mark. A sentence is ended with a period (also called a full stop). A phrase may be ended with a semi-colon (;).

In Visual Basic, we keep it a bit simpler. We speak mainly of statements, and statements are generally ended with a new line.

Bb330923.663d0fce-18af-445e-962d-ff7c05d9b470(en-US,VS.80).png

Here, for example, is a piece of code with 4 statements:

Dim x As String
x = "Nobody wins in war"
Dim m As myClass = New myClass()m.numberOfSpots = 3

You need to put each statement on a new line in Visual Basic for the computer to understand each statement.

I’m willing to bet that many errors that you encounter in your programs will be because you forgot to write different statements on separate lines, but after forgetting about 5000 times, you’ll probably start to get into the habit. Luckily, Visual Basic Express is quite smart with realizing you’ve left out things like that and will tell with a squiggly line or some other gadget to show you how stupid you’re being.

Code Blocks - Grouping Statements Together

In English, we use paragraphs to group together statements that are closely related.

In VB.Net we use the "End" keyword to end a block that groups a number of statements together.

Bb330923.d612b04f-64c5-42fb-90f7-ae6f3a9a90a3(en-US,VS.80).png

Let's look at an example. Suppose we have a program that asks the user questions and checks their answers. Suppose we want to do 2 things only if somebody answers correctly. We may write code as follows :

If userAnswer = correctAnswer Then
      Console.WriteLine("That is correct")
      score = score + 1
End If
 
AskNextQuestion()

The two statements inside the "If ... End if" block will both be run if the user's answer is the correct answer - they have been grouped together into a block.

You have already seen many other examples with blocks ending with "End" in this book. Every time a class is defined, you put all its code inside "Class ... End Class" block:

Class MyClass
      Dim numberOfSpots As Integer
End Class

And, when you write a method in your class, you group it's statements together with "Sub ... End Sub" too :

Class MyClass      
      Dim numberOfSpots As Integer
 
      Public Sub Main()
            Dim x As MyClass =  New MyClass() 
            x.numberOfSpots = 365
      End Sub
End Class

Similarly you group a function's statements with "Function ... End Function".

Comments - Helping the Humans Out

You will have noticed that many books have notes or comments written at the sides or at the bottom of the pages. These comments are not part of the core text but are usually extras that explain something further about the core text.

You may wonder whether you can do the same in your computer programs, and you can. You will often want to write some comments for either yourself or other programmers, to explain what's going on in your code. In fact, this is considered good behavior by programmers. It's true that a programmer should be capable of figuring out what's going on in a program, but why waste people's time when a short English comment will quickly explain the basics about what your code is doing.

And if you're thinking "I'm the only one who will read my code", trust me, when you come back to your program in a few month's time you'll wish that you'd commented your own code too.

Since these comments are for people and not the computer, you need some way to tell the computer to ignore whatever you write in the comments sections. You need to "hide" these comments from digital eyes.

Bb330923.9a79e0f1-623a-499a-a6de-1d122270005d(en-US,VS.80).png

To comment out a line, you can simply put a ' character at the beginning of the line.

The following piece of code shows the usage of comments:

Private Sub DrawCircleInSquare(ByVal left As Integer, ByVal top As Integer, _
                        ByVal size As Integer)

      'This is a method for drawing a red circle.
      'Pass the size of the circle in the 'size' parameter.
      'Pass the position to draw at in the 'left' and 'top' parameters.

      Dim g As Graphics =  Me.CreateGraphics()  ' Prepare drawing surface
      Dim redPen As Pen =  New Pen(Color.Red,3)  ' Use a red pen
 
      ' Draw a square (a rectangle with equal width & height)
      g.DrawRectangle(redPen, left, top, size, size)
 
      ' Draw a circle (an ellipse with equal width & height)
      g.DrawEllipse(redPen, left, top, size, size)
 
      ' Clean up
      g.Dispose()
End Sub

When the computer encounters the character ', it ignores anything on the rest of that line.

Indenting Your Code

By now you've probably noticed that we tend to indent code like this:

If userAnswer = correctAnswer Then

Bb330923.c34db7e4-2592-4d23-8375-21b15e7850f0(en-US,VS.80).png

      Console.WriteLine("That is correct")
      score = score + 1
      If score > 10 Then

Bb330923.c34db7e4-2592-4d23-8375-21b15e7850f0(en-US,VS.80).png

            Console.WriteLine("You have qualified!")
      Else 

Bb330923.c34db7e4-2592-4d23-8375-21b15e7850f0(en-US,VS.80).png

            Console.WriteLine("Bugger. Keep going ...")
      End If
End If

rather than writing it all mashed together like this:

If userAnswer = correctAnswer Then
Console.WriteLine("That is correct")
score = score + 1
If score > 10 Then
Console.WriteLine("You have qualified!")
Else 
Console.WriteLine("Bugger. Keep going ...")
End If
End If

This is in order to make it clear where a particular block of code begins and ends. As your programs get longer, it gets harder and harder to see the beginning and end of a block. Indenting makes it a lot easier to read the code with meaning.

Failing to indent does not break the code - indenting is not a grammatical requirement - but it is considered "good practice" and you should get into the habit.

To indent code when writing within Visual Basic Express, you can select one or more lines of code and click the TAB key on your keyboard. If you need to do the opposite, hold the SHIFT key and press TAB. Alternatively, you can click the

Bb330923.40da76b6-4442-49e8-a6dc-cdc131752a77(en-US,VS.80).png

icons on the toolbar.

Variables

The word "variable" means "changeable" - a variable is a thing whose value can change.

Here's a typical way we might use a variable. We choose to call the variable "theAnswer" in this case, because we want it to hold the answer of a sum, although we could have chosen any other name.

Private Function addPositiveNumbers(ByVal x As Integer, ByVal y As Integer) As Integer
      Dim theAnswer As Integer
      If (x > 0) && (y > 0) Then          ' If x and y are positive numberstheAnswer = x + y' Add them & put the sum into answer
      Else                    ' OtherwisetheAnswer = 0' Put zero into answer
      End If
      Return theAnswer
End Function

In this example, the variable "theAnswer" is used to hold the sum of whatever is passed in the parameters x and y. At the end, we then send back whatever value is being held in theAnswer.

It's interesting to note that "theAnswer" is really just a place in the computer's memory. When the computer sees the line "Dim theAnswer As Integer", it finds a free spot in its memory and names it "theAnswer". When it reaches the line "Return theAnswer", it goes and fetches whatever number it had stored in the place called "theAnswer". In the example above, since it's now finished all it was asked to do, it would then free up that memory space for other purposes.

Bb330923.d4ca8f9c-ebf4-4d91-bacd-b86292bb3e34(en-US,VS.80).png

It's Not That Strange Really

When we humans work with things in our brains, we also use places in our memory to hold answers; but we do it almost without realizing it. Suppose you're asked to

  • add the numbers 10, 20 and 50

  • then multiply that answer by 2

One likely way you'd approach this is

  • Work out 10 + 20. Hold this answer (30) in your head.

  • Work out 30 + 50. Now hold this answer (80) in your head.

  • Work out 80 x 2. Hold that in your head until you're ready to give it to the person who asked you to work this out.

  • Once you've done what you needed to do, stop trying so hard to remember it - that answer is of no value anymore - rather free up your memory to be used for other things.

So we're not always that different from our digital friends.

Variables Are Always of a Particular Type

A variable is really a lot like a field or a parameter except that it does not have anything directly to do with a particular class or object - we just need a temporary place to store something so we can work with it.

But variables, just like fields (which are actually a kind of variable), are always of a particular type. E.g.

Dim myString As String
Dim theAnswer As Integer
Dim isFinished As Boolean

(If you haven't read the chapter "Fields and Their Types", consider going and reading it so you understand more about "types")

Operators

Suppose you write the following in one of your programs:

Dim theAnswer As Integer = x + y

and in another place you write

Dim theAnswer As Integer = x - y

In the first case you're using the symbol for adding and in the second case subtracting. These actions you're performing on numbers are called operations and their symbols + and - are called operators - they operate on things. An operator takes one or more things in, operates on them and pushes some resulting value out.

This picture illustrates the plus operator, working on 2 numbers. In this example it takes in the values 5 and 2 in and puts out the value 7 

Bb330923.15144ad0-958b-4425-a4d0-710be38b5682(en-US,VS.80).png

And here is the minus operator, taking the same values in but putting a different value out:

Bb330923.1808ed45-5f61-4929-a1fd-15011bd89c6e(en-US,VS.80).png

All computer languages have at least a few operators and it is very important to be familiar with them. So let's cover the one's you're likely to see often in Visual Basic.

Symbol

Operation

Discussion

+

add 2 things

  • When using number types, this is just as in math. E.g. 25 + 5 gives the number 30.

  • When using strings, it sticks the strings together.

    E.g. "how" + " are" + " you" produces the longer string "how are you".

-

subtract two things

  • Just as in mathematics. E.g. 25 - 5 gives the number 20

/

divide

  • Since keyboards don’t have a ÷ key, the slash is used to mean "divide". E.g. 20 / 5 gives the number 4.

*

multiply

  • Multiplication works just as in mathematics. E.g. 25 * 5 gives the number 125.

>

Greater than

  • Example 1: Writing 5 > 2 will give the answer "True"

  • Example 2: Writing 5 > 17 will give the answer "False".

<

Less than

Example 1: Writing 2 < 3 will give the answer "True"

12 < 11 will give the answer "False"

=

Set equal to

  • This is used to set a variable equal to some value.

  • Example 1: Dim x As Integer= 512

  • Example 2: Dim y As String= "Hello how are you"

= =

Test if equal to

  • This is used to check whether two things are equal.

  • Example 1: 13 = 70 will obviously give the answer "False"

  • Example 2: x = 15 will give the answer "True" if x has the value 15

  • Example 3: "HELLO" = "hello".ToUpperCase() will give the answer "True"

<=

Less than or equal to

  • Example 1: Writing 2 <= 3 will give the answer "True"

  • Example 2: Writing 2 <= 2 will give the answer "True"

  • Example 3: Writing 2 <= 1 will give the answer "False"

>=

Greater than or equal to

  • Example 1: Writing 23 >= 23 will give the answer "True"

  • Example 2: Writing 23 >= 2 will give the answer "True"

  • Example 3: Writing 23 >= 33 will give the answer "False"

Not

Not

  • Example: Not( 5 > 17 ) means "is it not the case that 5 > 17 ? The answer will be "True" since it is not true that 5 is greater than 17. 

<>

Not equal to

  • Makes it easy to check whether something is "not equal to".

  • Example: 5 <> 12 will give the answer "true" since 5 is not equal to 12.

And

And

  • This allows you to test whether more than one thing is true. The following example checks whether both the password and username are correct :

    If ( pwd = "Pass") And (user = "jim") Then Return True

    You could just as easily test more than two conditions – there is no limit to how many times you can use && in a statement.

Or

Or

  • The keyword "Or" allows you to check whether any one of a number of things is true. The following example checks whether favorite color is green OR the age is greater than 12.

    If ( favcolor = "green") Or (age < 12) ThenReturn True

&

add 2 strings

  • It sticks strings together.

    E.g. "how" & " are" & " you" produces the longer string "how are you".

  • Generally, & is used instead of + in Visual Basic

Converting Between Types

Suppose we included the following code in a program:

' Declare some variables

Dim x As Integer
Dim y As String
Dim z As Integer

' Put values into x and y

x = 5y = "17"

' Calculate the product of x and y and put the answer into z

z = x * y

The last line would work in this case. The variable "z" would have the value 85.

In Visual Basic, the result depends on data type checking. Visual Basic generally allows implicit conversion of any data type to any other data type. Data loss can occur when the value of one data type is converted to data type with less precision or smaller capacity, however a run time error message will occur if the conversion fails.

Consider the following example:

' Declare some variables

Dim x As Integer
Dim y As String
Dim z As Integer

' Put values into x and y

x = 5y = "funny"

' Calculate the product of x and y and put the answer into z

z = x * y

In this case too, there would be no error at compile time. But when you run the program, you will get the following error message:

"Conversion from string "funny" to type 'Double' is not valid."

To ensure that such checking should be done at compile time, we need to set Option Strict to On. Option Strict ensures compile time notifications of these types of conversions.

The last line above would now cause an error at compile time - the computer will complain because you cannot multiply strings - and y is a string type.

Bb330923.bf2c1059-71d4-48ca-beea-54aa4c80f694(en-US,VS.80).png

With Option Strict On, it is possible to convert strings to numbers. The following shows how you could get around the above problem, since the string "17" can be sensibly converted to the number 17. It just takes a small change to the last line.

z = x * Convert.ToInt32( y )

What we've done is to use Microsoft's class called "Convert" and call its method "ToInt32", which converts to one of the integer types. Now that y has been converted to an integer, the computer will happily multiply the two numbers.

The "Convert" class has many other methods but the ones you'll most often need are:

  • Convert.ToInt32()

  • Convert.ToString()

  • Convert.ToBoolean()

Visual Basic also provides conversion functions like CBool, CByte, CChar, CDate, CDec, CDbl, CInt, CLng, CObj, CShort, CSng, and CStr.

Note that Option Strict is Off by default in Visual Basic.

It's important to remember how fussy computers are about types.

Bb330923.4147392a-344e-4ea4-b1db-96a5e4b4741f(en-US,VS.80).png

Casting

Another way that values can be changed from one type to another is through something called casting. To cast something means to convert it to a different type, but unlike the "Convert" examples above, casting only works when the type you're changing from is very similar to the type you're changing to. It is useful, for example, for converting between two number types, but is useless for converting between a number and a string.

Visual Basic provides two options for casting, namely, CType and DirectCast. The main difference between the two is that DirectCast only works if the specified type and the runtime type of the expression are the same. This difference only appears when you're converting from an object type to a value type, or unboxing a value.

For instance, the following DirectCast operation will fail because the boxed type of the object O is not an integer:

Dim Obj As Object = 1.5
Dim Int As Integer = DirectCast(Obj, Integer) ' Causes a run-time error.
Dim Int As Integer = CType(Obj, Integer) ' Does not cause a run-time error.

The DirectCast operation in this example, on the other hand, will succeed:

Dim Obj As Object = 1
Dim Int As Integer = DirectCast(Obj, Integer) ' Succeeds.
Dim Int As Integer = CType(Obj, Integer) ' Succeeds, but is slower than the DirectCast.

Note that CType includes all of the VB conversion functions. These are CBool, CByte, CChar, CDate, CDec, CDbl, CInt, CLng, CObj, CShort, CSng, and CStr.

Branching

Often when you want the program to do something, the line it must run next depends on something. You want the program to "branch off" one way in one case but branch off another way in another case. Visual Basic has two branching structures to allow this, the If statement and the Select Case statement. We won't discuss the switch statement here since it's more complicated and you can achieve the same result using the If statement.

Bb330923.b5e3d4ca-7a36-4be4-8829-99eded383975(en-US,VS.80).png

The If statement is easy to understand. You check something to see if it’s true and if the computer finds it to be true it runs the code in that block. Otherwise it falls down to the next block and tests that one. It keeps falling through until it either finds something true or reaches the end of the entire If.

If UserAnswer = "Bald Eagle" Then
      Console.WriteLine("Yup, you got it right !")
Else If UserAnswer = "Eagle" Then 
      Console.WriteLine("Almost – the answer is ‘Bald Eagle’")
End If

You will often want to have a final "else" block at the end too – so if none of the things higher up were true, the code in the "else" block will be run.

If UserAnswer = "Bald Eagle" Then
      Console.WriteLine("Yup, you got it right !")
Else If UserAnswer = "Eagle" Then 
      Console.WriteLine("Almost – the answer is ‘Bald Eagle’")
Else
      Console.WriteLine("Sorry, you got it wrong. Try again.")
End If

Looping

What if you need to do something over and over again? Take, for example, writing the numbers 1 to 10. It would be foolish to do it this way:

Console.WriteLine("Number " & 1)
Console.WriteLine("Number " & 2)
Console.WriteLine("Number " & 3)
Console.WriteLine("Number " & 4)
Console.WriteLine("Number " & 5)
Console.WriteLine("Number " & 6)
Console.WriteLine("Number " & 7)
Console.WriteLine("Number " & 8)
Console.WriteLine("Number " & 9)
Console.WriteLine("Number " & 10)

And it would be REALLY foolish to do it that way if you needed to write numbers from 1 to 15000!

That’s where loops come in – we can do things over and over again with very little code. Visual Basic has a few kinds of loops, but we’ll just cover two of them.

The For Loop

For loops are usually used in a counting fashion. You can think of a FOR loop like this:

  • start with a counter set to some value (E.g. 1)

  • keep going around until that counter gets to some specific value (E.g. 10)

  • each time we go around, do something specific to that counter (E.g. add 1 to the counter)

The following piece of code - only 3 lines - writes out exactly the same as the 10 lines of code above.

For x As Integer = 1 To 10
     Console.WriteLine( "Number " & x )
Next

By default, the value of x (loop index) is incremented by 1 between loop iterations. This increment can be set to any numeric value by adding a Step Clause in the For loop syntax.

For x As Integer = 1 To 10 Step 2
     Console.WriteLine( "Number " & x )
Next

Bb330923.20b00334-7037-4be8-b82d-f2248fa8f938(en-US,VS.80).png

You can also have "for" loops count downwards. This is simply done by specifying Step with a negative value.

For x As Integer = 10 To 1 Step -1
     Console.WriteLine( "Number " & x )
Next

This says "starting with x equal to 10, and as long as x is greater than or equal to 1, subtract 1 from x".

The While Loop

Think of a WHILE loop like this: "while something is true, keep going round and doing this block of code again and again".

Bb330923.bf6554b9-2dbf-4e9d-88f3-6cdd340b639b(en-US,VS.80).png

"While" loops can be used to achieve exactly the same as the "for" loop example above, but here is an example that has nothing to do with counting. This code keeps asking the user to type the word "smokey" in at the keyboard. Only once they type it correctly does it jump out of the loop and say "Thank You". Notice that the condition we check each time we go around is "s <> smokey", which means while the word typed in is not equal to 'smokey'.

Dim s As String = ""
While  s <> "smokey" 
      Console.WriteLine("Please type the word 'smokey' : ")
      s = Console.ReadLine()
End While  
Console.WriteLine("Thank You")

Here is an example which uses a while loop to do exactly the same as the "for" loop example we showed further above. We simply introduce a variable (x in this case) to use as a counter.

Dim  x As Integer = 1
While  x < 11 
      Console.WriteLine( "Number " & x )  
      x = x + 1
End While

You can see that x starts out being one but gets 1 added to it each time we go around the loop. Each time we go around again, the computer checks again "is x still less than eleven?" Eventually x gets to be equal to 11 and then the computer jumps out of the loop.

Never-ending Loops

Hey, want to try something really stupid? Leave out the line x = x + 1. That way, x will NEVER get any bigger and so it will ALWAYS be less than 11 and we’ll go around and around the loop FOREVER – or at least until the computer runs out of memory, throwing up an ugly error message. Whenever you write an endless loop, you should feel suitably embarrassed.

Bb330923.aa9d0449-fe44-4920-841e-b762bdbb4ae9(en-US,VS.80).png

Whole Program Structure

Introduction

In the previous chapters, we've been looking at specific concepts and how to explain each of those ideas to the computer. But we have not considered what a "whole program" should look like. How do we start the program and what are the parts that have to be there for it to work.

So in this chapter we discuss the basic structure that a Visual Basic program must have. The idea is not to confuse ourselves with all the detail - just to understand the basic outline of a program - the skeleton.

There are Several Different Types of Visual Basic Programs

If you went camping in the mountains for the weekend, you would use a structure such as a tent - not a house that needs brick foundations. You use a structure that suits the environment you're in. Tents and houses are basically the same, but tents are specifically structured for camping purposes.

Bb330923.e1170410-5157-4835-b741-50261db535d2(en-US,VS.80).png

In a comparable way, Visual Basic and the .NET framework can be used to create applications for different environments. The language is the same in each case, but there are some specific differences in how you structure the program - depending on the environment you're working in. Here are some examples of different types of Visual Basic programs.

  • Windows Forms applications

  • Class libraries

  • Windows services

  • Console applications

  • ASP.NET web applications

This book focuses on only two of these types of programs - Console applications and Windows Forms applications. But the knowledge you gain in these two environments is also directly useful in the other types of applications.

The Structure of Console Applications

A console application is one which can display and read text only - no pictures, graphs, etc. If you've ever opened a "command prompt" and typed a command like DIR to see a list of files, then you've already used a console application. When you want to try something out quickly, or want to write a program that doesn't need anything other than text, you will often use a console application, because that's the simplest type of program you can write with Visual Basic.

Here is an example of a simple, but complete, console application:

1. Imports System 
2.
3. Class PleaseSayYo
4.    Shared Sub Main()
5.          Console.WriteLine("Yo!")
6.          Console.ReadLine()
7.    End Sub
8. End Class

And this is what you'll see on the screen when you run it. It simply writes a line of text to the screen:

Bb330923.099ca06b-35f5-4cfd-ad4a-df8c0c808910(en-US,VS.80).png

The program's pretty short isn't it? But let's break it down and discuss the structure.

  • Line 1 tells the computer that something later on in our program is going to be using some class from the namespace called "System". The particular class whose code we'll be using is "Console". This is a class, written by Microsoft, which has all sorts of things that console applications need. Instead of us writing lots of lines of code to do something simple, like write a word to the screen, we simply call the method "WriteLine" from the System.Console class.

  • Between lines 3 and 8 we "define the class". In the class we write our program by writing down properties and methods. This particular class has no properties (it just doesn't need any) but does have one method.

  • Between lines 4 and 7, we define a method. This method is very much like the methods we discussed earlier in this book, but it is a special one - it MUST be called "Main" - we're not allowed to choose our own name. When the computer is asked to run a Visual Basic program, it needs to know where to start. It has been taught that, for Visual Basic programs, the programmer will always write a method called "Main" and that's where it must start - that's the entry point. You are allowed to put this method anywhere in your class - top, bottom or anywhere else - the computer will find it as long as you've called it "Main".

  • In line 5, we call the "WriteLine" method from the System.Console class, which writes out the text we specify.

Console applications can, of course, do way more than what we've shown here, and they will typically have a lot more code, but generally they start out with a basic structure like the two examples shown above.

Shared Methods

The one strange word you'll see in the program above is "Shared". To explain this, let's first write a slightly more complex example of a console application. Read the "Main" method through carefully - do you understand what it's doing ?

1. Imports System
2.
3. Class SimpleConsoleApp
4.    Public  Sub MySimpleMethod()
5.         Console.WriteLine("Yo!")
6.    End Sub
7.
8.    Shared Sub Main()
9.          Dim s As SimpleConsoleApp
10.         s = New SimpleConsoleApp()
11.         s.MySimpleMethod()
12.   End Sub
13. End Class

This example really shows the object-oriented approach better than the previous one. Instead of just writing our code directly in the "Main" method, we have, in lines 9 and 10, declared and constructed an object - an instance of this class. Then, in line 11, we have called a method of that object.

You'll notice that the method "MySimpleMethod" does not have the word "Shared" in front of it, while the method "Main" does. This is why:

  • The word "Shared" is added to the beginning of a method definition if that method is not meant for operating on a specific object.

  • Now since the "Main" method is the "entry point" method, it is called before any objects are constructed - so it has to be defined as "Shared".

Here is a further example to illustrate the "Shared" concept. We draw your attention particularly to the italic text.

Imports System
 
Class SchoolTest
    Public testName As String
    Public total As Decimal
    Public myScore As Decimal
 
    ' A constructor method for this class
    Public  Sub New(ByVal tn As String, ByVal s As Integer, ByVal tot As Integer)
        Me.testName = tn
        Me.myScore = s
        Me.total = tot
    End Sub
 
    ' A SHARED method to calculate the percentage from ANY two numbers
    Public Shared Function CalculateAnyPercentage(ByVal numerator As Decimal, _
                              ByVal denominator As Decimal) As DecimalDim percentage As Decimal = (numerator / denominator) * 100Return percentageEnd Function 
 
    ' An INSTANCE method to calculate the percentage of an instance of SchoolTest
    Public Function CalculateThisPercentage() As DecimalDim percentage As Decimal = (Me.myScore / Me.total) * 100Return percentageEnd Function 
 
    Shared Sub Main()
        ' Use the Shared method to calculate a percentage without any instance
        Console.WriteLine("123/200 = " + SchoolTest.CalculateAnyPercentage(123, 200))
 
        ' Create a new instance of a SchoolTest, passing in the scores
        Dim t As SchoolTest =  New SchoolTest("Geography",12,60) 
 
        ' Use the instance method to calculate the percentage of this instance
        Console.WriteLine("Percentage for test = " + t.CalculateThisPercentage())
 
        ' Wait until ENTER is pressed
        Console.ReadLine()
    End Sub
End Class

The important thing to notice is that we have two very similar methods – they both calculate percentages – but their usage is quite different. The one with the word "Shared" in it can be used without any need to declare a New SchoolTest() instance, while the other one can ONLY work on an instance. In the above example, our instance is named "t".

So Shared methods have a kind of "I’m above all this – I work where I want to!" feel to them, while instance methods acknowledge "I only come into being when an actual object arises – and then I only work inside that object".

The Structure of Windows Forms Applications

Once you know the general structure of a Console application, it's easy to graduate to more complex program types, because the basic structure remains the same. Here is a simple Windows forms application.

Imports System.Windows.Forms
 
Class SimpleWindowsApp
       Inherits Form

      ' Constructor method
 
      Public  Sub New()
            Me.Text = "A really Simple Form"
      End Sub
 
      ' Entry point for the program
 
      Shared Sub Main()
            Application.Run(New SimpleWindowsApp())
      End Sub
End Class

The two "new" things here are:

  1. The "Inherits Form" piece

    This is saying "my class is going to inherit everything from the class called 'Form'". This is quite an important secret, discussed in the chapter named "Inheritance". Microsoft has written a large set of classes which know how to create buttons, menus, picture boxes, checkboxes and all sorts of other things to put in applications. Simply by adding "Inherits Form", your own class can suddenly have access to all those things.

  2. The "Application.Run" bit. This is the standard way to start a Windows forms application. It is a method which "runs" your forms application. You pass to that method a new instance of your class.

Using Class Libraries

A library, in the real world, is a place that contains a large number of books and other sources of information. When people need to know something, they don't have to go and discover it all over again and write their own book - they can simply refer to the correct books, saving a lot of time and effort.

Bb330923.47dcfa5c-2fc7-49f4-8a64-45e152c8b79c(en-US,VS.80).png

In the same way, people all across the world have written huge libraries of Visual Basic code. So, instead of us writing everything ourselves, it often makes sense to use someone else's libraries for parts of our programs.

This still takes some effort - you need to look up what's in the library and understand which parts are useful to you - but it allows you to concentrate more on doing what your program needs to do, rather than wasting a lot of time writing the background code.

By far the most important library for Visual Basic is Microsoft's ".NET Framework Class Library". You can't really write any useful program that doesn't use something from it. Part III of this book is all about that library. When you install the .NET Framework on your computer, the library is automatically loaded and is therefore available to your programs. You just need to know how to refer to it - how to use it from your programs.

An Example

The "System.Drawing" part of the .NET Framework Class Library contains many useful classes for working with pictures.  Let's use it to illustrate how to use other people's code in your own program.

The System.Drawing.Image class has a method called RotateFlip, which will rotate (turn around) or flip (reverse) any picture that you pass to it. Suppose you want to use that method in your own program - you want to load a picture file from disk, flip it horizontally, and save a copy of the flipped picture back to disk. Here's one way to do so. This is a complete, working program. (If you're experimenting, note that program 9 in the collection of example files provided with this book, is a similar program).

Class PhotoSizer
      Public Sub New()
            ' Load a photograph from disk into the computer's memory
            Dim img As System.Drawing.Image
            img = New System.Drawing.Bitmap("d:\samples\myDog.jpg")
 
            ' Flip the image horizontally
            img.RotateFlip(System.Drawing.RotateFlipType.RotateNoneFlipX)
 
            ' Save the flipped photo to a different disk file
            img.Save("d:\samples\myDogFlipped.jpg")
      End Sub
 
      Shared  Sub Main()
            Dim p As PhotoSizer = New PhotoSizer() 
      End Sub
End Class

What we've done in the example above is to use a class from a library. The class is called Image and it is in the section of the library called System.Drawing.

After creating an Image object, we called two methods of the image class. The methods' names are RotateFlip and Save.

An Easier Way

But you can see that each time you need to talk about something in the System.Drawing space, you need to write those long words again. This is a waste of energy and there is a way to avoid it. Instead, you can tell your program once at the top that you'll be using some things from the System.Drawing namespace as well as some things from the System.Drawing.Imaging namespace specifically using the "Imports" keyword.

Imports System.Drawing
Imports System.Drawing.Imaging
 
Class PhotoSizer
      Public Sub New()
            ' Load a photograph from disk into the computer's memory
            Dim img As Image = New Bitmap("d:\samples\myDog.jpg") 
 
            ' Flip the image horizontally
            img.RotateFlip(RotateFlipType.RotateNoneFlipX)
 
            ' Save the flipped photo to a different disk file
            img.Save(" d:\samples\myDogFlipped.jpg")
      End Sub
 
      Shared Sub Main()
            Dim p As PhotoSizer =  New PhotoSizer() 
      End Sub
End Class

A Note About References in Visual Basic Express

When creating a Visual Basic Express project, it is also necessary to create something called a "reference" pointing to the Class Library file which holds the library code you want to use (these files have a ".dll" extension that stands for "Dynamic Link Library"). Fortunately, the most common references are usually added for you automatically.

The picture below, for example, shows the references that are added when you create a "Windows Application" project. Visual Basic Express guesses the parts of the library that it thinks you might use for an application like that.

Bb330923.31f28466-760d-415c-b92c-38403ccd79cd(en-US,VS.80).png

In the example above, you can see that the classes of the System.Drawing namespace are held in a file called System.Drawing.dll.

If the part of the library you want to use has not been automatically referenced for you, you need to add a reference yourself. Suppose, for example, you find a code example that uses the System.Web.HttpRequest class to retrieve some information from a web server, and you want to try it. You would right-click on "References", select "Add Reference" and select the System.Web part of the library. Now you will find you can use its classes in your code.

Bb330923.3f29c305-a552-462c-943e-61929856f5ff(en-US,VS.80).png

Conclusion

So it's quite simple to include code from a library. The hard part is knowing what classes, methods, etc. are available for you to use in this vast library.

Bb330923.2e81c326-b44e-4e64-b913-603a549f7a0a(en-US,VS.80).png

That's why part III of this book has been written - to expose you to some of the useful parts of Microsoft's .NET Framework class library.