none
casting (or converting) from base class to UNKNOWN derived class. RRS feed

  • Question

  • I have a little app which is driving me crazy this afternoon. Perhaps you can help.

    This app uses a base class, "NetworkBase", and several derived classes: "A_Derived" and "B_Derived" and a coupla others.

    The User may choose to launch a widget associated with either "A_Derived", "B_Derived" (or one of two others).  The widget code has only the base class available for inspection.  Previously, I cheated because I KNEW that the base class was actually an "A_derived", so I performed explicit casts, but today I began to implement the ""B_Derived" class and ran into a problem with the Widget code:   I can no longer cheat with explicit casts, because I don't KNOW which derived type I'm working with.   I am in dire need of a more sophisticated remedy.

    It seems to me that I solved an identical problem four years ago using Reflection, but I'm so old that I no longer can  recall what I did 4 DAYS ago, let alone four years ago.

    I SEEM to have solved the problem (it compiles and runs!) with IF statements  (e.g.if pNet is A_Derived) …  but I'd prefer to do it using Reflection (that is SO elegant!).

    So, bottom Line, can you help this old fool find a Reflection solution ?

    Thanks for your help, Gurus.

    Saturday, August 18, 2018 12:54 AM

Answers

  • I would suggest you show us a simple example of what's been done so far so those who are here to help can clearly see things.

    If there are Interfaces involved show them too.

    In the mean time see the following.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Lincoln_MA Saturday, August 18, 2018 4:57 PM
    Saturday, August 18, 2018 5:18 AM
    Moderator

All replies

  • I would suggest you show us a simple example of what's been done so far so those who are here to help can clearly see things.

    If there are Interfaces involved show them too.

    In the mean time see the following.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Lincoln_MA Saturday, August 18, 2018 4:57 PM
    Saturday, August 18, 2018 5:18 AM
    Moderator
  • Good morning Karen, and thank you for taking the time to reply.

    Last night I decided that perhaps, as much as I may want to use it, Reflection may not be a good fit for this particular instance. So with a few deft key-clicks,  BeyondCompare has restored the original code. It's back to basics for me …. abstract and override.

    It broke my heart .. opportunities to use reflection are few and far-between in my line of work. I had hoped this was one, I was even more-than-willing to take the performance hit associated with Reflection, but expediency rules.

    Thank you for your help.

    Saturday, August 18, 2018 4:15 PM