Problem using namespaces

W

wadefleming

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
 
R

Richard Blewett [DevelopMentor]

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]
 

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

Top