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

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
}
}

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

Nested classes in c#

Sorry I'm kinda new to c# how would I make a class where I can access it like this:
Myclass.subclass.method();
This is what I have now:
namespace zzcore
{
class myclass
{
class subclass
{
public static void method() { }
}
}
}
What happens here is that a nested class without a visibility modifier is implicitly private. In this context, private means that only the parent class can see it.
Declare both classes as public and you will be able to call myclass.subclass.method();
namespace zzcore
{
public class myclass
{
public class subclass
{
public static void method() { }
}
}
}
Working example: http://ideone.com/tJVKJ

namespace being shadowed

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;
}
}

Extension methods to extend class members?

I'd like to write an extension method that extends some member of my class. Specifically, I'd like to extend an enum. However, this is not working:
namespace mynamespace
{
public class myclass
{
public enum ErrorCodes
{
Error1, Error2, Error3
}
public static double GetDouble(this ErrorCodes ErrorCode)
{
return (double)((int)ErrorCode);
}
public void myfunc()
{
ErrorCodes mycode;
MessageBox.Show(mycode.GetDouble().ToString());
}
}
}
Specifically, it doesn't recognize GetDouble() as an extension. This is not just for enums either, I tried creating an extension method for doubles and had the same problem, too.
You can only write extension methods in top-level, static, non-generic classes, but they can extend nested classes. Here's a complete example, based on your code:
using System;
public static class Extensions
{
public static double GetDouble(this Outer.ErrorCode code)
{
return (double)(int)code;
}
}
public class Outer
{
public enum ErrorCode
{
Error1, Error2, Error3
}
}
public class Test
{
public static void Main()
{
Outer.ErrorCode code = Outer.ErrorCode.Error1;
Console.WriteLine(code.GetDouble());
}
}
The extension method must be defined in a static class.
See the manual.
edit
As Jon pointed out, the static class must be top-level, not nested.

How can I factor out the code duplication here?

So, I'd like to hear what you all think about this.
I have a project where three different inheritance paths need to all implement another base class. This would be multiple inheritance and isn't allowed in C#. I am curious how I can implement this without code duplication.
EDIT: I don't own the three classes. The three classes are from 3rd party code. So I cannot make them all extend my base class.
Right now I am using three different classes, each one extending a different base class. Then I have the same code in each of the three abstract classes.
I could use a single interface, but I would still need to duplicate the code.
I could make some kind of static class that implements the code and then reference that in each of the 3 abstract classes. It would eliminate the duplication, but, I am not sure how I feel about this. I could implement Extensions methods on the interface, but then the interface itself would be empty and the extension methods (containing the duplicate code) would be in a totally different file, which seems not quite right. Plus I can't implement properties in extension methods...
How can I factor out the code duplication here?
EDIT, inheritance tree:
class Class1 : 3rdPartyBaseClass1 { }
class Class2 : 3rdPartyBaseClass2 { }
class Class3 : 3rdPartyBaseClass3 { }
I have code I want to be in each of the above Classes, but I cannot add it to the 3rdPartyClasses.
Create an interface that Class1, Class2, and Class3 can implement. Then put your code in extension methods so it will apply to all.
interface IMyInterface {
void Foo(); //these are the methods that these
//classes actually have in common
void Bar();
}
public class Class1 : 3rdPartyBaseClass1, IMyInterface {
// whatever
}
public static class IMyInterfaceExtensions {
public static void CommonMethod(this IMyInterface obj) {
obj.Foo();
obj.Bar();
}
}
public static class Program {
public static void Main() {
var instance = new Class1();
instance.CommonMethod();
}
}
OK, you can do something similar to my previous suggestion, and also similar to recursive's suggestion. For the functionality you require in all three of your derived classes, you can create a single Interface along with a single class (call it "Implementer" for kicks) that implements that Interface (and that has the actual code you want executed with each call).
In each of your derived classes, then, you implement the Interface and create a private instance of Implementer. In each of the interface methods, you just pass the call along to the private instance of Implementer. Because Implementer and your derived classes all implement your Interface, any changes you make to the Interface will require you to modify Implementer and the derived classes accordingly.
And all your code is in one place, except for all the lines passings the calls on to the private instance of Implementer (obviously multiple inheritance would be better than this, but you go to war with the army you have, not the army you wish you had).
Update: what about just adding a public instance of your class to each of the derived classes?
public class DerivedClass1 : ThirdPartyClass1
{
public MyClass myClass = new MyClass();
}
Or if you care who Demeter is and you get paid by LOC:
public class DerivedClass1 : ThirdPartyClass1
{
private MyClass _myClass = new MyClass();
public MyClass myClass
{
get
{
return _myClass;
}
}
}
Then you'd just call the MyClass methods like this:
DerivedClass1 dc1 = new DerivedClass1();
dc1.myClass.DoSomething();
This way, we could all go to sleep.
Similar to MusiGenesis's suggestion, if you need the functionality of the 3rd party classes but do not have to descend from them, you could use composition as follows:
class ThirdPartyBaseClass1
{
public void DoOne() {}
}
class ThirdPartyBaseClass2
{
public void DoTwo() { }
}
class ThirdPartyBaseClass3
{
public void DoThree() { }
}
abstract class Base
{
public void DoAll() { }
}
class Class1 : Base
{
public void DoOne() { _doer.DoOne(); }
private readonly ThirdPartyBaseClass1 _doer = new ThirdPartyBaseClass1();
}
class Class2 : Base
{
public void DoTwo() { _doer.DoTwo(); }
private readonly ThirdPartyBaseClass2 _doer = new ThirdPartyBaseClass2();
}
class Class3 : Base
{
public void DoThree() { _doer.DoThree(); }
private readonly ThirdPartyBaseClass3 _doer = new ThirdPartyBaseClass3();
}
This also gives you the freedom to define whatever interfaces you want and implement them on your classes.
Sounds like you need to insert the new abstract class into the inheritance tree at whatever point those three paths come together, but there really isn't enough information to tell. If you could post some of your inheritance tree, that would help a lot.
I think you may want to use composition instead of inheritance. Exactly how to do this depends on what the third party classes look like, and what your own code looks like. Some more specific code relating to your problem would be helpful, but for example, suppose you want to have three different third party GUI widgets that all need to be customized with your own initializer code.
Case 1: Suppose your third party widgets look like:
public interface IThirdPartyWidget {
public void doWidgetStuff();
}
public class ThirdPartyWidget1: ThirdyPartyWidget implements IThirdPartyWidget {
...
}
public class ThirdPartyWidget2: ThirdPartyWidget implements IThirdPartyWidget {
...
}
You can do:
public class MyWidget implements IThirdPartyWidget {
private IThirdPartyWidget delegateWidget;
public MyWidget(IThirdPartyWidget delegateWidget) {
this.delegateWidget = delegateWidget;
}
public void doWidgetStuff() {
delegateWidget.doWidgetStuff();
}
}
Case 2: Suppose you absolutely need to extend those widgets, and you have to refactor your own code:
public class MyWidget1: ThirdPartyWidget1 {
public void myMethod() {
runMyCode();
}
private void runMyCode() {
//something complicated happens
}
}
public class MyWidget2: ThirdPartyWidget2 {
public void myMethod() {
runMyCode();
}
private void runMyCode() {
//something complicated happens
}
}
This can become:
public class MyCodeRunner {
public void runMyCode() {
//...
}
}
public class MyWidget1: ThirdPartyWidget1 {
private MyCodeRunner myCode = new MyCodeRunner();
public void myMethod() {
myCode .runMyCode();
}
}
public class MyWidget2: ThirdPartyWidget2 {
private MyCodeRunner myCode = new MyCodeRunner();
public void myMethod() {
myCode .runMyCode();
}
}
Hope this makes sense!

Categories