473,396 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problem using namespaces

I have the following code which I am having trouble with. The problem
is detailed in the comments near the end.

Constants.cs:
--------------
namespace MyApp.Const
{
public class Const
{
public const int iID = 26;
public const string sUser = "Joe";
........
}
}
Main.cs
---------
using namespace MyApp.Const;

namespace MyApp.Main
{
public class MyClass
{
.....

private void SomeFunction()
{
int i = Const.iID; // causes compiler error
'The type or namespace name 'iID' does not exist in the class or
namespace 'MyApp.Const' (are you missing an assembly reference?)'
int i = Const.Const.iID; // works, but why the
double Const?
}

}
}

How can I get around this problem? I have found that it works if I
change the namespace name in Constants.cs to something like
MyApp.Constants(ie: different to the name of the class inside it), but
I would prefer to have the names the same.

Thanks

Nov 16 '05 #1
1 1355
You have a namespace called MyApp.Const. In this namespace there is a class called Const. Now think about the framework. There is a namespace called System and in there there is a class called Console (amongst others) so the fully qualified name of this class is System.Console. Now go back to your code and you can see that the class is in fact MyApp.Const.Const

If you are from a Java background you may be confusing the "using" statement withthe Java "import" statement. They are very different things. using simply removes the need for the developer to type the namespace when using a typename, so:

using System;
class App
{
static void Main()
{
Console.WriteLine("Hello World"); // notice the lack of the word System here
}
}

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

I have the following code which I am having trouble with. The problem
is detailed in the comments near the end.

Constants.cs:
--------------
namespace MyApp.Const
{
public class Const
{
public const int iID = 26;
public const string sUser = "Joe";
.......
}
}
Main.cs
---------
using namespace MyApp.Const;

namespace MyApp.Main
{
public class MyClass
{
....

private void SomeFunction()
{
int i = Const.iID; // causes compiler error
'The type or namespace name 'iID' does not exist in the class or
namespace 'MyApp.Const' (are you missing an assembly reference?)'
int i = Const.Const.iID; // works, but why the
double Const?
}

}
}

How can I get around this problem? I have found that it works if I
change the namespace name in Constants.cs to something like
MyApp.Constants(ie: different to the name of the class inside it), but
I would prefer to have the names the same.

Thanks
--
No virus found in this incoming message.
Checked by AVG Anti-Virus.
Version: 7.0.300 / Virus Database: 265.6.8 - Release Date: 03/01/2005

[microsoft.public.dotnet.languages.csharp]
Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Mike McGavin | last post by:
Hi everyone. I've been trying for several hours now to get minidom to parse namespaces properly from my stream of XML, so that I can use DOM methods such as getElementsByTagNameNS(). For some...
0
by: Nashat Wanly | last post by:
HOW TO: Call a Parameterized Stored Procedure by Using ADO.NET and Visual C# .NET View products that this article applies to. This article was previously published under Q310070 For a Microsoft...
5
by: OJO | last post by:
Hello microsoft.public.dotnet.xml! I need to parse some 'jabber xml' (www.jabber.org). I opted for using System.Xml.XmxDocument. The sample 'jabber xml' goes here: <message...
5
by: Alexis | last post by:
Hello, I have a set of classes I created from schema files using the xsd.exe tool. I'm using namespaces in the clases ( I had to because I have some classes with the same name but not the same...
3
by: Steven Fox | last post by:
============================================================ About DB2 Administration Tools Environment ============================================================ DB2 administration tools level:...
35
by: +-={K-SoL}=-+ | last post by:
when I try to reference 2 obfuscated libraries at the same time visual studio gives me a compilation error since the obfuscator has converted some name spaces to a and b in both libraries, now I...
5
by: Steven Wolf | last post by:
hi again, i have several namespacing problems in my big project. my current namespaces in my solution are: b2b.Server.Common b2b.Server.Business b2b.Server.Business.Mapping...
3
by: Yair Cohen | last post by:
hello 1 i use an xml documet with xml schema but when i try to use XPath (selectnodes) it retrieves empty list. when i remove the schema (the 'xmlns="http..."') i get the data. how can i handle...
5
by: TPJ | last post by:
I have the following code: ----------------------------------- def f(): def g(): a = 'a' # marked line 1 exec 'a = "b"' in globals(), locals() print "g: a =", a
0
by: PSingh | last post by:
Hi, I know this is a frequently asked question but have tried several combinations and cant seem to figure this out. I am serializing my object as follows: XmlSerializer serializer = new...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.