![]() ![]() Instead you get a new variable that points to the original array so any changes you make to the "2nd" array also affects the first array. If you are creating Extension methods on Integer you should keep this in mind as you may also want to create Int32 extension methods as well.Īrrays are treated as objects so be aware that assigning an array to another variable or passing an array as a parameter does not make a copy of the array. In addition, although the methods Count, FirstIndex, IndexOf and LastIndex all indicates they return an Integer, they technically return an Int32 (even with 64-bit app). This example reduces the aNames array to 11 elements.Īrrays can have a maximum index value of 2,147,483,646 for both 32-bit and 64-bit apps. When you resize an array to decrease its size, existing values are retained if they are still within the new array bounds.Īrrays can have a maximum index value of 2,147,483,646. When you ResizeTo an array to increase its size, any existing values in the array are retained. You can use the array AddRow or AddAt methods to add rows or you can resize it again to the exact size you need. It is faster to resize an array in this manner than to loop through the array and individually remove each row. When you resize an array to -1, all rows are removed. The number of parameters passed is the number of dimensions of the array being resized. Arrays are zero-based (the first element is zero) so you resize the array using a number that is one less than the number of elements you actually want. ![]() The ResizeTo method is used to increase or reduce the number of rows in the array specified. Resizes the array to the specified index. RemoveAt ( 1 ) // names() = "Bob", "Jane" Var names () As String = Array ( "Bob", "Tom", "Jane" ) names. Reporting bugs and making feature requests.Either way, I hope you found this interesting. The above code is slightly shorter since it doesn’t need to declare the variable, but was this really worth all this this effort just for that? Well, perhaps not, but I think it’s cool and sometimes it’s fun to do stuff just because you can! Plus, it makes for a good excuse to learn about some more advanced Xojo features. With these pieces now in place, this code (which is almost exactly what we wanted above - AddressOf is used to pass a reference of the Display method to the delegate) now works: Var xdcs() As String = Array("Orlando", "Austin", "Houston", _ To work with other array types you’d need to add equivalent delegates and ForEach method overloads. Note: The above method and delegate are designed to work with String arrays. Its code still uses For Each and calls the delegate like this: For Each s As String In stringArray Parameters: Extends stringArray() As String, action As ForEachString.This method also takes the delegate defined above as a parameter. An extension method is a method that is called using dot notation on a specific data type (or class), even though it is not specifically part of the data type. This will be an extension method on a string array. So the Display method could be passed into a parameter of this delegate type. If you look at the Display method above, you’ll see that it matches this delegate’s signature. Here are its specifics:Īny actual method that matches this delegate signature is said to satisfy the delegate. A Delegate is a type that is a method declaration. To set this up, add a Module to your project and add a Delegate to the module. Is that even possible in Xojo? Yes, it mostly is, with a couple things called Extension Methods and Delegates. Here is the code for the Display method: Public Sub Display(it As String)īut what if you wanted to simplify things even further? Say you want to write code like this: xdcs.ForEach(Display) We’re looking forward to XDC London in October 2021. ![]() The Xojo Developer Conference, aka XDC, is the biggest Xojo event of the year. Such a loop might look like this: Var xdcs() As String = Array("Orlando", "Austin", "Houston", _ When looping through an array, I am a big fan of using For Each as I find it more readable than using a For loop with a counter and looking up the item in the array with the counter. ![]()
0 Comments
Leave a Reply. |