开发者

Howto access a namespace with the same name as a class instance?

I have a question regarding namespace names and classes:

If I have a class called cVeloConnect in namespace VeloConnect. e.g.

namespace VeloConnect
{
    public class cVeloConnect
    {
        // Some content
        public void PrintMe();

    }

    public class cSomeClass
    {
        // Some content

    }
}

And now I want to create a new instance of cVeloConnect, calling this instance VeloConnect, I cannot access the VeloConnect namespace anymore... e.g

VeloConnect.cVeloConnect VeloConnect = new VeloCon开发者_如何学JAVAnect.cVeloConnect();
VeloConnect.PrintMe();
// The below thing is not possible
VeloConnect.cSomeClass MyClass = new VeloConnect.cSomeClass();

How can I access the other class, if I don't want to rename the namespace or my previous instance ?


Well, one option is to follow naming conventions to avoid this sort of thing - your classes shouldn't start with c, and your local variables would normally start with a lower case letter.

To avoid having to use the namespace at all in the declaration, you can just use a using directive at the top of your code. Or if you really want to specify the namespace, you could use global:::

global::VeloConnect.cSomeClass MyClass = new global::VeloConnect.cSomeClass();

In general though, I don't like seeing namespaces within my actual code - I prefer having using directives so I can just use the simple names in the body of the code. Either way, I would strongly urge you to start following the .NET naming conventions.


Alias the namespace;

 using vc=VeloConnect;

 // ... then, later ....

 vc.cVeloConnect VeloConnect = new vc.cVeloConnect();
 VeloConnect.PrintMe();
 // The below thing is not possible
 vc.cSomeClass MyClass = new vc.cSomeClass();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜