Click here to Skip to main content
15,843,623 members
Home / Discussions / C#
   

C#

 
AnswerRe: Extract ConnectionString from App.Config Pin
ZurdoDev12-Apr-12 9:56
professionalZurdoDev12-Apr-12 9:56 
GeneralRe: Extract ConnectionString from App.Config Pin
PIEBALDconsult12-Apr-12 13:08
mvePIEBALDconsult12-Apr-12 13:08 
AnswerRe: Extract ConnectionString from App.Config Pin
Bernhard Hiller12-Apr-12 22:06
Bernhard Hiller12-Apr-12 22:06 
QuestionMultiple Inhertance solution Pin
zeeShan anSari12-Apr-12 4:54
zeeShan anSari12-Apr-12 4:54 
AnswerRe: Multiple Inhertance solution Pin
zeeShan anSari12-Apr-12 5:06
zeeShan anSari12-Apr-12 5:06 
GeneralRe: Multiple Inhertance solution Pin
PIEBALDconsult12-Apr-12 5:50
mvePIEBALDconsult12-Apr-12 5:50 
GeneralRe: Multiple Inhertance solution Pin
BobJanova12-Apr-12 6:01
BobJanova12-Apr-12 6:01 
GeneralRe: Multiple Inhertance solution Pin
BobJanova13-Apr-12 0:31
BobJanova13-Apr-12 0:31 
I thought about this some more and you can make the fakery even better, as long as you can actually inherit from A and B (which is a requirement for multiple inheritance when it's supported, obviously):



class Multi { // : A, B
 private A a;
 private B b;
 
 Multi(string argForA, int argForB){
  a = new HostedA(this, argForA);
  b = new HostedB(this, argForB);
 }
 
 public static implicit operator A(Multi m) { return m.a; }
 public static implicit operator B(Multi m) { return m.b; }

 public static implicit operator Multi(A a) { return ((HostedA)a).host; }
 public static implicit operator Multi(B b) { return ((HostedB)b).host; } 

 private class HostedA : A {
  internal Multi host;
  internal HostedA(Multi host, string argsForA) : base(argsForA) {
   this.host = host;
  }
 }

 private class HostedB : B {
  internal Multi host;
  internal HostedB(Multi host, int argsForB) : base(argsForB) {
   this.host = host;
  }
 }
}


Those cast-back operators will fail if you give it the wrong sort of A or B (i.e. one that isn't a cast-out of a Multi), but that's correct (though the exception message might not be quite right). You can override behaviour 'inherited' from A and B in HostedA and HostedB, and as inner classes they have access to their host's private state and methods.

If you want Multi itself to be virtual and inheritable, you can virtualise the creation of the instances of A and B and make the hosted classes visible for inheritance:

class Multi { // : A, B
 private A a;
 private B b;
 
 Multi(string argForA, int argForB){
  a = ConstructA();
  b = ConstructB();
 }

 protected virtual HostedA ConstructA(string argForA) { return new HostedA(this, argForA); }
 protected virtual HostedB ConstructB(int argForB) { return new HostedB(this, argForB); }
 
 public static implicit operator A(Multi m) { return m.a; }
 public static implicit operator B(Multi m) { return m.b; }

 public static implicit operator Multi(A a) { return ((HostedA)a).Host; }
 public static implicit operator Multi(B b) { return ((HostedB)b).Host; } 

 protected class HostedA : A {
  public Multi Host { get; private set; }
  public HostedA(Multi host, string argsForA) : base(argsForA) {
   this.Host = host;
  }
 }

 protected class HostedB : B {
  public Multi Host { get; private set; }
  public HostedB(Multi host, int argsForB) : base(argsForB) {
   this.Host = host;
  }
 }
}


To override some of Multi's 'A' behaviour, create a new inner subclass and override the construction method:

class Subclass : Multi {
 protected override Multi.HostedA ConstructA(string argForA) { return new HostedA(argForA); }

 protected class HostedA : Multi.HostedA {
  public HostedA(Multi host, string argsForA) : base(host, argsForA) {}

  public override string ToString() { "subclassed A"; }
 }
}


... and now ((A)new Subclass("A", 12)).ToString() -> "subclassed A". Casting back with subclassing is a bit ugly though, you can't do (Subclass)(A)mySubclass, instead having to do (Subclass)(Multi)(A)mySubclass.

This retains the disadvantage over real multiple inheritance that a cast from Multi to A gives you a different object (unlike a cast from Button to Control or from an object to an implemented interface). However, the A which is returned maintains a reference to the Multi and protects it from garbage collection, and it should now be castable back (i.e. (Multi)(A)myMulti == (Multi)(B)myMulti == myMulti). And, of course, to use any A methods on a Multi instance you must do ((A)myMulti).MethodOnA(), because the compiler won't know to cast and the methods aren't actually defined on Multi (and although you could create forwarding shims, that makes your code ugly).

However, an approach where you inherit normally from one class and fake-inherit like this from another:

class Multi : A { // : A, B
 private B b;
 
 Multi(string argForA, int argForB) : base(argForA) {
  b = ConstructB();
 }
 
 protected virtual HostedB ConstructB(int argForB) { return new HostedB(this, argForB); }
 
 public static implicit operator B(Multi m) { return m.b; }
 
 public static implicit operator Multi(B b) { return ((HostedB)b).Host; } 
 
 protected class HostedB : B {
  public Multi Host { get; private set; }
  public HostedB(Multi host, int argsForB) : base(argsForB) {
   this.Host = host;
  }
 }
}


... could make sense if B is a decorator class and A is the main functionality which you want to be able to use normally.
AnswerRe: Multiple Inhertance solution Pin
RobCroll12-Apr-12 15:40
RobCroll12-Apr-12 15:40 
QuestionSetting users access permissions on tabs in C# tabcontrol Pin
Xonel11-Apr-12 23:46
Xonel11-Apr-12 23:46 
AnswerRe: Setting users access permissions on tabs in C# tabcontrol Pin
Mycroft Holmes12-Apr-12 1:05
professionalMycroft Holmes12-Apr-12 1:05 
GeneralRe: Setting users access permissions on tabs in C# tabcontrol Pin
Xonel12-Apr-12 1:22
Xonel12-Apr-12 1:22 
GeneralRe: Setting users access permissions on tabs in C# tabcontrol Pin
Mycroft Holmes12-Apr-12 5:29
professionalMycroft Holmes12-Apr-12 5:29 
NewsRe: Setting users access permissions on tabs in C# tabcontrol Pin
Eddy Vluggen12-Apr-12 2:05
professionalEddy Vluggen12-Apr-12 2:05 
GeneralRe: Setting users access permissions on tabs in C# tabcontrol Pin
Mycroft Holmes12-Apr-12 5:33
professionalMycroft Holmes12-Apr-12 5:33 
AnswerRe: Setting users access permissions on tabs in C# tabcontrol Pin
Eddy Vluggen12-Apr-12 9:54
professionalEddy Vluggen12-Apr-12 9:54 
GeneralRe: Setting users access permissions on tabs in C# tabcontrol Pin
Mycroft Holmes12-Apr-12 13:36
professionalMycroft Holmes12-Apr-12 13:36 
GeneralRe: Setting users access permissions on tabs in C# tabcontrol Pin
Eddy Vluggen13-Apr-12 8:51
professionalEddy Vluggen13-Apr-12 8:51 
Questionweb config for spring frame work Pin
SFORavi11-Apr-12 17:55
SFORavi11-Apr-12 17:55 
QuestionA Question Of Structure Pin
Roger Wright11-Apr-12 10:54
professionalRoger Wright11-Apr-12 10:54 
AnswerRe: A Question Of Structure Pin
Wes Aday11-Apr-12 11:05
professionalWes Aday11-Apr-12 11:05 
GeneralRe: A Question Of Structure Pin
Alan Balkany12-Apr-12 5:34
Alan Balkany12-Apr-12 5:34 
GeneralRe: A Question Of Structure Pin
Jason McBurney12-Apr-12 15:14
Jason McBurney12-Apr-12 15:14 
GeneralRe: A Question Of Structure Pin
Roger Wright12-Apr-12 20:47
professionalRoger Wright12-Apr-12 20:47 
AnswerRe: A Question Of Structure Pin
Big Daddy Farang11-Apr-12 11:14
Big Daddy Farang11-Apr-12 11:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.