Package access confusion

K

K Viltersten

I'm using the following setup.

namespace Outer.ProjName {
public class ProjName {...} }

Note that the namespace and the class have the same
name. When i in another class make the call as this:

namespace Outer.SomeStuff {
class Class {
public Class () {
ProjName.ProjName.someMethod();
} } }

It works. But skipping the first "ProjName" and
adding "using Outer.ProjName;" as follows, seems
to confuse C#, as it believes that it's the namespace
i'm referring to, not the class in a namespace that
i've using'ed.

Suggestions? The only solution i see is not to use
the same name for the namespace and the
contained class. Is there anything more neat?
 
F

Family Tree Mike

K Viltersten said:
I'm using the following setup.

namespace Outer.ProjName {
public class ProjName {...} }

Note that the namespace and the class have the same
name. When i in another class make the call as this:

namespace Outer.SomeStuff {
class Class {
public Class () {
ProjName.ProjName.someMethod();
} } }

It works. But skipping the first "ProjName" and
adding "using Outer.ProjName;" as follows, seems
to confuse C#, as it believes that it's the namespace
i'm referring to, not the class in a namespace that
i've using'ed.

Suggestions? The only solution i see is not to use
the same name for the namespace and the
contained class. Is there anything more neat?

--
Regards
Konrad Viltersten
----------------------------------------
May all spammers die an agonizing death;
have no burial places; their souls be
chased by demons in Gehenna from one room
to another for all eternity and beyond.


I have not had to use this too much, but you can do the following:

using foo = Outer.ProjeName.ProjName;

then in code you would do:

foo opp = new foo();
foo.someMethod();
 
F

Family Tree Mike

I have not had to use this too much, but you can do the following:
using foo = Outer.ProjeName.ProjName;

then in code you would do:

foo opp = new foo();
foo.someMethod();


Sorry, that is obviously, opp.someMethod();
 
K

K Viltersten

namespace Outer.ProjName {
I have not had to use this too much, but you can do the following:
using foo = Outer.ProjeName.ProjName;
then in code you would do:
foo opp = new foo();
foo.someMethod();

Right. That's the suggestion i got from Resharper too. It
seems to be a very awkward solution, though. I'd rather
rename the classes/packages, in fact. :)

Thanks anyway!
 
J

Jeff Johnson

I have not had to use this too much, but you can do the following:

using foo = Outer.ProjeName.ProjName;

then in code you would do:

foo opp = new foo();
foo.someMethod();

I think that should really be

using foo = Outer.ProjeName.ProjName;

bar opp = new foo.bar();
opp.someMethod();
 
F

Family Tree Mike

Jeff Johnson said:
I think that should really be

using foo = Outer.ProjeName.ProjName;

bar opp = new foo.bar();
opp.someMethod();


Actually, no, the OP stated ProjName was a class. The using statement
example I gave aliases the full name of the class to something completely
different. I use this when classes with similar names to .net classes, such
as:

using esriPolygon = ESRI.Geometry.Polygon;
using msPolygon = System.Drawing.Polygon;

It avoids the name conflicts, and makes the code more readable (at least to
me...).
 
J

Jeff Johnson

Actually, no, the OP stated ProjName was a class. The using statement
example I gave aliases the full name of the class to something completely
different. I use this when classes with similar names to .net classes,
such as:

using esriPolygon = ESRI.Geometry.Polygon;
using msPolygon = System.Drawing.Polygon;

It avoids the name conflicts, and makes the code more readable (at least
to me...).

Okay. I had no idea using could alias a class name; I thought it only
applied to namespaces.

And lookee there, right in the documentation: To create an alias for a
namespace OR A TYPE. Sheesh. The sad part is that I just looked it up a
couple of weeks ago because I couldn't remember the syntax. Guess I just
glanced at the sample and didn't read the description....
 
A

Arne Vajhøj

K said:
Right. That's the suggestion i got from Resharper too. It
seems to be a very awkward solution, though. I'd rather
rename the classes/packages, in fact.

I think rename is the optimal solution - unless
you participate in a contest about most obfuscated
code.

:)

Arne
 
K

K Viltersten

I have not had to use this too much, but you can do the following:
I think rename is the optimal solution - unless
you participate in a contest about most obfuscated
code.

No, i truly don't. It just seems that way... :)
 

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