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.

You might also like...

Comments

Jamie Lindgren 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 pursui...

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook