Jump to content
xisto Community
Sign in to follow this  
Sizux

Learning Vb Part 2 -- Various Techniques

Recommended Posts

Notice: Coding between compilers may vary. For example, "messagebox.show()" is the message prompt in Express Edition whereas "msgbox()" is the prompt in Visual Basic 6.0. All code in this guide is Express Edition.

 

In part on, we learned the basics of Visual Basic, including Strings and Integers. Once you feel comfortable with it, you can move onto this part. As said before, the beginning part of learning is the hardest. Since this is still the beginning, you may struggle on some things. Don't worry though; things are repeated numerous times.

 

First off, we are going to learn how do use an Input Box, a box where you can enter text and record it. To do that, we need to use another Dim. For this particular case, we need to use the Dim Object command. Dim Object, when declared, tells the compiler to store an object into the memory. Objects can be many types of forms or message boxes--in this case, an input box.

 

Dim box as Object

The variable "box" is now an object. So we need to tell the compiler what object it is. We want Mr. box to be an input box, so let's say so.

 

box = InputBox("What is your name?")

The "box = InputBox" defines it. The ("") is simply text. You can add variables in there by simply removing the quotes. When you run this, you'll see something like this:

 

Posted Image

 

This is nice, but it doesn't do anything when you click OK. We're going to add a textbox afterwards saying what the user typed in the box. How do we do that? Simple.

 

       Dim box As Object        box = InputBox("What is your name?")        MessageBox.Show("Your name is " + box)

When the data is entered, it is stored in the variable "box". Objects, such as "box", can store both alphabetical (strings) and numerical (integers) characters. After the user has entered a value, the message "Your name is " will show up. Then, the value "box" (AKA: The value the user entered" will show up, following the space at the end of the message "Your name is ". Easy? Try understanding this one:

 

Dim name As Object        Dim age As Object        Dim state As Object        Dim city As Object        name = InputBox("What is your name?")        age = InputBox("What is your age?")        state = InputBox("What state do you live in?")        city = InputBox("What city do you live in?")        MessageBox.Show("Your name is " + name + ", you're " + age + " years old, and you live in " + city + ", " + state)

When you want to compare certain information, you use Signs, symbols that represent greater then, less then, and equal. Actually, there is more then that. This table shows the signs and their meaning.

 

>                                  Greater Then<                                  Less Then=                                  Equals<>                                Not equal<=                                Less then or equal to>=                                Greater then or equal to

Tip: If you confuse greater and less then signs, think of their looks. The < sign looks like an "L" if you turn it, which means "Less then".

 

We are also going to learn Branching, coding comparing multiple data and varying on their conditions. Branching must always start with "If" and end with "End If" There must also be an "Else" condition in the middle. Most compilers, however, already add the "End If" and "Else".

 

       If 5 = 5 Then            MessageBox.Show("This code is correct!")        Else            MessageBox.Show("What happened here?")        End If

Branching is like asking a question. This case, it is saying "Is the number 5 equal to the number 5?" Of course, this is true. So, the value under "If 5 = 5 Then" is what happens when this is true. The value under "Else" is what happens when it's incorrect. So, what'll show up here? Easy.

 

Read through the following code and think to yourself what it does.

 

       Dim check As Object        check = InputBox("You must be 10 years or older to access")        If check >= 10 Then            MessageBox.Show("OK, you're old enough")        Else            MessageBox.Show("In a few more years, sonny")        End If

This code is simple. If you enter a number equal to 10 or higher, the message "OK, you're old enough" will appear. However, if you say the number 9 or below (negatives count), then the other message will appear.

 

Let's now learn comments. These helpers help--and a lot. Comments are side-notes added into the script that do nothing but tell you a note. They are NOT displayed in the program itself. Here is how you do a comment:

 

' This is a comment!

Everything displayed on the line following the ' is a comment. You can use them to remind you of things. Pretty easy.

 

I'm coming to a conclusion on this tutorial. But I'll bring you one nice tip; check over your work. You can make pretty stupid mistakes that you miss. Some compilers will find them for you, but be cautious.

 

Dim apple as delicious

If you have any questions, just post them. I'll try to answer as many as I can. Please remember that I'm not an expert at this; so don't ask me to build an operating system.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.