Solved

Initializing variables

Posted on 2004-09-22
7
1,544 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…

829 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