Members
Technology Zones
IBM Learning Center
Articles
Hosted By
Info
|
Rated
Read 17,286 times
Related Categories
Speed tips for beginners
Have you ever tried to write a program where you've had to type something over
and over? Well with this tutorial, you'll learn about speed saving things like
modules, loops, and the with statement.
First off, I'll start with something REALLY easy but really time saving. It's
called the with statement. Have you ever had to modify several properties of
an object at once? If so, without the with statement you'd have a LOT of typing
to do. Your code might look like this.
Text1.Text = "BlAh"
Text1.Font = "Arial"
Text1.Enabled = "True"
Text1.Height = 1000
Well, there is an way that makes it much faster. It's called the with statement.
It saves you time by making it so you don't have to type the name of the object
over and over again. You would use it like this.
With Text1
.Text = "BlAh"
.Font = "Arial"
.Enabled = "True"
.Height = 1000
End With
See the difference? A little extra code saves you from typing an object's name
over and over. Make sure you remember the "End With". Also, it's a good idea
to indent the property changes inside the with tags, since it's good to get into
the habit of indenting.
The next thing that's really useful is loops. They allow you to repeat a process
over and over, only typing it once. There are different types of loops. I'll
tell keep it simple and tell you about the "Until" loop. Say you had a schedule
and that gave you spaces to write down what you were supposed to be doing at
five minute intervals. Let's say you want to exercise from 3:00 to 3:30. You
wouldn't put "Exercise for 5 minutes" in every slot, would you? You'd probably
put "Exercise until 3:30" at the 3:00 slot and leave the rest blank. That's like
an "until" loop. Here's an example of an "Until" loop:
Do Until Text1.Text = "JoeJoeJoe"
Text1.Text = Text1.Text + "Joe"
Loop
That would make Text1.Text say "JoeJoeJoe" and you didn't have to type "Text1.Text
= Text1.Text + "Joe"" 3 times. That's how an until loop works.
One thing that's very useful is modules. First, we need to create a module. Say
you had a function, for example, changing a text box's font. If you wanted to
use it several times, for different fonts, then you could use a module instead
of typing it over and over again. Make a new project in Visual Basic, and add
a text box, and a button. Go to the Project menu and select "Add Module". Select
"Module" on the pop up window and hit Open. Now you'll see a coding window. Modules
are like forms, but they don't have any form, they're just code. Modules allow
you to write code that can be called by other forms, just like Public subs. Copy
this code and paste it in your module. Now copy this code into your module.
Sub Changefont(FN As String)
Form1.Text1.Font = FN
End Sub
Notice the "FN As String" in brackets. This defines "FN" as a string, and that
string will receive input from the form using the function when the function
is used. But we'll get to that later. Also, notice how we put "Form1.Text1.Font"
instead of just "Text1.Font". This is because the module is not a part of Form1,
so we have to make it know which Form the textbox it's modifying is on. Now,
go back to your form, and add this to "Private Sub Command1_Click"
Changefont (Tahoma)
Notice the "Tahoma" in brackets. That's the name of the font we're changing to.
You can substitute any font for that, but I like Tahoma, so I used it. What this
does is tells your "Changefont" function the variable FN is. For an exercise,
try writing a function that changes a buttons caption.
Hopefully this tutorial will help you write programs more quickly, and save space.
Uhm... I dunno why I'm typing this, cuz nobody is gonna read it probably, but anyway... I am (guess my age!! haha... 14) years old and I like programming, web design, and graphic design. I'm pursuing a career in game programming. I like basketball, volleyball, skiing, snowboarding, and skateboarding. My favorite pro skater: [b]Jamie Thomas!![/b] Well... I guess that about covers it. Oh yeah... I love to play video games, I'm a Final Fantasy fanatic, I have FF7-9 and FFT, gonna get FF10... I also like the Tony Hawk's Pro Skater series. Gameboy Advance rules! :p
All content in this tutorial was created by the author, Jamie Lindgren, and may not be reproduced without the author's permission. To contact the author, please send an e-mail to wipeoutgc@hotmail.com
Comments
Posted by Chocobo on 29 Oct 2001
Sorry, can't help you there
Posted by Chocobo on 29 Oct 2001
I don't understand what you're saying, could you rephrase that? -
Posted by jponce on 26 Oct 2001
Good Morning.
I have one question regarding a VB project. I relatively new to VB but I do have some years of programming experience. Trying to add the masked box to my project I incorrectly added a r... -
Posted by krishh76 on 26 Oct 2001
hi all
i m new to programming environment and thought VB could be a better language to start with.As most our applications we work for client require some customisation, programming beomes essential....
|
Search
Code Samples
New Members
|