About Monkey 2 › Forums › Monkey 2 Programming Help › Creating an empty class or struct array
Tagged: empty array
This topic contains 5 replies, has 4 voices, and was last updated by
TomToad 1 year, 6 months ago.
-
AuthorPosts
-
October 1, 2017 at 1:33 pm #10905
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)?
Monkey12345678910111213141516Class ControlField Name:StringField Trigger:Bool()Field Callback:Void()EndClass ControlManagerField Controls := New Control[]( New Control() ) ' how can I initialise this as an empty array?Method New ()Print( "Initial array length: " + Controls.Length ) ' is already Length 1Controls = Controls.Resize( Controls.Length + 1 )Print( "Length after resize: " Controls.Length ) ' Length 2'Controls[ Controls.Length - 1 ] = controlEndEndI also have a related “best practice” question, but I’ll put that into a different thread.
October 1, 2017 at 1:46 pm #10906to 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]Monkey123[crayon-5cb9b9456676a217870156 inline="true" ]For Local I:int = 0 Until 20controls[I] = New Control()Nextyou can also add them in line like this:
Field controls := new Control[](New Control(),New Control(), New Control() ]
`October 1, 2017 at 1:59 pm #10908Oh! Thanks Jesse. I could’ve sworn that I tried this, but apparently not. And yes, that works well, even with length 0.
October 1, 2017 at 4:10 pm #10912To create a zero length array, use ArrayName:Type[]. Then you can use Resize or New to add to the array
Monkey12345678910111213141516171819202122232425262728293031Namespace myapp#Import "<std>"Using std..Class ControlField Name:StringField Trigger:Bool()Field Callback:Void()EndFunction Main()Local Controls:Control[] '0 length controlsFor Local i:Int = 0 To 3Print ("Length before adding control = "+Controls.Length)Controls = Controls.Resize(i+1)Print ("Length after adding control = "+Controls.Length)Controls[i] = New Control 'add the instance to the arrayControls[i].Name = "Control #"+iNextFor Local control := Eachin ControlsPrint control.NameNextEndOctober 2, 2017 at 6:36 am #10921Local Controls:Control[] ‘0 length controls
really! why it’s not a null object?! magic.
October 2, 2017 at 10:33 am #10930Arrays 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.
Monkey123456789Local IntArray:Int[]If IntArray = Null 'The array is NullPrint "Array is Null"ElsePrint "Array is not Null"End IfPrint "Array length = "+IntArray.Length 'However, it still has a Length property -
AuthorPosts
You must be logged in to reply to this topic.