Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Initializing variables

Posted on 2004-09-22
7
Medium Priority
?
1,549 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 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…
Suggested Courses

604 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