Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Thursday, July 21, 2011

Creating Variables in VB .NET

Variables
Why are we discussing variables? And what is a variable? 


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.


Now examine this:

Dim number1 As Integer
Dim number 2 As Integer

number1 = 3
number2 = 5


That's code from Visual Basic Net. It's VB's way of setting up (or declaring) variables.

Here's a breakdown of the variable Declaration:

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. 





Wednesday, July 20, 2011

Creating a New Project

Time to get our hands dirty with a bit of programming. There's no putting it off any longer, I'm afraid! We'll create a new project for the next section.

So, if you already have you VB NET software open, you can get rid of the current project by clicking File from the menu bar. 
From the drop down menu, choose Close Project. You will be returned to the Start Page. Click File > New Project from the menu bars. When you get the dialogue box popping up, choose Windows Forms Application at the top. Then change the Name from WindowsApplication to Variables: (View Image)

When you click the OK button, a new form will appear.
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. (View Image)

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.

Tuesday, July 19, 2011

Starting Visual Basic.Net (2008 version)

Follow these easy steps:
Step 1: Open your VB.Net Software
Launch your Visual Basic .NET or Visual Studio software. When the software first loads, you'll see a screen something like this one:
This is the Start Page. (View Image)

This is where you can start a new project, or open an existing one. If you have created numerous projects before, you'll see it under the Recent Projects block located on the left side.

Below this, you will see two links : Open Project and Create Project. Open Project link is used to open an existing project while Create Project link is used to create a new project. 

When you create a project, the Name you gave it will be displayed on this page, as a hyperlink just like what you see under Recent Projects block. Clicking the link will open the project. 

Step 2: Create New or Open Project



To get started, click the Create Project link or Go to File > New > Project. When you do, you'll see the dialogue box appear (View Image).


By Default, the selected project type on the window is Windows Form Application. This means that you're going to be designing a Window type program to run on a computer running the Microsoft Windows operating system.

If you would like to create a web application, you can select Web > ASP.Net Web Application.


Step 3: Enter Project Name

The default name for your project is   "WindowsApplication1" but you can change this to your desired project name. For example we'll going to change this to My First Project.

The default location is located in your "My Documents" folder called "Visual Studio Projects". All of your files for your first project are then saved in this folder. You can keep the Location the same as the default but you can also change this to your desired location.  

A new folder will then be created for you, and its name will be the same as the one you typed in the "Name" textbox. 


Step 4: Click OK button

Click the OK button, and the Visual Basic NET design time environment will open. It will look like the following :


This is the Default Form (View Image).

This is actually your program. Whatever you see on this form, its size, color and its contents, this is exactly what your program looks like.


Step 5: Run the Program
To run the form, click Debug > Start or Press F5 or , your program 
will look like this (View Image).


Congratulations! You have just made your first Windows Application Program .

Visual Basic.Net Online Programming