Simple and short, this is how you can initialize a variable in VB.NET.
Take the following for example:
Dim dirInfo As System.IO.DirectoryInfo = New System.IO.DirectoryInfo("C:\")
The first part:
Dim dirInfo As System.IO.DirectoryInfo
as I would like to call it, “dreams” about a variable dirInfo which belong to a type of System.IO.DirectoryInfo.
The second part:
New System.IO.DirectoryInfo("C:\")
actually realizes your dream, it “creates” that variable. It’s only now that your variable dirInfo trully exist.
You can also initialize a variable like so:
Dim dirInfo As System.IO.DirectoryInfo
dirInfo = New System.IO.DirectoryInfo("C:\")
One of the error message that you may get if you haven’t been initializing a variable correctly is “Object reference not set to an instance of an object”. The following code can cause an error, since the variable dirInfo is still a “dream”.
Dim dirInfo As System.IO.DirectoryInfo
dirInfo.GetDirectory("C:\")
As in life, good things starts with a dream, and you have to do something to it, before it can be realized.
Leave a Reply