Importing namespaces in a class

Discussion in 'ASP .Net' started by Ralph Wiggum, Jan 23, 2008.

  1. Ralph Wiggum

    Ralph Wiggum Guest

    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...
    Ralph Wiggum, Jan 23, 2008
    #1
    1. Advertising

  2. Ralph Wiggum

    David Wier Guest

    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


    "Ralph Wiggum" <> wrote in message
    news:...
    >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...
    David Wier, Jan 23, 2008
    #2
    1. Advertising

  3. Ralph Wiggum

    bruce barker Guest

    using System.Collections.Specialized;

    -- bruce (sqlwork.com)


    "Ralph Wiggum" wrote:

    > 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...
    >
    bruce barker, Jan 23, 2008
    #3
  4. Ralph Wiggum

    Ralph Wiggum Guest

    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?


    David Wier wrote:
    > 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
    >
    >
    > "Ralph Wiggum" <> wrote in message
    > news:...
    >> 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...

    >
    >
    Ralph Wiggum, Jan 23, 2008
    #4
  5. MyClass should have a reference to MyOtherClass, and a using statement at the
    top of its class file in order to use a namespace defined in MyOtherClass.cs
    .. You don't need it at the @Page level at all.
    -- Peter
    Site: http://www.eggheadcafe.com
    UnBlog: http://petesbloggerama.blogspot.com
    MetaFinder: http://www.blogmetafinder.com


    "Ralph Wiggum" wrote:

    > 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?
    >
    >
    > David Wier wrote:
    > > 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
    > >
    > >
    > > "Ralph Wiggum" <> wrote in message
    > > news:...
    > >> 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...

    > >
    > >

    >
    Peter Bromberg [C# MVP], Jan 23, 2008
    #5
  6. Ralph Wiggum

    Ralph Wiggum Guest

    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{

    }
    }



    Peter Bromberg [C# MVP] wrote:
    > MyClass should have a reference to MyOtherClass, and a using statement at the
    > top of its class file in order to use a namespace defined in MyOtherClass.cs
    > . You don't need it at the @Page level at all.
    > -- Peter
    > Site: http://www.eggheadcafe.com
    > UnBlog: http://petesbloggerama.blogspot.com
    > MetaFinder: http://www.blogmetafinder.com
    >
    >
    > "Ralph Wiggum" wrote:
    >
    >> 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?
    >>
    >>
    >> David Wier wrote:
    >>> 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
    >>>
    >>>
    >>> "Ralph Wiggum" <> wrote in message
    >>> news:...
    >>>> 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...
    >>>
    Ralph Wiggum, Jan 23, 2008
    #6
  7. Ralph Wiggum

    David Wier Guest

    Put that at the very top of the page, before the Class declaration

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


    "Ralph Wiggum" <> wrote in message
    news:...
    > 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{
    >
    > }
    > }
    >
    >
    >
    > Peter Bromberg [C# MVP] wrote:
    >> MyClass should have a reference to MyOtherClass, and a using statement at
    >> the top of its class file in order to use a namespace defined in
    >> MyOtherClass.cs . You don't need it at the @Page level at all.
    >> -- Peter
    >> Site: http://www.eggheadcafe.com
    >> UnBlog: http://petesbloggerama.blogspot.com
    >> MetaFinder: http://www.blogmetafinder.com "Ralph Wiggum" wrote:
    >>
    >>> 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?
    >>>
    >>> David Wier wrote:
    >>>> 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
    >>>>
    >>>>
    >>>> "Ralph Wiggum" <> wrote in message
    >>>> news:...
    >>>>> 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...
    >>>>
    David Wier, Jan 23, 2008
    #7
  8. Ralph Wiggum

    Ralph Wiggum Guest

    I'm probably a little slow, but "that" is...?

    David Wier wrote:
    > Put that at the very top of the page, before the Class declaration
    >
    > David Wier
    > http://aspnet101.com
    > http://iWritePro.com - One click PDF, convert .doc/.rtf/.txt to HTML with no
    > bloated markup
    >
    >
    > "Ralph Wiggum" <> wrote in message
    > news:...
    >> 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{
    >>
    >> }
    >> }
    >>
    >>
    >>
    >> Peter Bromberg [C# MVP] wrote:
    >>> MyClass should have a reference to MyOtherClass, and a using statement at
    >>> the top of its class file in order to use a namespace defined in
    >>> MyOtherClass.cs . You don't need it at the @Page level at all.
    >>> -- Peter
    >>> Site: http://www.eggheadcafe.com
    >>> UnBlog: http://petesbloggerama.blogspot.com
    >>> MetaFinder: http://www.blogmetafinder.com "Ralph Wiggum" wrote:
    >>>
    >>>> 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?
    >>>>
    >>>> David Wier wrote:
    >>>>> 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
    >>>>>
    >>>>>
    >>>>> "Ralph Wiggum" <> wrote in message
    >>>>> news:...
    >>>>>> 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...

    >
    >
    Ralph Wiggum, Jan 23, 2008
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ravi sankar
    Replies:
    2
    Views:
    377
    abdul bari
    Aug 27, 2003
  2. D. Shane Fowlkes
    Replies:
    1
    Views:
    923
    Tu-Thach
    Jan 13, 2004
  3. plb
    Replies:
    2
    Views:
    339
  4. Harlin Seritt
    Replies:
    10
    Views:
    472
    Harlin Seritt
    Mar 5, 2005
  5. Donnal Walter
    Replies:
    1
    Views:
    276
    Peter Hansen
    Aug 22, 2005
Loading...

Share This Page