Importing namespaces in a class

R

Ralph Wiggum

I have an aspx that looks like this:

<%@ Import Namespace="System.Collections.Specialized" %>
<%@ Assembly Src="MyClass.cs" %>
....

I'm only using the imported namespace in MyClass.cs, not in the aspx itself. Is it possible to do the import in MyClass.cs, instead of the aspx? I can't figure out the syntax...
 
D

David Wier

It's just the basic:
Imports System.Collections.Specialized

And yes, import it only in the class - it will automatically get referenced
in the page, when you instantiate the class.

David Wier
http://aspnet101.com
http://iWritePro.com - One click PDF, convert .doc/.rtf/.txt to HTML with no
bloated markup
 
R

Ralph Wiggum

Ok, thanks. But what if the namespace is defined in a separate file, MyOtherClass.cs and I need to use it in MyClass.cs:

<%@ Import Namespace="NamespaceInMyOtherClass" %>
<%@ Assembly Src="MyClass.cs" %>
<%@ Assembly Src="MyOtherClass.cs" %>
....

How can I tell MyClass.cs to import from MyOtherClass.cs when I remove the first line above?
 
R

Ralph Wiggum

I'm pretty sure the compiler will complain about "using NamespaceInMyOtherClass;" in the code below. How can I tell MyClass to look in the MyOtherClass.cs file?

aspx:
<%@ Assembly Src="MyClass.cs" %>
<%@ Import Namespace="MyClass" %>
<%
MyClass mc = new MyClass();
%>

MyClass.cs:
namespace MyClass{
public class MyClass{
using NamespaceInMyOtherClass; <-- Problem!
public MyClass(){
MyOtherClass moc = new MyOtherClass();
}
}
}

MyOtherClass.cs:
namespace NamespaceInMyOtherClass{
public class MyOtherClass{

}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top