namespace being shadowed - c#

so I have a library Mine.SuperFun which calls stuff in the library SuperFun whose main namespace is SuperFun. The problem i'm having is that i can't address classes or basically anything in the SuperFun library inside classes in the Mine.SuperFun.XyZFoo namespaces
The only way to address them i have is doing stuff like:
using SuperFun_NiceClass = SuperFun.NiceClass;
using Mine.SuperFun {
...
SuperFun_NiceClass.DoStuff()
is there something i can do (besides changing the namespace in Mine library) to be able to address those classes directly?

You can use the global contextual keyword
What is the usage of global:: keyword in C#?
http://msdn.microsoft.com/en-us/library/cc713620.aspx
namespace Mine.SuperFun
{
public class My { public int a; }
}
namespace SuperFun
{
public class Theirs { public int a; }
}
namespace SomeProgram
{
public class Program
{
SuperFun.Theirs theirs;
global::Mine.SuperFun.My mine;
}
}

Related

Compiler complaining on a missing namespace which is the name of the class

I have the following:
// Generator.cs
namespace MRP
{
public class Generator
{
public enum ModeGeneration
{
ByRequest,
ByCommit
}
}
}
// CustomerOrderWrapper.cs
namespace MRP
{
class CustomerOrderWrapper
{
readonly ModeGeneration _mode;
}
}
Why am I getting an error that the namespace name 'ModeGeneration' could not be found. Both classes are under MRP namespace, why the compiler is complaining on 'ModeGeneration' namespace?
Read the error message again. It should say something like "can't find class or namespace ModeGeneration", and it's correct, there is no class/namespace named ModeGeneration, maybe you want Generator.ModeGeneration?
As an inner class, Generator.ModeGeneration is the whole type name, and you can't omit the outer class name, because you can define a few inner classes with the same name in C# like this:
namespace Foo
{
class One
{
public class Bar { }
}
class Another
{
public class Bar { }
}
}
You can see Bar is ambiguous. You should use One.Bar and Another.Bar instead.
That is because your enum is defined in the class. Change the code as follows:
namespace MRP
{
class CustomerOrderWrapper
{
readonly Generator.ModeGeneration _mode;
}
}
There are two method to solve this problem
Method 1) You can use class name put in front of enum
namespace MRP
{
public class Generator
{
public enum ModeGeneration
{
ByRequest,
ByCommit
}
}
}
// CustomerOrderWrapper.cs
namespace MRP
{
class CustomerOrderWrapper
{
readonly Generator.ModeGeneration _mode;
}
}
Method 2) You can bring enum from in class to out namespace
namespace MRP
{
public enum ModeGeneration
{
ByRequest,
ByCommit
}
public class Generator
{
}
}
// CustomerOrderWrapper.cs
namespace MRP
{
class CustomerOrderWrapper
{
readonly ModeGeneration _mode;
}
}
I hope it will help you.
Yes they are in the same namespace, but enum ModeGeneration is also inside Generator class. So either:
Put the enum outside the Generator class and use it as you have
or
Prefix ModeGeneration in CustomerOrderWrapper.cs with Generator - Generator.ModeGeneration

Is there a way to reuse enums in other classes? [duplicate]

This question already has answers here:
What does the using directive do, exactly?
(4 answers)
Closed 6 years ago.
Is there a way to reuse enums in other classes?
For example,
I have a class using another class's enums. However, I want to avoid having to type the other class's namespace every time I use that class's enums for my active class.
Is there a good reason why you can't just use a using statement?
using Namespace.With.Enum;
namespace Namespace.Without.Enum
{
public class Foobar
{
...
}
}
This should allow you to use the enum inside Foobar without needing to preface it.
I don't see the issue with putting a using statement at the top of each file.
namespace MyNameSpace.EnumNamespace
{
public enum MyEnum
{
One,Two,Three
}
}
And in a separate file
using MyNamespace.EnumNamespace;
namespace AnotherNamespace
{
public class AnotherClass
{
//somewhere in the same file
public void SomeMethod(MyEnum enumValue)
{
//Do stuff
}
}
}
Alternatively, you can also assign an alias as well.
using MyEnumAlias = MyNameSpace.EnumNamespace.MyEnum
If you just don't want put namespace and class name, e.g.
namespace AnotherNamespace {
...
public class AnotherClass {
...
public enum MyEnum {
One,
Two,
Three
}
}
...
}
...
namespace MyNamespace {
...
AnotherNamespace.AnotherClass.MyEnum v = ...
you can try creating synonym via using:
namespace MyNamespace {
using MyEnum = AnotherNamespace.AnotherClass.MyEnum;
...
MyEnum v = ...

Class Library Namespace Hierarchy Not Working

I am trying to achieve the namespace hierarchy used in C# in my class library. Here is what I am trying to do:
namespace Parent
{
namespace Child
{
Class ChildClass { }
}
Class ParentClass { }
}
After compiling the class library It did not work as expected. Here is my expected working principle.
To access the ChildClass one has to using Parent.Child. But one can access ParentClass just by using Parent.
I can do this without compiling the class library but adding the cs file to the project. But when I compile as DLL and add it as a reference in a project I can't access the sub-namespaces.
UPDATE: I have different files for each class. When I write all namespaces and classes into one file it seems to work. But why?
Is there anyway to achieve this in C#?
I think your classes missing public; Following code works for me.
namespace Parent
{
namespace Child
{
public class ChildClass { }
}
public class ParentClass
{
}
}
I can create;
Parent.ParentClass p;
Parent.Child.ChildClass c;
Which is your expected working principle.
EDIT: separate cs file for each class approach;
ParentClass.cs
namespace Parent
{
public class ParentClass{ }
}
ChildClass.cs
namespace Parent
{
namespace Child
{
public class ChildClass { }
}
}
This seems to be working for me.
You are nesting classes and namespaces and it all seems a little confused. Why don't you keep a flatter namespace structure and do the nesting in your classes. Keep in mind that you don't need to nest namespaces or classes to maintain a parent child relationship.
Have a read of the following: Parent child class relationship design pattern
This should get you started in the right direction:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication2
{
using System;
using System.Collections.Generic;
public class ChildClass
{
private ParentClass parent;
public ChildClass(ParentClass parentIn)
{
parent = parentIn;
}
public ParentClass Parent
{
get { return parent; }
}
}
public class ParentClass
{
private List<ChildClass> children;
public ParentClass()
{
children = new List<ChildClass>();
}
public ChildClass AddChild()
{
var newChild = new ChildClass(this);
children.Add(newChild);
return newChild;
}
}
public class Program
{
public static void Main()
{
Console.WriteLine("Hello World");
var p = new ParentClass();
var firstChild = p.AddChild();
var anotherChild = p.AddChild();
var firstChildParent = firstChild.Parent;
var anotherChildParent = anotherChild.Parent;
}
}
}

C# namespace questions

what's the difference for the following two ways to define namespace?
namespace A.B.C {
public class AA{
}
}
namespace A {
namespace B{
namesapce C{
public class AA{
}
}
}
}
in some where I may have
namespace A{
//some classes
}
namespace A.B {
//some classes
}
namespace A {
namespace B {
//some classes
}
}
Both need to do the same to use class AA by using A.B.C; Can I use C.AA a; to specify the AA class in C namespace or I have to use the fall namespace convention: A.B.C.AA a; to avoid possbile confliction?
They're the same. If you look at this code in .NET Reflector:
namespace A {
namespace B{
namespace C{
public class AA{
}
}
}
}
you get this:
namespace A.B.C
{
public class AA
{
// Methods
public AA();
}
}
Both methods are compiled to exactly the same intermediate language code.

How to make an object belong to a namespace (VS 2008)?

I have a name space Company.Controls, which contains several controls. I also have a class called "Common" which contains enums/structures/static methods that I use throughout the controls.
Is there a way to make these "Common" peices belong to the Company.Controls namespace this way I don't have to keep typing "Common.Structure"? Essentially having he "Common" both a namespace and a class.
Just seems messy and confusing when reading the code.
example (all the other controls are in the Blah.Controls.Common namespace)
namespace Blah.Controls
{
public enum ControlTouchState
{
Down = 0x00,
Up = 0x01,
}
public Common()
{
//Stuff here
}
}
Thanks.
You can't get exactly what you want; in C# all methods have to be in a class.
Depending on what is in your Common class, you might be able to find something a slightly more satisfying by using extension methods:
namespace Blah.Controls
{
public class CommonControl { }
public static class Common
{
public static void Foo(this CommonControl cc) { }
}
public class Control1 : CommonControl
{
public void Bar()
{
this.Foo();
}
}
}
Another thing you might consider is using partial classes which would let you write simple wrappers elsewhere:
namespace Blop.Controls
{
public static class Common
{
public static void Foo() { }
}
public partial class Control1
{
public void Bar()
{
Foo();
}
}
public partial class Control1
{
public void Foo()
{
Common.Foo();
}
}
}
Obviously, introducing some inheritence could eliminate some of the duplication; I'm assuming you don't want to do that.
Is there some reason that the nested types in Common MUST be nested? Why not separate them out into their own namespace?
namespace Common
{
public struct Structure
{
// ...
}
public enum Enumeration
{
// ...
}
public class Common
{
// ...
}
}
You could then use the Common namespace as such:
namespace Blah.Controls
{
using Common;
class Control
{
Struct myStruct;
Enumeration myEnum;
Common myCommon; // references the class, not the namespace
}
}

Categories