473,406 Members | 2,345 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,406 software developers and data experts.

question on namespaces

Hi,
I am trying to use a class from one namespace in another via the using
directive. The 2 namespaces have some part of the name in common (A.B.x,
A.B.y). However in the second namespace, I am unable to access the class
from the first without qualifying it, which is what i want to avoid. Any
ideas what I am doing wrong?

I am attaching the code and compiler error below to show exactly what I am
trying to do,

Any help is greatly appreciated,
Thanks
Aniket

file1.cs -
using System;
namespace apstry.A.apstry1
{
public class apstry1
{
public apstry1()
{
}
public static void Dummy()
{
System.Windows.Forms.MessageBox.Show("dummy call");
}
}
public class noName
{
public noName()
{
}
}
}

file2.cs -
using System;
using apstry.A.apstry1;
namespace apstry.A.apstry2
{
public class Class11
{
public Class11()
{
System.Windows.Forms.MessageBox.Show("duh");
apstry1.apstry1.Dummy(); // -- this works
apstry1.Dummy();

/*--gives the error C:\temp\apstry2\apstry2\Class1.cs(22): The type or
namespace name 'Dummy' does not exist in the class or namespace
'apstry.A.apstry1' (are you missing an assembly reference?)*/

noName y;
noName z;

}
public void duh()
{
}
}
}
Nov 15 '05 #1
7 1421
The name of the class and the end of the namespace are the same so the
compiler doesn't know which one you want without prefixing the name of the
class with the last part of the namespace.

"Aniket Sule" <as***@meridium.com> wrote in message
news:u6**************@TK2MSFTNGP12.phx.gbl...
Hi,
I am trying to use a class from one namespace in another via the using
directive. The 2 namespaces have some part of the name in common (A.B.x,
A.B.y). However in the second namespace, I am unable to access the class
from the first without qualifying it, which is what i want to avoid. Any
ideas what I am doing wrong?

I am attaching the code and compiler error below to show exactly what I am
trying to do,

Any help is greatly appreciated,
Thanks
Aniket

file1.cs -
using System;
namespace apstry.A.apstry1
{
public class apstry1
{
public apstry1()
{
}
public static void Dummy()
{
System.Windows.Forms.MessageBox.Show("dummy call");
}
}
public class noName
{
public noName()
{
}
}
}

file2.cs -
using System;
using apstry.A.apstry1;
namespace apstry.A.apstry2
{
public class Class11
{
public Class11()
{
System.Windows.Forms.MessageBox.Show("duh");
apstry1.apstry1.Dummy(); // -- this works
apstry1.Dummy();

/*--gives the error C:\temp\apstry2\apstry2\Class1.cs(22): The type or
namespace name 'Dummy' does not exist in the class or namespace
'apstry.A.apstry1' (are you missing an assembly reference?)*/

noName y;
noName z;

}
public void duh()
{
}
}
}

Nov 15 '05 #2
public void duh()

Lol, I like that one.

Fergus
Nov 15 '05 #3
"Aniket Sule" <as***@meridium.com> wrote in message
news:u6**************@TK2MSFTNGP12.phx.gbl


I think that the problem is in :

namespace apstry.A.apstry1
{
public class apstry1
{
..
..
..

You have a namespace and class with same name : apstry1.
Nov 15 '05 #4
Thanks a lot, that explains it. I dont have control over the class naming
right now, so looks like I will have to go with the qualified name

"Joe" <jo*@nospam.com> wrote in message
news:e9**************@TK2MSFTNGP12.phx.gbl...
Hi Aniket,

apstry1.Dummy() doesn't work because the compiler is resolving the name by
following the highest level namespace down through nested namespaces. It
first finds the namespace apstry.A.apstry1 and then looks for Dummy. The
only class next in the hierarchy is aspstry1, but Dummy is a member of the
aspstry1 class and not at the same level, so the compiler doesn't find it
where you said it should be.

The best way to avoid this problem is by not naming a class the same as any of the namespaces in its containing hierarchy. The C# style guidelines (in the SDK docs) advise against this practice also.

Joe
--
http://www.csharp-station.com
"Aniket Sule" <as***@meridium.com> wrote in message
news:u6**************@TK2MSFTNGP12.phx.gbl...
Hi,
I am trying to use a class from one namespace in another via the using
directive. The 2 namespaces have some part of the name in common (A.B.x,
A.B.y). However in the second namespace, I am unable to access the class
from the first without qualifying it, which is what i want to avoid. Any
ideas what I am doing wrong?

I am attaching the code and compiler error below to show exactly what I am trying to do,

Any help is greatly appreciated,
Thanks
Aniket

file1.cs -
using System;
namespace apstry.A.apstry1
{
public class apstry1
{
public apstry1()
{
}
public static void Dummy()
{
System.Windows.Forms.MessageBox.Show("dummy call");
}
}
public class noName
{
public noName()
{
}
}
}

file2.cs -
using System;
using apstry.A.apstry1;
namespace apstry.A.apstry2
{
public class Class11
{
public Class11()
{
System.Windows.Forms.MessageBox.Show("duh");
apstry1.apstry1.Dummy(); // -- this works
apstry1.Dummy();

/*--gives the error C:\temp\apstry2\apstry2\Class1.cs(22): The type or namespace name 'Dummy' does not exist in the class or namespace
'apstry.A.apstry1' (are you missing an assembly reference?)*/

noName y;
noName z;

}
public void duh()
{
}
}
}


Nov 15 '05 #5
It's because your first class has the same name as the enclosing namespace.
The compiler thinks you are trying to call

apstry.A.apstry1.Dummy()

which of course doesn't work because namespaces cannot contain methods
directly. Just change the name of the first class to something else, and
the compiler won't get confused.

"Aniket Sule" <as***@meridium.com> wrote in message
news:u6**************@TK2MSFTNGP12.phx.gbl...
Hi,
I am trying to use a class from one namespace in another via the using
directive. The 2 namespaces have some part of the name in common (A.B.x,
A.B.y). However in the second namespace, I am unable to access the class
from the first without qualifying it, which is what i want to avoid. Any
ideas what I am doing wrong?

I am attaching the code and compiler error below to show exactly what I am
trying to do,

Any help is greatly appreciated,
Thanks
Aniket

file1.cs -
using System;
namespace apstry.A.apstry1
{
public class apstry1
{
public apstry1()
{
}
public static void Dummy()
{
System.Windows.Forms.MessageBox.Show("dummy call");
}
}
public class noName
{
public noName()
{
}
}
}

file2.cs -
using System;
using apstry.A.apstry1;
namespace apstry.A.apstry2
{
public class Class11
{
public Class11()
{
System.Windows.Forms.MessageBox.Show("duh");
apstry1.apstry1.Dummy(); // -- this works
apstry1.Dummy();

/*--gives the error C:\temp\apstry2\apstry2\Class1.cs(22): The type or
namespace name 'Dummy' does not exist in the class or namespace
'apstry.A.apstry1' (are you missing an assembly reference?)*/

noName y;
noName z;

}
public void duh()
{
}
}
}

Nov 15 '05 #6
100
Hi Aniket,
If you don't have control over the name of the class. You might be able to
change the name you are using only in file2. You can do that using aliases

file2:

using System;
using apstry.A.apstry1;
using NewName = apstry.A.apstry1.apstry1; //a new name for the class apstry1

and then call Dummy like:
NewName.Dummy();

HTH
B\rgds
100
"Aniket Sule" <as***@meridium.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Thanks a lot, that explains it. I dont have control over the class naming
right now, so looks like I will have to go with the qualified name

"Joe" <jo*@nospam.com> wrote in message
news:e9**************@TK2MSFTNGP12.phx.gbl...
Hi Aniket,

apstry1.Dummy() doesn't work because the compiler is resolving the name by
following the highest level namespace down through nested namespaces. It first finds the namespace apstry.A.apstry1 and then looks for Dummy. The only class next in the hierarchy is aspstry1, but Dummy is a member of the aspstry1 class and not at the same level, so the compiler doesn't find it where you said it should be.

The best way to avoid this problem is by not naming a class the same as any
of the namespaces in its containing hierarchy. The C# style guidelines

(in
the SDK docs) advise against this practice also.

Joe
--
http://www.csharp-station.com
"Aniket Sule" <as***@meridium.com> wrote in message
news:u6**************@TK2MSFTNGP12.phx.gbl...
Hi,
I am trying to use a class from one namespace in another via the using
directive. The 2 namespaces have some part of the name in common (A.B.x, A.B.y). However in the second namespace, I am unable to access the class from the first without qualifying it, which is what i want to avoid. Any ideas what I am doing wrong?

I am attaching the code and compiler error below to show exactly what I am trying to do,

Any help is greatly appreciated,
Thanks
Aniket

file1.cs -
using System;
namespace apstry.A.apstry1
{
public class apstry1
{
public apstry1()
{
}
public static void Dummy()
{
System.Windows.Forms.MessageBox.Show("dummy call");
}
}
public class noName
{
public noName()
{
}
}
}

file2.cs -
using System;
using apstry.A.apstry1;
namespace apstry.A.apstry2
{
public class Class11
{
public Class11()
{
System.Windows.Forms.MessageBox.Show("duh");
apstry1.apstry1.Dummy(); // -- this works
apstry1.Dummy();

/*--gives the error C:\temp\apstry2\apstry2\Class1.cs(22): The
type
or namespace name 'Dummy' does not exist in the class or

namespace 'apstry.A.apstry1' (are you missing an assembly reference?)*/

noName y;
noName z;

}
public void duh()
{
}
}
}



Nov 15 '05 #7
I'll agree with Joe that this the problem occurs because of the way name
resolutions is done in C#. However it's not that simple - for instance try
to move [using apstry.A.apstry1;] directive _inside_ apstry2 namespace and
see what happens.
If you find it entertaining you could go through sections 3.7, 3.8 and 9 of
the language cpecs, but I'd suggest not to bother and just avoid name
collisions.

Regards,
Val.
"Aniket Sule" <as***@meridium.com> wrote in message
news:u6**************@TK2MSFTNGP12.phx.gbl...
Hi,
I am trying to use a class from one namespace in another via the using
directive. The 2 namespaces have some part of the name in common (A.B.x,
A.B.y). However in the second namespace, I am unable to access the class
from the first without qualifying it, which is what i want to avoid. Any
ideas what I am doing wrong?

I am attaching the code and compiler error below to show exactly what I am
trying to do,

Any help is greatly appreciated,
Thanks
Aniket

file1.cs -
using System;
namespace apstry.A.apstry1
{
public class apstry1
{
public apstry1()
{
}
public static void Dummy()
{
System.Windows.Forms.MessageBox.Show("dummy call");
}
}
public class noName
{
public noName()
{
}
}
}

file2.cs -
using System;
using apstry.A.apstry1;
namespace apstry.A.apstry2
{
public class Class11
{
public Class11()
{
System.Windows.Forms.MessageBox.Show("duh");
apstry1.apstry1.Dummy(); // -- this works
apstry1.Dummy();

/*--gives the error C:\temp\apstry2\apstry2\Class1.cs(22): The type or
namespace name 'Dummy' does not exist in the class or namespace
'apstry.A.apstry1' (are you missing an assembly reference?)*/

noName y;
noName z;

}
public void duh()
{
}
}
}

Nov 15 '05 #8

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

Similar topics

11
by: Random | last post by:
I'm confused about the proper use and usefulness of namespaces. I beleive I understand the purpose is so the developer can put classes within namespaces to essentially organize your code. And I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
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.