Import Statement

HardCode

Freshman
Joined
Apr 2, 2004
Messages
49
Why do you need to put both of these in an ASP.NET page:

Code:
<%@ import Namespace="System.Data.SqlClient" %>
<%@ import Namespace="System.Data" %>

Doesn't the <%@ import Namespace="System.Data" %> statement also include objects in System.Data.SqlClient?
 
You are required to import the namespaces explicitly, this prevents confusion when the same classname occurs in multiple sub namespaces and compilation errors if in future a revised library version may introduce a duplicate classname in a sub namespace.
 
So, if there is a class Person in System.People.Person, and a class Person in MyLib.People.Person, then I can do this:
Code:
<%@ import Namespace="MyLib.People.Person" %>
<%@ import Namespace="System.People" %>
... and I will be sure, when instantiating class Person, to get Person from Mylib.People and not System.People? .NET will know that MyLib.People.Person is the most explicit so it will use that?
 
You can't import down to the class level, only the namespace so the 1st line would be invalid. If you have duplicate definitions you could alias a namespace to a shorter version and use that to fully qualify classnames

Code:
<%@ import Namespace="MyPeople = MyLib.People" %>
<%@ import Namespace="System.People" %>

'you could now use
MyPeople.Person
'and can always use
MyLib.People.Person
 
Back
Top