Dim nameFile As System.IO.StreamReader nameFile = New System.IO.StreamReader("a:/friends.txt")Note that the class name StreamReader is preceded by "System.IO."
StreamReader is one of thousands classes that are included with VB.NET, but not included in the VB toolbox. Since there are so many of these classes, they are subdivided into groups, called namespaces. StreamReader is in the System.IO namespace.
If you plan to use several classes from a given namespace in your program, you can save typing and make your program more readable by importing the namespace into the program. If, for example, you wished to import the System.IO namespace into your program, you would place the line:
Imports System.IOat the start of the program, just after the option strict on statement.
Had I done that in the file reading example, I could have simplified the object declaration and construction as follows:
Dim nameFile As StreamReader nameFile = New StreamReader("a:/friends.txt")The compiler would have known that StreamReader was included in the System.IO namespace.
We can also import appropriate namespaces to simplify references to shared properties and methods. For example, we have used shared properties of the Color class and shared methods of the Math class, writing statements like:
lblGreeting.ForeColor = Color.Blue y = Math.Sqrt(z)
But, if we had begun our program with
Imports System.Drawing.Color Imports System.Math
we could have simply written:
lblGreeting.ForeColor = Blue y = Sqrt(z)
The development system automatically imports the most common namespaces into your program, but if you wish to use classes from other namespaces, you can import those on your own. If you do not, you will have to use the fully qualified class names.