To add to what Patrice has said, if you do not initialize a variable, it is given a default based on it's type. A "basic variable" such as an integer or double is given zero, so
Dim i As Integer
Dim d As Double
Is the same as saying
Dim i As Integer = 0
Dim d As Double = 0.0
But for more complex variables such as strings they aren't - these variables are given class default values, which is a special value called
Nothing
- which contains ... well, nothing. That's actually very important to know, because
Nothing
is not the same as "empty".
An empty string:
Dim s As String = ""
means that the variable contains a real, live, genuine - if empty - string.
Dim s As String
is different - it doesn't contain anything at all,, and if you try to use
s
to call any of the string methods, you will get a "Null reference exception" - which is saying "there is nothing here to call a method on!"
It's a bit like cars. You have a car which you always park in the same space. You go to the space, get in the car, and you can drive to work. The works fine, even if you sell the car and get a new one: it's in the space, so you can drive it. The car is the value (a string); the space is the variable (a container for a Car instance)
But ... if your wife gets up early and takes then the car the space is empty, and when you try to drive to work you have a problem: you can't do anything that you would normally do:
Dim mySpace as Car
mySpace.OpenDoor(Cardoors.Driver)
Will fail (with a Null Reference) when you try to call it because the space is empty. In real life, if you approach your empty car parking space and try to open the door an sit down you are going to get some very strange looks, and you really aren't going to get to work any time soon! The computer world is the same - trying to use a variable containing
Nothing
means the computer gives you strange looks and doesn't work the way you hoped!
As a result, it's a good practice to always set an initial value when you declare a variable, even if it contains a zero or empty value.
Make more sense now?