New OOTS products from CafePress
New OOTS t-shirts, ornaments, mugs, bags, and more
Results 1 to 3 of 3
  1. - Top - End - #1
    Ogre in the Playground
    Join Date
    Aug 2009

    Default Typecasting in C#

    For some reason typecasting doesn't seem to work like it does in other languages.

    Maybe I'm just missing something.

    Consider the following set-up:

    Code:
    public class BaseClass {
    
    }
    
    public class DerivativeClass : BaseClass {
       public void DoStuff(){
          //do stuff
       }
    }
    Now lets assume a method as our context that takes an object of BaseClass as parameter. Naturally the method accepts objects of DerivativeClass too. And if the object is of the DerivativeClass type, DoStuff shall be called. All in all a very simple set-up that can come up reasonably often. In all other programming languages I know the following would work:

    Code:
    public void CallingContext(BaseClass anObject){
       if (anObject is DerivativeClass){
          (DerivativeClass)anObject.DoStuff;
       }
    }
    But C# doesn't like that. Regardless of the cast I can only see what fields and methods are available in BaseClass.
    Instead, C# forces the following construct:

    Code:
    public void CallingContext(BaseClass anObject){
       if (anObject is DerivativeClass){
          DerivativeClass dummy = (DerivativeClass)anObject;
          dummy.DoStuff;
       }
    }
    Why, C#, why?
    This is incredibly clunky and makes the code look stupid.

    Do I miss something or is C# (which I like otherwise) just dumb in this regard?

  2. - Top - End - #2
    Titan in the Playground
     
    Jasdoif's Avatar

    Join Date
    Mar 2007
    Location
    Oregon, USA

    Default Re: Typecasting in C#

    If I'm reading this right, you should be able to do
    Code:
    ((DerivativeClass)anObject).DoStuff
    .
    Feytouched Banana eldritch disciple avatar by...me!

    The Index of the Giant's Comments VI―Making Dogma from Zapped Bananas

  3. - Top - End - #3
    Surgebinder in the Playground Moderator
     
    Douglas's Avatar

    Join Date
    Aug 2005
    Location
    Mountain View, CA
    Gender
    Male

    Default Re: Typecasting in C#

    What you're missing is the order of operations involved - the dot happens before the cast, so you're casting the result of "derivedObject.DoStuff" to the derived type. If you want to cast first, the line needs to be this:
    Code:
    ((DerivativeClass)anObject).DoStuff;
    But! If you're working with the latest version of C#, version 7 which comes with Visual Studio 2017, there's a much more convenient way to do it:
    Code:
    if (anObject is DerivativeClass derivedObject) {
        derivedObject.DoStuff;
    }
    Yes, that's a variable declaration and assignment inside that conditional, accomplished by nothing more than adding the new variable's name to the end of the type check. It's called Pattern Matching, and was specifically designed for exactly this use case.
    Last edited by Douglas; 2017-10-27 at 06:05 PM.
    Like 4X (aka Civilization-like) gaming? Know programming? Interested in game development? Take a look.

    Avatar by Ceika.

    Archives:
    Spoiler
    Show
    Saberhagen's Twelve Swords, some homebrew artifacts for 3.5 (please comment)
    Isstinen Tonche for ECL 74 playtesting.
    Team Solars: Powergaming beyond your wildest imagining, without infinite loops or epic. Yes, the DM asked for it.
    Arcane Swordsage: Making it actually work (homebrew)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •