Solved

Initializing variables

Posted on 2004-09-22
7
1,540 Views
Last Modified: 2012-06-27
Is it necessary to initialize variables or is it OK to let the compiler do it?  I know in languages like C++ it's absolutely necessary to initialize variables, but I'm not sure about VB6.

Please explain answer.

Thanks.
0
Comment
Question by:halfondj
7 Comments
 
LVL 67

Assisted Solution

by:sirbounty
sirbounty earned 50 total points
ID: 12124159
If you don't declare a variable, it's declared as variant which takes up more resources.
http://juicystudio.com/tutorial/vb/variables.asp gives a great explanation.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12124218
Also, even languages like VB6 or ASP or PHP allow you not to declare variables, it is a good practice because it contributes to readability:

Dim var1 as string     ' Explain here purpose of this variable
Dim var2 as integer   ' Explain here purpose of this variable
Dim var3 as double    ' Explain here purpose of this variable
0
 
LVL 32

Accepted Solution

by:
Erick37 earned 200 total points
ID: 12124262
When you Dim a variable, VB is kind enough to initialize it for you.

Numbers are set to 0
Strings are empty
Objects are Nothing

If you have Form level variables or even Static variables inside form procedures, they can retain values even after you unload the Form.  If you need to have them reset, either do it via code in Form_Load, or be sure you Set FormX = Nothing before you reload it.  That will force the form completely out of memory, and the variables will reinitialize when the form loads again.

Hope it helps!
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 32

Assisted Solution

by:Erick37
Erick37 earned 200 total points
ID: 12124324
An Example

Private sTest as String  'Form level variable initializes to "" on Form_Load

Sub Command1_Click()

    Dim i as Long   ' Dimmed variable is reste to 0 each time this Sub is entered
    Static lx as Long ' Static variables initialize to 0, but retain values.

    Debug.Print i, lx

    lx = lx + 1

End Sub
0
 
LVL 12

Expert Comment

by:fulscher
ID: 12124381
Two comments in addition to those of sirbounty:

Example:
    Dim ikl as Long
    ikI = test(a, b)   'note: Uppercase i instead of lowercase L
    Debug.Print ikl    'always prints 0

This kind of bugs is rather difficult to find. Declaring variables helps you to avoid this; if you had Option Explicit turned on, the compiler would have complained about your using an undeclared variable "ikI".

Also, by declaring them to have a specific data type, you can avoid certain types of errors where you pass the wrong datatype to a function, for example. This is a problem if a function can handle one option but not the other:

    Dim v as Variant
    if Day(Now) = 1 then
        v = "seven"
    else
        v = 7
    end if

    Debug.Print v / 7   ' Crashes ONLY on the first day of a month. Nasty.

If you had declared v as numeric type, this error would have been be possible.

So, by declaring variables, you remove amiguity from your code - which is always a good thing to do...

0
 
LVL 12

Expert Comment

by:fulscher
ID: 12124456
Ups - sorry: Just re-read the question.

"Is it necessary to initialize variables"

You CAN in C/C++, although it's not necessary, but good style. Helps avoiding strange errors.
You CANNOT in VB6, unfortunately, if I'm not mistaken. Initialising a variable is especially useful if it's a static variable.

Compare these two bits of code:

Sub DoIt1
   Static myStr as String ' must contain "hello" in the first call
   
   if myStr = "" Then
       myStr = "hello"
   end if

   Debug.Print myStr
   myStr = myStr & "!"
End Sub

Sub DoIt2
   Static myStr = "hello"
   Debug.Print myStr
   myStr = myStr & "!"
End Sub

So, initializing variables certainly has its advantages, but is not strictly necessary.

Jan
0
 

Author Comment

by:halfondj
ID: 12124533
Thanks for all the replies.  I knew about declaring variables and I also knew it's good practice to initialize them, but I wanted to hear from the experts re:the initialization.

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now