Note that there are some explanatory texts on larger screens.

plurals
  1. POImplementations of an Interface with Different Types?
    text
    copied!<p>Searched as best I could but unfortunately I've learned nothing relevant; basically I'm trying to work around the following problem in C#...</p> <p>For example, I have three possible references (refA, refB, refC) and I need to load the correct one depending on a configuration option. So far however I can't see a way of doing it that doesn't require me to use the name of said referenced object all through the code (the referenced objects are provided, I can't change them). Hope the following code makes more sense:</p> <pre><code>public ??? LoadedClass; public Init() { /* load the object, according to which version we need... */ if (Config.Version == "refA") { Namespace.refA LoadedClass = new refA(); } else if (Config.Version == "refB") { Namespace.refB LoadedClass = new refB(); } else if (Config.Version == "refC") { Namespace.refC LoadedClass = new refC(); } Run(); } private void Run(){ { LoadedClass.SomeProperty... LoadedClass.SomeMethod(){ etc... } } </code></pre> <p>As you can see, I need the Loaded class to be public, so in my limited way I'm trying to change the type 'dynamically' as I load in which real class I want. Each of refA, refB and refC will implement the same properties and methods but with different names. Again, this is what I'm working with, not by my design.</p> <p>All that said, I tried to get my head around Interfaces (which sound like they're what I'm after) but I'm looking at them and seeing strict types - which makes sense to me, even if it's not useful to me.</p> <p>Any and all ideas and opinions are welcome and I'll clarify anything if necessary. Excuse any silly mistakes I've made in the terminology, I'm learning all this for the first time. I'm really enjoying working with an OOP language so far though - coming from PHP this stuff is blowing my mind :-)</p> <p>EDIT</p> <p>Sorry for not making it clear at all, but each of refA, refB, refC has unique types for their methods, even though they're the essentially the same method functionality wise (the idea being they're versioned). This means with an interface, in my mind, I'm left with:</p> <pre><code>public interface IRef { SomeType&lt;&lt;RefA,RefB,RefC,???&gt;&gt; SomeProperty {get;} void SomeMethod(); } </code></pre> <p>Thanks to GenericTypeTea for the starting Interface point. I may be trying to do something that's not recommended/not possible... The underlying problem is I need to support different versions (as per the configuration option) from the same program; or I'm going to be developing 'different' programs for each version and that's just a big horrible mess :-)</p> <p>EDIT 2</p> <pre><code>public interface Sage { SageDataObject???.SDOEngine sdo; } class SageObj150 : Sage { SageDataObject150.SDOEngine sdo = new SageDataObject150.SDOEngine(); } class SageObj160 : Sage { SageDataObject160.SDOEngine sdo = new SageDataObject160.SDOEngine(); } class SageObj170 : Sage { SageDataObject170.SDOEngine sdo = new SageDataObject170.SDOEngine(); } </code></pre> <p>Where SDOEngine() (I've only just seen) is an interface itself - which I think just confused things greatly. Thought I was doing pretty well to write the whole application starting with no knowledge of C# but this now seems beyond my Google skills. Thanks in advance for everyone's patience!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload