Casting classes and subclasses

About Monkey 2 Forums Monkey 2 Programming Help Casting classes and subclasses

This topic contains 3 replies, has 2 voices, and was last updated by  Mark Sibly 2 years, 11 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #855

    Ethernaut
    Participant

    This code:

    Works fine in M1, the result is True in the first two tests and False in the third.

    But I get a “Type ‘Void’ cannot be invoked” error in M2. How do I figure out if an object is of a certain class or its subclasses? How do I cast objects?

    #856

    Mark Sibly
    Keymaster

    Hi,

    You have to use the new ‘cast’ operator, eg:

    If Cast<Alpha>( test ) Then Print( “alpha” )

    Basically the same idea as the old cast with slightly different syntax:

    Cast< SomeType >( expression )

    Bye!
    Mark

    #857

    Ethernaut
    Participant

    Got it. Thanks!

    #858

    Mark Sibly
    Keymaster

    Another thing – you can still use the old syntax for converting built-in types, eg:

    Local x:Float=1.0

    Local y:Int=Int( x )

    (Not actually necessary in this case but you get the idea…)

     

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.