Programmerare, skeptiker, sekulärhumanist, antirasist.
Författare till bok om C64 och senbliven lantis.
Röstar pirat.
2011-05-07
In C# you can use the using directive to import the classes in a given namespace to your current scope. If you want to use the classes in System.Data.SqlClient, you could refer to them using their full path, like so:
var r = new System.Data.SqlClient.SqlDataAdapter();
You can add namespaces to your scope by adding a using directive in the beginning of your source file. If you add this directive:
using System.Data.SqlClient;
You could then access the SqlDataAdapter class in your code, without typing the full path, like so:
var r = new SqlDataAdapter();
Visual Basic does not have the using directive, but it has a more powerful directive called Imports. Imports can import both classes and other namespaces. The code that creates a SqlDataAdapter in Visual Basic looks like this:
Dim R As New System.Data.SqlClient.SqlDataAdapter()
If you use the Imports directive to import System.Data in the beginning of your file, like so:
Imports System.Data
You can access both the namespaces (like SqlClient) and the classes in System.Data without providing the full path.
Dim R As New SqlClient.SqlDataAdapter()
If course, you can go on and import the SqlClient namespace to access the SqlDataAdapter class without specifying it’s full path, just as you can do in C#. So, the using directive in C# can import classes, but the Imports directive in VB imports both classes and namespaces.
Categories: VB.NET
Bjud mig på en kopp kaffe (20:-) som tack för bra innehåll!
Leave a Reply