Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Questions on Creating/Including a namespace

Status
Not open for further replies.

dpanattoni

IS-IT--Management
Jan 29, 2002
76
US
I want to create a library of commonly called routines and classes in C#. Is this commonly done through the use of namespace?

If so, what are the steps?
Create a file with a namespace included. What type of file is this, a .cs or some other type?
From the program, how do I include the newly created namespace?

Thanks.
 
The namespace is declared before every class of the library like this:

namespace mynamespace
{
class XXX
....
}

The file should have the .cs extension, but the project should compile to a library (dll).

If you have Visual Studio .NET, I advise you to use the wizard for creating a library. It will do much of the organizational work for you.

After it is compiled, you use it like this: you add a reference in the project that uses the library to the library (go in the solution explorer to references, click right, add reference and choose the library file). Then use the using directive to import the namespace.
 
Microsoft suggests using "company.technology" for choosing a namespace. You could extend this a little bit if you like: "company.technology.utility" but don't get carried away.

Chip H.
 
Thanks. I am not using any IDE at this time. How do I do this without VS.net?

I have a .cs file that has my namespace defined in it. I also have another application (.cs) that wants to include the namespace defined in the first .cs. Because the first .cs will be a commonly used routine, I'm sure I will have other applications that will need it as well.

Is this too difficult or not possible from the command line?

Thanks again for your responses.
DP
 
I haven't used the command line compiler, but running csc /? gives this:

Code:
C:\Documents and Settings\chiph\Desktop>csc /?
Microsoft (R) Visual C# .NET Compiler version 7.00.9466
for Microsoft (R) .NET Framework version 1.0.3705
Copyright (C) Microsoft Corporation 2001. All rights reserved.

                      Visual C# .NET Compiler Options

                        - OUTPUT FILES -
/out:<file>             Output file name (default: base name of file with main
                        class or first file)
/target:exe             Build a console executable (default) (Short form:
                        /t:exe)
/target:winexe          Build a Windows executable (Short form: /t:winexe)
/target:library         Build a library (Short form: /t:library)
/target:module          Build a module that can be added to another assembly
                        (Short form: /t:module)
/define:<symbol list>   Define conditional compilation symbol(s) (Short form:
                        /d)
/doc:<file>             XML Documentation file to generate

                        - INPUT FILES -
/recurse:<wildcard>     Include all files in the current directory and
                        subdirectories according to the wildcard specifications
/reference:<file list>  Reference metadata from the specified assembly files
                        (Short form: /r)
/addmodule:<file list>  Link the specified modules into this assembly

                        - RESOURCES -
/win32res:<file>        Specifies Win32 resource file (.res)
/win32icon:<file>       Use this icon for the output
/resource:<resinfo>     Embeds the specified resource (Short form: /res)
/linkresource:<resinfo> Links the specified resource to this assembly (Short
                        form: /linkres)

                        - CODE GENERATION -
/debug[+|-]             Emit debugging information
/debug:{full|pdbonly}   Specify debugging type ('full' is default, and enables
                        attaching a debugger to a running program)
/optimize[+|-]          Enable optimizations (Short form: /o)
/incremental[+|-]       Enable incremental compilation (Short form: /incr)

                        - ERRORS AND WARNINGS -
/warnaserror[+|-]       Treat warnings as errors
/warn:<n>               Set warning level (0-4) (Short form: /w)
/nowarn:<warning list>  Disable specific warning messages

                        - LANGUAGE -
/checked[+|-]           Generate overflow checks
/unsafe[+|-]            Allow 'unsafe' code

                        - MISCELLANEOUS -
@<file>                 Read response file for more options
/help                   Display this usage message (Short form: /?)
/nologo                 Suppress compiler copyright message
/noconfig               Do not auto include CSC.RSP file

                        - ADVANCED -
/baseaddress:<address>  Base address for the library to be built
/bugreport:<file>       Create a 'Bug Report' file
/codepage:<n>           Specifies the codepage to use when opening source files
/utf8output             Output compiler messages in UTF-8 encoding
/main:<type>            Specifies the type that contains the entry point (ignore
                        all other possible entry points) (Short form: /m)
/fullpaths              Compiler generates fully qualified paths
/filealign:<n>          Specify the alignment used for output file sections
/nostdlib[+|-]          Do not reference standard library (mscorlib.dll)
/lib:<file list>        Specify additional directories to search in for
                        references

So it would appear that you would use the /reference (or /r) option to reference your other module, and then in your .cs code file you'd use a &quot;using xxx.xxx&quot; statement at the top to be able to use the objects in the other namespace without having to give the full prefix each time.

Or, just reference the other assembly, and I think you can give the full namespace path each time you use an object from the other library: &quot;othernamespace.otherobject.property&quot;

Chip H.
 
I'm confused.
Over the past week, I think I've tried every combination of the csc that I could think of. I can compile the .cs file that has the namespace defined, but I cannot run or compile the .cs file that is trying to reference the newly defined namespace. The compiler says that the namespace doesn't exist.

I'm not sure why this is so hard, other than I guess I'm not using Visual Studio.

I just want to reuse my own namespace in more than one program without having to type it in every time. Doesn't anyone write C# like C++ or C where code is reused, or are they all using VS.net to do this?
 
I got it working, but its now working with a method that I thought for sure I was trying to begin with.

For the .cs file with the newly described namespace:
csc /t:library /out:MyCompany.Utility.dll utility.cs

I then put the created .dll file into the application's bin directory, and I can then reference the classes defined in the namespace.

I'm not sure what I was missing the first few times, but persistance finally paid off.

Thanks for all of your help.
DP
 
Glad you remembered about putting the referenced DLL into the bin directory - I had forgotton (the IDE does this for you).

Hope I was of some help, anyway.

Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top