Dynamic Cast

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Monomachus
    Recognized Expert New Member
    • Apr 2008
    • 127

    #16
    Originally posted by Wildhorn
    No they do not inherit from the same type, but they all have string variables that need to be treated the same way.

    Anyway, I got a very "dirty" solution that I do not like much but that will do it for the moment. I made a function that receive the class object and with a switch case return an object of the good type and it works now, but it piss me off that I cant on the fly cast stuff when language like LUA allow such task (you dont even need to cast anything, LUA knows what you want).
    In .NET 4.0 you can do dynamic cast. So please see the specs for dynamic keyword.
    Also please do notice that C# is a static/strongly-typed language and by adding DLR (Dynamic Language Runtime) it hasn't changed into a dynamic language.
    To see why is C# statically typed please visit the link "Why is C# statically typed?"

    Comment

    • hype261
      New Member
      • Apr 2010
      • 207

      #17
      Casting is ugly

      If you have to cast then your design is probably wrong. From what I understand of your problem is that you want to store multiple classes in the same array and then cast them out to use them. Even if you get this to work you will probably run into run time problems due to improper casting. Like others have suggested you should define an interface if you want multiple classes to have the same behavior even if they don't derive from the same type.

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #18
        Could you post sample LUA code that does what you want?

        This is mostly for interest, but also to see if there is another way to do it.

        Comment

        Working...