Avis Junior Poster

Joined: 07 Oct 2003 Posts: 510 Location: India
|
Posted: Oct 16th, 2003 01:00 AM Post subject: Tips for Visual Basic Begineers |
|
|
What are some tips that every VB programmer should know ?
You need to use Option Explicit in your programs. These keywords, placed at the very top of the (General)(Declarations) section of your code, forces you to explicitly dimension every variable name and type (note that if you do NOT specify a type, it will be a Variant). So instead of typing in a new variable name, you will first have to Dim varname As type. Run VB and select Standard EXE. Then select the menu options Tools then Options. On the Editor tab, check the Require Variable Declarations tab. This will automatically put Option Explicit at the top of every code window. It will save you many headaches and crashes in the future, especially when you start using API functions.
Sharing variables - you can share variables four different ways. The first way isn't really sharing - just maintaining the value of a variable using the Static keyword. This is needed to prevent re-entry into an event. For example, if you change the value of a TextBox from within the Change event, the Change event will fire again. You therefore need some code to tell yourself that you are already in the Change event, so don't do anything. Static works like Dim except that the variable is only created once and maintains is value after the procedure exits.
Private Sub Text1_Change()
Static bInHere As Boolean
If bInHere Then Exit Sub
bInHere = True
' do some stuff that changes Text1.Text
bInHere = False
End Sub
The second type of share is the object level share. That means that the variables can be used by any procedure in the same object (Form, Module, Class, UserControl), but not by any other object. Just define the variable in the (General)(Declarations) section of the module (that means that (General) appears in the left ComboBox of the code window and (Declarations) appears in the right ComboBox.)
Option Explicit
Dim aVar As String
Private Sub Form_Load()
aVar = "Anything"
End Sub
Private Sub Form_Resize()
aVar = "Something else"
End Sub
The third type of share is what I call the Form Property Share. This type of share applies to Forms, Classes, and UserControls and makes a variable act like a property of the form that can be referenced from outside the form.
' Form1
Option Explicit
Public aVar As String
Private Sub Form_Load()
aVar = "Something"
End Sub
' Form2
Private Sub Form_Load()
MsgBox Form1.aVar
End Sub
The fourth type of shared variable is the most useful - the Application Share. This type of share can only be created in a module (a .Bas file). The application share variable can be used in any procedure in any object in the project.
' Module1.bas
Public aVar As String
Private Sub DisplayString()
MsgBox aVar
End Sub
' Form1
Private Sub Form_Load()
MsgBox aVar
End Sub
Visual Basic is an event driven language. Various events can be fired by setting the properties of an object. For example, Text1.Text = "Something" will fire the Text1_Change() event. Combo1.ListIndex = 1 will fire the Combo1_Click() event. If something appears to be happening when it shouldn't, then the most likely problem is that a property is being changed and is firing an event. _________________ Code Snippets, Tutorials, Utilities, Controls
Low cost Web Hosting
Hosting starts at as low as $4 per year!
Always follow posting guidelines
Put your VB code in [vb ] your code [ /vb] tags! |
|