?
Solved

Initializing variables

Posted on 2004-09-22
7
Medium Priority
?
1,548 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 67

Assisted Solution

by:sirbounty
sirbounty earned 200 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 800 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 32

Assisted Solution

by:Erick37
Erick37 earned 800 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month7 days, 23 hours left to enroll

765 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