0

I have a treeview which is made up of IDetail and IAssembly items.

public interface IDetail {
    string Nomenclature { get; set; }
    string Cost { get; set; }
}
public interface IAssembly:IDetail{
    IEnumerable<IDetail> DirectChildren { get; }
    IEnumerable<IDetail> LoadChildren();
}

I'm adding the ability to insert childen into the tree. I can add a IDetail child to the IAssembly just fine but want to include ability to add a child to the IDetail. This means I need to convert IDetail to IAssembly so that it can have children.

How do I cast IDetail to IAssembly assuming I'm working within IDetail. Can I cast the object I'm in to IAssembly? this is only a get and I can't set this=(IAssembly)this;

Thanks for reading,

UPDATE Let me clarify my question a bit.. I don't want to add children to IDetail but if a user decides to branch out the current item more, then the current item of type IDetail will switch to IAssembly, then add IDetail as a child.

markokstate
  • 923
  • 2
  • 14
  • 28
  • 1
    Well you can't *assign* to `this` - but you can still *cast* `this`, assuming you're confident that it really is an `IAssembly`. It's hard to know the exact context here, given that you're not going to be writing implementation code in an interface anyway... – Jon Skeet Sep 23 '15 at 13:18
  • 1
    Would it make sense to move `DirectChildren` and `LoadChildren()` declarations to `IDetail`? – Dmytro Shevchenko Sep 23 '15 at 13:20
  • I believe this post may have your answer. I think it's deliberately not able to be done. I realize you're dealing with interfaces, but still a class that implements the IDetail interface shouldn't be able to be cast backwards like that. http://stackoverflow.com/questions/8329470/convert-derived-class-to-base-class – Justin Russo Sep 23 '15 at 13:21
  • If you are making `IDetail` aware of children, you don't need two interfaces. – Igor Sep 23 '15 at 13:24
  • It's not that I want to make IDetail aware of children, but before I add a child I want to change the type to IAssembly, then add children – markokstate Sep 23 '15 at 14:57

4 Answers4

0

You can take a look to Covariance and ContraVariance (or CounterVariance) on MSDN This is the article on MSDN

If the object you put inside the DirectChildren is an IDetail you cannot convert it to IAssembly, but if the object you put in the DirectChildren is an IAssembly you can use its method making a cast of the Directchildren element to IAssembly to use its method.

Sabrina_cs
  • 421
  • 3
  • 18
0

If you need to add children to both IDetail types and IAssembly types you can try moving the child related code (field(s) and method(s)) up into IDetail rather than IAssembly.

Update: Rather than casting the same object to a new type, you should be able to simply create a new IAssembly object and add the current IDetail object as a child. You would then have to replace the IDetail object in the treeview with the new IAssembly object.

Jordan Shurmer
  • 946
  • 7
  • 21
0
class Program
   {
        public static void Main()
        {
            Program P = new Program(); //Creating an instance.
            IAssembly iAssebly = ((IAssembly)P); //Casting it to IAssembly.
            IDetail iDetail = ((IDetail)P); //Casting it to IDetail.

            IAssembly iDetailToIAssembly = ((IAssembly)iDetail); //Casting IDetail to IAssemmbly.
            IDetail iAssemblyToIDetail = ((IDetail)iAssebly); //Casting IAssemmbly to IDetail.
        }

    }
Jayrooi
  • 3
  • 1
0

I suggest you to use Composite Pattern, it's gof structural pattern, you can find code sample, copy paste, documentation and class diagram.

link : http://www.dofactory.com/net/composite-design-pattern

Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51
  • With this example I would need to add IAssembly(composite) to the tree before I could add IDetail (leafs) to it. Which will probably have to do for now. What I would prefer is to only always add IDetails and whenever an IDetail gets something added to it, it converts itself to IAssembly then adds child. – markokstate Sep 23 '15 at 15:30
  • yes i inderstand, but with your organization you can define your composite and your child, these two respect contract – Aghilas Yakoub Sep 24 '15 at 06:52