Variable Declare Error issue VB 2005

I am having what I think might be a common problem.

When upgrading my applications to VB 2005 from VB 2003, I have received a number of warnings regarding my declarations of local Strings without a default value assigned initially.

Example of My earlier code:

Dim tmpDriveType as Strin

Warning treated as error : Variable 'tmpDriveType' is used before it has been assigned a value. A null reference exception could result at runtime.

This is an easy fix by simply declaring the local variable as follows:

Dim tmpDriveType as String = ""

Not a problem, problem solved and understood.

However, I have several code declares as follows:

Example # 1

Dim AllProcess As Process()

AllProcess = Process.GetProcesses

Return AllProcess

Example # 2

Dim AllProcess As Process()

AllProcess = Process.GetProcessesByName(strProcessName)

'return array of all process properties for each instance running

Return AllProcess

Example # 3

Dim logDirectoryProperties As System.IO.DirectoryInfo

If My.Computer.FileSystem.DirectoryExists("C:\backup\logs") Then

logDirectoryProperties = My.Computer.FileSystem.GetDirectoryInfo("C:\backup\logs")

End If

MsgBox(logDirectoryProperties.Exists.ToString)

With these examples I am currently battling, I am getting the same error and I have no clue as to what to default these variable to in order to eliminate the errors.

Any help of direction would be much appreciated.



Answer this question

Variable Declare Error issue VB 2005

  • AndrewMcK

    Thanks, this fixed this problem.

    For some reason I was looking or thinking of some default specific variable type on this and overlooked the most obvious Nothing.

    Dim tmp Do I feel like an idiot = Yes


  • PoonamTS

    Typically your code is more maintainable when you are explicit in your object instantiation, particularly on Reference types (objects, strings, etc). Value types will have default values where VS will complain if you try to explicitly instantiate them to the default (ex dim foo as boolean = false).

    I wanted to take each of your examples and tweak them a bit.

    For Example 1, you don't need the AllProcess local variable. You could just do the following:

    Return Process.GetProcesses

    Example 2 you can shorten as well to

    Return Process.GetProcessByName(strProcessName)

    Example 3 is a bit trickier. The challenge here is on your last line (MsgBox). In your case, if the directory does not exist, you are asking the message box to display the exists property on an object that was never instantiated. The instantiation happens within the If block but the msgbox is outside of the block. You need to put the MsgBox line inside the If block. Additionally, you may want to add an else clause to notify the user that the directory does not exist, or to create it if necessary.

    Jim Wooley
    http://devauthority.com/blogs/jwooley



  • Yash.cse

    In cases where no meaningful initialization is possible you should be able to initialize to Nothing and the warning should go away. The warning is just making sure that Nothing is what you really intended.
  • Variable Declare Error issue VB 2005