Solved

Initializing variables

Posted on 2004-09-22
7
1,541 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

929 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

13 Experts available now in Live!

Get 1:1 Help Now