Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Learning Vb, Part 2 -- Various Techniques
Sizux
post Jan 21 2006, 12:21 AM
Post #1


Newbie [Level 2]
**

Group: Members
Posts: 29
Joined: 15-January 06
Member No.: 17,145



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.

CODE
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.

CODE
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:

user 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.

CODE
       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:

CODE
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.

CODE

>                                  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".

CODE
       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.

CODE
       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 helper’s 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:

CODE
' 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.

CODE
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.
Go to the top of the page
 
+Quote Post
final_fantasy
post Jan 27 2006, 12:26 PM
Post #2


Newbie [Level 3]
***

Group: Members
Posts: 49
Joined: 19-January 06
Member No.: 17,375



gooooooog!
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Your First Autoit(4)


 



- Lo-Fi Version Time is now: 13th October 2008 - 11:43 AM