using a namespace in another namespace?

Is it possible to "use" a namespace in another namespace.  Let's assume that I have Namespace1 (which has Class1 and Function1) and Namespace2 (which has Class2 and Function2).  Now, I have both of these in seperate .cs files and I've compiled them by hand using the Command Prompt (I'm not using VS.NET).  So, now I want to rewrite Namespace1 so that it "uses" Namespace2 (while still keeping it in a different .cs file)  This is what I would want Namespace1 to look like..


namespace1.cs file
namespace Namespace1
{
// when I compile the file with this using statement it gives me an error that it's unknown name
using Namespace2; // how would I do this?
public class Class1
{
public void Function1 ()
{
// but how do I get Namespace1 to "use" or recognize this?
Namespace2 objNamespace2 = new Namespace2 ();
objNamespace2.Function2 ();
}
}
}
0
synthetikk242
1/7/2004 8:32:08 AM
asp.net.getting-started 91979 articles. 4 followers. Follow

4 Replies
865 Views

Similar Articles

[PageSpeed] 48

Add following line on the top:


using Namespace2;
0
almir_1
1/7/2004 9:18:41 AM
Sorry didn.t look properly into your code

Basically there are wo ways to teference classes belonging to other Namespace:
1. Import the Namespace and use classess from imported namespace as you would use any class in your code :

using Namespace2;
namespace Namespace1
{
using Namespace2;// when I compile the file with this using statement it gives me an error that it's unknown name
// how would I do this?

public class Class1
{
public void Function1 ()
{
// but how do I get Namespace1 to "use" or recognize this?
objFromNamespace2 oObject= new objFromNamespace2();
oObject.Function2 ();

}
}
}

2.Use fully qualified reference


namespace Namespace1
{
using Namespace2;// when I compile the file with this using statement it gives me an error that it's unknown name
// how would I do this?

public class Class1
{
public void Function1 ()
{
// but how do I get Namespace1 to "use" or recognize this?
Namespace2.objFromNamespace2 oObject = new Namespace2.objFromNamespace2();
oObject.Function2 ();

}
}
}

0
almir_1
1/7/2004 9:31:38 AM

Firstly, it is quite legal to include your using statement inside another Namespace, however the ASP.NET compiler needs to know the location of it's dll (for Namespace2), so that it can check that you are using the correct Classes, Properties, Methods and so on. If you are compiling the file via the command line you include /r:Namespace2.dll (or whatever the name of the .dll is).
Secondly, a Namespace is a container for Classes, Structs, etc. and is not instantiated as an object. You would instantiate a Class that is contained inside the Namespace like so:
Namespace2.Object myObject = new Namespace2.Object() //C#

Dim myObject As Namespace2.Object = New Namespace2.Object() 'VB (I think!!)

Steven Bey

Recursion: see Recursion
0
stevenbey
1/7/2004 10:44:29 AM
Thanks Steven for clarifying the matter.

One observation for Synthetikk242 - you need to reference dll from command line only if your Namespace2 is compiled previously in a separate component.
If you compile your code in a single component there is no need to add any reference while compiling from command line.

0
almir_1
1/7/2004 11:13:25 AM
Reply:

Similar Artilces:

Trying to create .net assembly
I'm just migrated a pfc based application from 10.5 to 12.1. The main reason for the migration was to reuse code in a .net application. To that end I've attempted to create a .net assembly. After adding the .net assembly target using the existing app I then tried to deply it but get the following error: Error when compiling generated C# code: a_madcst(4,16): error CS0101: The namespace '<global namespace>' already contains a definition for 'c__a_madcst' Any help you can give me would be much appreciated. Thank You , Mike Mike, Please cla...

Adding a new namespace to an existing namespace in VS.net
Hi, I'm a bit new to Visual Studio. In my example, I already have a class library built - named Vision.PageTracker. However, I'd like to add a new namespace as a member of "Vision.PageTracker" called "Vision.PageTracker.Ads" (however I don't want to add a new project; just a new folder with some classes in it that are separate from the base NameSpace). For some reason I'm finding this difficult. Thanks for any help! Bryan Hey, Sure, if your project is Vision, and you declare a PageTracker namespace, for the other classes in the new namesp...

Why can't global resource namespaces be used in an import namespace statement?
Custom namespaces for global resources within the Resources namespace is a completely undocumented but nice new feature: "~/App_GlobalResources/TransparentFolder/XXX.YYY.ZZZ.ResourceName.resx." This is an invariant file that gets code-generated into a public class ResourceName inside the namespace Resources.XXX.YYY.ZZZstring s= Resources.XXX.YYY.ZZZ.ResourceName.ResKey1; Before RTM I had a thousand entries which all appeared at once in the intellisense popup and were hard to navigate. Now I can break them up into different resx files and incorporate the namin...

Cannot build. designer.cs classes get errors when classes have Namespace.ChildNamespace.Namespace.ChildChildNamespace pattern
Hello,We are experiencing a problem building that gets thrown in the designer.cs design behind files throw: "The type of namespace WebAppp does not exist in the namesapce Console.WebApp.Console (are you missing an assembly reference?)"        protected Console.WebApp.Console.PrintButton ctlPrint; This is apparently because the root level namescape is Console, and there is a folder within the WebApp named Console, so the compiler cannot resolve the top path from the top for some reason. I guess it's probably just bad practice to give ...

Reference Namespace and code in App_code from another .Net Project?
I couldn't figure out how to add a reference to a web project. There is no dll until I build the website. Is this how it should be? I have 2 projects in a solution file. One is web project other is a class library. I can't get the class library to have reference to namespace and classes in the web project. Have you considered to create a web service so that in the class library you can add web reference to it?Welcome to my SQL/ASPNET forum for Chinese http://51up.org/bbs/forumdisplay.php?fid=38...

using namespace
HiI have an web app solution ABC.Now my solution ABC contains a website and an another c# project class library as DbManager which contains my db methods.My dbmanager is under namespace Manager.Now in my website i want to use my dbmethods that i have in my DbManager.When I add the namespace at the top as: using DbManger;the intellisense finds and lets me add it (i even added the reference to that project in my bin of my website)but when I run my application i get the error:Error    1    The type or namespace name 'DbManager' could not be found (are you m...

CS0246:The type or namespace name <type/namespace> could not be found (are you missing a using directive or an assembly reference?)
iam new to asp.net.i have created a class (invoice.cs), in the App_Code folder. Also a .dll file in the bin directory. i have used classes from both the dll and the class file to my .aspx and .aspx.cs pages. In the local server (using IDE), it's working. But in the case when i upload those files to the website, iam getting a error - CS0246 The type or namespace name <type/namespace> could not be found (are you missing a using directive or an assembly reference?). What's the problem. Please help. I read somewhere that it's something related to virtual directory.Is that corre...

specify DNS server to use w system.net namespace
Is it possible to specify the DNS server to use before calling dns.resolve(sIPAddress)? Thanks! sal...

How to get the NameSpace
HI All I am building webservice.as per the sources im able to create MyApp.dll and MyAppFile.cs , after compilation, i compile as csc CSC /t:library /r:system.web.dll /r:system.xml.dll MyAppFile.cs Its created .dll file too.when i call Ex :  System.Web.MyNameSpace;Its showing error like " The type or namespace name 'MyNameSp' could not be found (are you missing a using directive or an assembly reference?) "what i have to do, can explaing the steps to compile and get the name space support.how can i call that namespace in aspx page ! Thanks in Advanceshiva H...

using namespace
Is there anyway I can do a using namespace for a particular loop like in c++; ( for functions not exported ) FOr eg in a perl script use Some::Module ..... { using namespace Some::Module; # Is this possible ? someFuntion1(); # Instead of Some::Module::someFunction1() someFunction2(); # Instead of Some::Module::someFunction2() } ---------------------------------------- Thanks Ram see perldoc -f import perldoc -f use and what happens when you use Some::Module (qw/somefunction1 somefunction2/) ; Manav |-----Original M...

using namespace
Now that we have unified builds, writing "using namespace" in the global scope of a .cpp file is almost as bad as writing it in a header. Regularly build errors show up like this one: https://tbpl.mozilla.org/php/getParsedLog.php?id=40010766&tree=Try What's awesome about these errors is that they typically show up on some platform but not the others and they come and go as we add files or do things that can alter the generation of the unified .cpp files. So, I strongly encourage everyone to stop using "using namespace". If you really really want to use t...

namespaces and using
Hi, When I create a class in a library I wrap everything in a namespace, like this in a header: namespace myLib { class someclass etc..... } If my custom class are using, say the list class from std, will there be any difference in writing {code} #include <list> using std::list; namespace myLib { class someclass using std::list ... } {/code} from putting the "using list"inside the namespace, i.e. {code} #include <list> namespace myLib { using std::list; class someclass using std::list ... } {/code} -totte With using std::list ...

how to use your own namespace
I have created a namespace "DataControls" in which I have a class "DataCalender" But while I debug it ,shows an error "Could not load file or assembly 'DataCalender' or one of its dependencies. The system cannot find the file specified" in the second line where I have written "<%@ Register Namespace="DataControls" Assembly="DataCalender" TagPrefix="dc" %>" Is your class in DataCalendar assembly (DLL)? I think it's just a name of the class, but assembly name is different.My last project - .NET Tip of ...

superreview granted: [Bug 248172] XMLSerializer corrupts namespace prefixes when XHTML namespace is used : [Attachment 151528] v1
Johnny Stenback <jst@mozilla.jstenback.com> has granted Johnny Stenback <jst@mozilla.jstenback.com>'s request for superreview: Bug 248172: XMLSerializer corrupts namespace prefixes when XHTML namespace is used http://bugzilla.mozilla.org/show_bug.cgi?id=248172 Attachment 151528: v1 http://bugzilla.mozilla.org/attachment.cgi?id=151528&action=edit ------- Additional Comments from Johnny Stenback <jst@mozilla.jstenback.com> sr=jst ...

Web resources about - using a namespace in another namespace? - asp.net.getting-started

Namespace - Wikipedia, the free encyclopedia
Namespaces provide a level of indirection to specific identifiers, thus making it possible to distinguish between identifiers with the same exact ...

Wikipedia:Help namespace - Wikipedia, the free encyclopedia
The Help namespace is a namespace consisting of Wikipedia pages whose titles begin with the prefix Help: , such as Help:Link . These pages contain ...

ICANN seeks to tackle DNS namespace collision risks
As the number of top-level domains undergoes explosive growth, the Internet Corporation for Assigned Names and Numbers (ICANN) is studying ways ...

OS X Lion: Guidelines for connecting to a DFS namespace via SMB
Ensure that the following guidelines are followed when connecting to a DFS namespace via SMB in OS X Lion.

Facebook's Namespace Land Grab? Or Maybe…It's Just Useful
Much buzz over the past few days about Facebook’s plans to let folks (and, ahem, brands) claim their namespaces on Facebook. IE, Starting this ...

Global Namespace for People (Part 3)
I ended yesterday’s post by saying that I would write about potential solutions to the global namespace problem for people (parts 1 and 2). So ...

Netflix namespace problem · Issue #34 · balloob/pychromecast · GitHub
pychromecast - Library for Python 2 and 3 to communicate with the Google Chromecast.

Adding Namespaces To Ruby
... about how close I can get Ruby to look like Clojure in size and style. Recently, what has bothered me most has been how you define a namespace ...

PHP 5.6 features built-in debugger, better namespace support
PHP, the venerable scripting language for server-side Web development, has been upgraded in a point release highlighted by a new debugger.

#include<bits/stdc++.h> using namespace std; int n, x, y; int main() { fo - Pastebin.com
PASTEBIN - #1 paste tool since 2002 create new paste tools api archive faq PASTEBIN create new paste trending pastes sign up login my alerts ...

Resources last updated: 12/26/2015 4:52:56 AM