Creating an empty class or struct array

About Monkey 2 Forums Monkey 2 Programming Help Creating an empty class or struct array

Tagged: 

This topic contains 5 replies, has 4 voices, and was last updated by  TomToad 1 year, 6 months ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #10905

    Anatol
    Participant

    Hi Monkeycoders,

    It’s still early days for me in Monkey 2, so this is a newbie question. I’m trying to create an initially empty array of custom classes or structs, but I only managed to create the array with Length 1, not empty. Is there any better way to create that array than this (simplified code)?

    I also have a related “best practice” question, but I’ll put that into a different thread.

    #10906

    Jesse
    Participant

    to initialize an empty array you can do it like this:

    Field controls := new Control[20] ' creates an array of 20 controls.

    they will still be null and will need to be added to the array.

    [/crayon]

    you can also add them in line like this:

    Field controls := new Control[](New Control(),New Control(), New Control() ]
    `

    #10908

    Anatol
    Participant

    Oh! Thanks Jesse. I could’ve sworn that I tried this, but apparently not. And yes, that works well, even with length 0.

    #10912

    TomToad
    Participant

    To create a zero length array, use ArrayName:Type[]. Then you can use Resize or New to add to the array

    #10921

    nerobot
    Participant

    Local Controls:Control[] ‘0 length controls

    really! why it’s not a null object?! magic. 🙂

    #10930

    TomToad
    Participant

    Arrays are a bit weird. A zero length array acts like a Null object, but at the same time has a Length property which it shouldn’t if the array is truly Null.

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

You must be logged in to reply to this topic.