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.apstry 1
{
public class apstry1
{
public apstry1()
{
}
public static void Dummy()
{
System.Windows. Forms.MessageBo x.Show("dummy call");
}
}
public class noName
{
public noName()
{
}
}
}
file2.cs -
using System;
using apstry.A.apstry 1;
namespace apstry.A.apstry 2
{
public class Class11
{
public Class11()
{
System.Windows. Forms.MessageBo x.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.apstr y1' (are you missing an assembly reference?)*/
noName y;
noName z;
}
public void duh()
{
}
}
} 7 1438
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******** ******@TK2MSFTN GP12.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.apstry 1 { public class apstry1 { public apstry1() { } public static void Dummy() { System.Windows. Forms.MessageBo x.Show("dummy call"); } } public class noName { public noName() { } } }
file2.cs - using System; using apstry.A.apstry 1; namespace apstry.A.apstry 2 { public class Class11 { public Class11() { System.Windows. Forms.MessageBo x.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.apstr y1' (are you missing an assembly reference?)*/
noName y; noName z;
} public void duh() { } } }
public void duh()
Lol, I like that one.
Fergus
"Aniket Sule" <as***@meridium .com> wrote in message
news:u6******** ******@TK2MSFTN GP12.phx.gbl
I think that the problem is in :
namespace apstry.A.apstry 1
{
public class apstry1
{
..
..
..
You have a namespace and class with same name : apstry1.
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******** ******@TK2MSFTN GP12.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.apstry 1 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******** ******@TK2MSFTN GP12.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.apstry 1 { public class apstry1 { public apstry1() { } public static void Dummy() { System.Windows. Forms.MessageBo x.Show("dummy call"); } } public class noName { public noName() { } } }
file2.cs - using System; using apstry.A.apstry 1; namespace apstry.A.apstry 2 { public class Class11 { public Class11() { System.Windows. Forms.MessageBo x.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.apstr y1' (are you missing an assembly reference?)*/
noName y; noName z;
} public void duh() { } } }
It's because your first class has the same name as the enclosing namespace.
The compiler thinks you are trying to call
apstry.A.apstry 1.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******** ******@TK2MSFTN GP12.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.apstry 1 { public class apstry1 { public apstry1() { } public static void Dummy() { System.Windows. Forms.MessageBo x.Show("dummy call"); } } public class noName { public noName() { } } }
file2.cs - using System; using apstry.A.apstry 1; namespace apstry.A.apstry 2 { public class Class11 { public Class11() { System.Windows. Forms.MessageBo x.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.apstr y1' (are you missing an assembly reference?)*/
noName y; noName z;
} public void duh() { } } }
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.apstry 1;
using NewName = apstry.A.apstry 1.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******** ********@TK2MSF TNGP09.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******** ******@TK2MSFTN GP12.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.apstry 1 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******** ******@TK2MSFTN GP12.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.apstry 1 { public class apstry1 { public apstry1() { } public static void Dummy() { System.Windows. Forms.MessageBo x.Show("dummy call"); } } public class noName { public noName() { } } }
file2.cs - using System; using apstry.A.apstry 1; namespace apstry.A.apstry 2 { public class Class11 { public Class11() { System.Windows. Forms.MessageBo x.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.apstr y1' (are you missing an assembly reference?)*/
noName y; noName z;
} public void duh() { } } }
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.apstry 1;] 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******** ******@TK2MSFTN GP12.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.apstry 1 { public class apstry1 { public apstry1() { } public static void Dummy() { System.Windows. Forms.MessageBo x.Show("dummy call"); } } public class noName { public noName() { } } }
file2.cs - using System; using apstry.A.apstry 1; namespace apstry.A.apstry 2 { public class Class11 { public Class11() { System.Windows. Forms.MessageBo x.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.apstr y1' (are you missing an assembly reference?)*/
noName y; noName z;
} public void duh() { } } }
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 understand that you declare your
intention to use a namespace within a page through the "Inherits" attribute.
I know that using "Inherits" isn't absolutely necessary, it's just
recommended so the developer doesn't have to type out the entire...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
| |
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |