Just for the fun of it, instead of using Dictionary, I want to make a method which mimics Java’s Array which accepts other arrays as items.
I want it to create some array properties inside a module, and fill them with the given values.
Also I want to optionally flush the array properties (or better, delete them from the module. is there any difference between deleting and flushing in regards to memory allocation?) when it is not needed anymore, to simulate local variables’ habit of self destruction.
BTW, this kind of array will only store strings.
The said method would be like this:
sub SetArrayOfArrays(ArName as string, Length as integer, Info() as string)
// Info's items must be in this format:
// "one,two,three,four,...."
// Each Info item will become a separate array.
for each item as string in Info
for each word as string in split(item, ",")
for i as integer = 1 to Length
// create array property under myModule
// name it as ArName + "_" + str(i)
// store word's value inside it
next
next
next
end sub
And the method for accessing them would be like:
sub GetArrayOfArrays(ArName as string, PrimeIndex as integer, SecondIndex as integer) as string
dim target as string
// code for combining ArName and str(PrimeIndex) to refer to ArName_PrimeIndex()
target = ArName_PrimeIndex(SecondIndex)
return target
end sub
Please tell me how to write the code for the commented sections.
Append this class to the Array that had the same type as this class.
awesomeArray.Append awesomeStorageClass
In the end you will have an Array of all these classes with storage capabilities and as you can see with my Array as a property of the class you can still gain a hierarchical array if you wish.
IMO this scales well in an OOP environment such as Xojo.
HTH and I am sure others will add to this:
Redim awesomeArray(-1)
Then to remove the elements of the array you could
Thank you Mike and Kem!
Mike what you described solves all of the problems I was facing.
I avoided Dictionary because I was tired of getting KeyNotFoundException.
So Instead I build the required number of array properties under a module, to do my job.
But each time I wanted to do something to them, I had to copy my code 36 times and manually write the names of all 36 array properties.
[quote=164628:@Kem Tekinay]Mike has provided the best answer. Build classes to store your data.
You can also emulate something like what you described through the Operator_Lookup method of a class, even if this is not typically recommended.
But you cannot create nor destroy properties of a module or class at runtime.[/quote]
What Mike posted solves all my problems, but I still like to know how to iterate through a series of similarly named objects in a For loop by writing the constant part of the series name and appending the instance number at the end of it.
Hopefully this is what you mean and using my example from above. Im not 100% sure what you mean on your last sentence, but hopefully you can adapt it.
FYI: I use the IndexID property of the class as my ID and/or a “isSelected” boolean so I always know which class is being used.
Dim i as integer
for i = 0 to awesomeArray.Ubound
if awesomeArray(i).Name = "theName" Then
// DO SOMETHING SPECIAL
// MARK AS SELECTED
awesomeArray.isSelected = True
Else
awesomeArray.isSelected = False
End if
next i
[quote=164637:@Mike Cotrone]Hopefully this is what you mean and using my example from above. Im not 100% sure what you mean on your last sentence, but hopefully you can adapt it.
FYI: I use the IndexID property of the class as my ID and/or a “isSelected” boolean so I always know which class is being used.
I tend to not use for each unless I don’t care about guaranteed search order.
[code]
Dim i as integer
for i = 0 to awesomeArray.Ubound
if awesomeArray(i).Name = “theName” Then
// DO SOMETHING SPECIAL
// MARK AS SELECTED
awesomeArray.isSelected = True
End if
next i
[/code][/quote]
Here’s what I meant:
Let’s say I have 40 PushButtons named myButton1, myButton2, myButton3, … myButton39, myButton40.
And I want to execute an action on every single one of them.
I want to know how can I accomplish this easily, WITHOUT first appending them inside an array to refer to them by index in a for loop.
(BTW, appending these 40 buttons to an array requires writing the names of all of them, so it kinda defeats the purpose)
I don’t use these too much, but what I do know is that the control set must encompass the SAME controls (can’t have different controls in the set which would be the same as an Array that has a a specific control class type).
[quote=164650:@Radium Radiovich]Ah I forgot to mention WITHOUT using control sets
What I really want is to know how to combine two strings to refer to an object/variable.[/quote]
If you create a container control with one button on it and set up the button how you want… Then you can Dynamically instantiate a container control (button) append to array in a loop fashion which would not require you to type out 40 names.
I do this commonly when I am presenting the UI dynamic graphs.
Also, the controls are always in a sort of array (maybe a true array? someone more knowledgeable can clarify that) which can be looped through. Check one of the examples of the IsA operator (http://documentation.xojo.com/index.php/IsA), modified here to be used with buttons:
Dim i as Integer
For i=0 to Self.ControlCount-1
If Self.Control(i) IsA PushButton then
if PushButton(Self.Control(i)).name=TheName then
'do something
End if
End If
Next
This code is intended to be used to loop through the controls present in a window (Self). I haven’t tested the code and I am not sure it is correct because I have never done this, but the concept is correct.
Append this class to the Array that had the same type as this class.
awesomeArray.Append awesomeStorageClass
In the end you will have an Array of all these classes with storage capabilities and as you can see with my Array as a property of the class you can still gain a hierarchical array if you wish.
IMO this scales well in an OOP environment such as Xojo.
HTH and I am sure others will add to this:
Redim awesomeArray(-1)
Then to remove the elements of the array you could[/quote]
Excuse me Mike, which parts must be done in code editor, and which by GUI?
And for parts which need to be done in code editor, where do I have to insert the codes? in open event of app?