Jim's VB.Net Notes: Intro And Lesson 01

  • From: "Homme, James" <james.homme@xxxxxxxxxxxx>
  • To: "programmingblind@xxxxxxxxxxxxx" <programmingblind@xxxxxxxxxxxxx>
  • Date: Thu, 22 Jul 2010 15:00:33 -0400

Jim's Vb.net Notes

By Jim Homme

----------


Introduction

This is my brain dump to you as I learn Visual Basic .Net. I hope it helps you.

I found, when I was learning my first programming language, Cobol, that if I 
wrote the concepts down as I learned them, they stuck better. Whatever your 
learning style, I hope you get something out of these notes.

----------


The Approach

The approach I will take is going to be simple. I'm going to try to 
intentionally use short examples that teach a concept at a time. Along the way, 
I will try to pull a few examples together that demonstrate several concepts 
that we've recently gone over.

We will repeat concepts as we move through the notes, but when we do that, with 
each successive treatment, we will dive deeper into the concepts we repeat.

----------


Programming Environment

As our programming environment we'll use a text editor called EdSharp, from 
Jamal Mazrui. So go get it from http://www.empowermentzone.com/edsetup.exe. 
Install it. Get familiar with it. Switch to the compiler setting for Visual 
Basic .Net. Then make a directory for programs. Come back here when that's done 
and let's get going.


----------



Adding A Compiler Setting For Visual Basic Console Applications

We'll write some console applications at the beginning  of the tutorial to cut 
down on and focus the learning curve. In order to get them to compile properly, 
we need to set up a line in the EdSharp main.ini file that will compile console 
applications. That's going to be extremely easy as long as you carefully follow 
these next instructions.

Open EdSharp.

Press Alt + Shift + M. This opens the main.ini file.

Press Control + F and search for the string visual basic .net. The cursor will 
land on a line that contains the command line string EdSharp uses to compile a 
windows executable Visual Basic .Net  program.

Put the cursor on the beginning of that line and press F8 to start a selection 
block.

Scroll down until you've selected the entire compiler directive and move one 
character past it.

Press Shift + F8. EdSharp selects the whole compiler directive.

Press Control + C to copy the text to the clipboard.

Create a blank line and paste with Control + V.

Change the beginning of the line to read something like Visual Basic .net 
Console Application, so that you know what it does when you pick it from the 
list of compilers.

Carefully move the cursor over to a string that reads in part winexe.

Delete the win from that string.

Save the file with Control + S.

You now will be able to compile Visual Basic .net Console applications in 
EdSharp and you're ready for the first few lessons.


----------


Lesson 01: As Tradition Would Have It

Programmers are steeped in tradition. I'm not about to break tradition, so our 
first program will be the HelloWorld program. It simply prints the words "Hello 
world!" in quotes to the screen. I'll explain what's going on after the program.

Let's get set up. For this lesson, and until I tell you to, use the console 
application compiler setting you created in the introduction to this tutorial. 
Probably the easiest thing to do is to put this tutorial file in the folder 
where you have your programs and make it a favorite in EdSharp. That way, you 
can make bookmarks in it and you don't have to go through the File Open dialog 
to select a text file to open it.

For every program after this one, try to type it into EdSharp. Don't paste. But 
do paste this one, because I want you to get used to compiling a program and 
running it without the headache of compiler errors to deal with.

In the future, I want you to type all programs in. If you type them in, you'll 
get used to seeing compiler errors. Compiler errors are messages the compiler 
puts on the screen when it doesn't understand something you put in your program.

Once you've pasted the  program, save it. Then compile it. Then go to the 
command line and run it. To do that, type it's name without an extension. It 
will print the words Hello world to the screen and wait for you to press Enter.

So for every program you write, you're going to do three things.

1. Write some code.
2. Save it.
3. Run it.

Ready? Here we go.

----------


Let's Do It: Program 01, HelloWorld.vb

Paste the below program into a separate window and save it.

' HelloWorld.vb
' Print a message to the screen.
' Wait for the user to press Enter
' Exit

imports system

Module HelloWorld
  Public Sub Main()
    Console.WriteLine("Hello, world!")
    Console.ReadLine()
  End Sub
End Module

----------


What Did We Just Do: Program 01, HelloWorld.vb

There was a lot going on in that little program. As of this writing, I don't 
know everything that happened, but here's what I know.



----------


Comments

Those lines at the top of the program that start with single quotes are called 
comments. They give you a way to make notes to whoever is reading your code. 
The compiler ignores them.


----------


The Line That Starts With Imports

This instructs the compiler to include a whole bunch of stuff from .Net. I 
included this line, because I tried the program on two computers. It wouldn't 
run on one computer without it.



----------


Code Blocks

In Visual Basic.Net, and any other language pretty much, we write programs in 
chunks of code called code blocks. It's easy to tell in Visual Basic.Net where 
a code block starts and ends. At the beginning of a code block will be a line 
that starts with a keyword. At the end of the code block will be the word end 
followed by that same keyword.

Our HelloWorld.vb program has two code blocks. One starts with Module and ends 
with End Module. The other starts with Sub and ends with End Sub.
Module/End Module

From what I understand at this point in my learning, every Visual Basic.net 
program must have at least either one class or one module, and the line at the 
top and bottom of this program satisfies that requirement.


----------


Sub Main/End Sub

From what I know right now, any program that uses .Net must have a routine 
whose name is main. This program satisfies that requirement.


----------


Console.WriteLine and Console.ReadLine

Here's what I know about these two lines.

The Console.WriteLine and Console.Readline statements are there to run 
something called methods. Methods usually act on data in some way. The second 
line is just there to make the program pause while you examine the line on the 
screen that says "Hello world!" You'd normally use it to get data from someone 
at the console who is using a keyboard to type stuff for your program to do 
something with.

The Console.ReadLine method gets data, puts something onto the end of it that 
makes your console take a new line, and sends all of that to the screen.


----------


Sub/End Sub

The word right before main in the program tells Visual Basic that we're 
starting something called a subroutine. A subroutine is another kind of code 
block you can call to make it do stuff with data if you want to.  Later on in 
the tutorial, I'll have more to say about the differences between subroutines 
and methods.



----------


Our Program's Relationship To .Net

The Imports System at the top of the program tells the compiler to bring in a 
whole bunch of stuff in a thing called a name space. The name space is called 
System. We're using something in the System name space called Console. Console 
is a class. The Console class contains methods. We used two of them.


----------


Lesson 01 Conclusion

As I said in the introduction to this tutorial, we are going to repeat a lot of 
concepts, but go more deeply into them each time we do, so try to put aside 
your curiosity about any questions you may have about the things we've just 
barely touched on and get a feel for running the program and think about how 
it's interacting with the system console.

Lesson 01 Lab: Your Turn

Copy the program to a new name and make sure the file name ends in .vb. Change 
the stuff at the top of the program after the single quotes. Compile it. Did 
anything change?

Go down inside the main subroutine and change the stuff inside the quotes. 
Recompile and run the program. What happened this time?

Purposely introduce a misspelling in one of the lines and try to recompile. 
What happened?

See you in the next lesson!

Jim




Jim Homme,
Usability Services,
Phone: 412-544-1810. Skype: jim.homme
Internal recipients,  Read my accessibility 
blog<http://mysites.highmark.com/personal/lidikki/Blog/default.aspx>. Discuss 
accessibility 
here<http://collaborate.highmark.com/COP/technical/accessibility/default.aspx>. 
Accessibility Wiki: Breaking news and accessibility 
advice<http://collaborate.highmark.com/COP/technical/accessibility/Accessibility%20Wiki/Forms/AllPages.aspx>


________________________________
This e-mail and any attachments to it are confidential and are intended solely 
for use of the individual or entity to whom they are addressed. If you have 
received this e-mail in error, please notify the sender immediately and then 
delete it. If you are not the intended recipient, you must not keep, use, 
disclose, copy or distribute this e-mail without the author's prior permission. 
The views expressed in this e-mail message do not necessarily represent the 
views of Highmark Inc., its subsidiaries, or affiliates.

Other related posts: