I’m trying to use the Extends keyword to add functionality to an array. The Xojo docs indicate that it can be done, but don’t supply a specific example. I want to append the contents of a one-dimensional array to another.
AppendArray(Extends a() as Variant, items() as Variant)
Usage:
[code]dim ar() as MyClassData
dim ap() as MyClassData
// …fill both arrays with data
ar.ArrayAppend(ap())[/code]
This does not work because Xojo doesn’t recognise the keyword “AppendArray”
Both arrays of course will contain the same data type, and I check if the array itself is a nil object, which can occur in my app.
a() as Variant is an array of variants which is a very specific type of array; ar() as MyClassData is not of this type as MyClassData is not the same type as Variant. While a variable of type Variant could hold an instance of MyClassData they are not the same data type.
[code]Module Module1
Sub AppendArray(extends arr1() As Object, arr2() As Object)
for i As integer = 0 to arr2.Ubound
arr1.Append arr2(i)
next
End Sub
End Module
Sub Action() //Pushbutton1
dim arr1(), arr2() As REALbasic.Point
arr1.Append new REALbasic.Point(1, 1)
arr2.Append new REALbasic.Point(2, 2)
Thanks Norman…shame I won’t be able to use it because of a Microsoft redistributable conflict with Xojo >2013r3.3 & msvcp120.dll & msvcr120.dll and my current Valentina license.
[quote=58490:@Grant Singleton]Thanks Norman…shame I won’t be able to use it because of a Microsoft redistributable conflict with Xojo >2013r3.3 & msvcp120.dll & msvcr120.dll and my current Valentina license.
[/quote]
What conflict ?
My Valentina license only goes to version 5.4.3. It expects msvcp100.dll & msvcr100.dll when Xojo >2013r3.3 is distributing msvcp120.dll & msvcr120.dll. When I include both versions my Windows app hangs. I’ve checked my Valentina dlls with a hex editor and they all expect msvcp100.dll & msvcr100.dll.
I’m certainly not paying $US900.00 for a Valentina license renewal so I’ll have to wait for (hopefully) the next Omegabundle. In the meantime I’m stuck at Xojo 2013r3.3 which still distributes msvcp100.dll & msvcr100.dll.
This gave me an interesting idea to implement something similar to linq in c#.
For example in c# I can say
var friends20YearsOld = friends.Where(x => x.Age = 20);
In Xojo I created something close
dim l() as REALbasic.Point
For i as integer = 0 to 50
dim p as new REALbasic.Point(i mod 2, i mod 2)
l.Append( p)
Next
dim j() as Object= l.Where("X","=", 0)[/code]
j will then be an array of Realbasic.Points where x = 0
This happens to be 25 of the 51 points in the array.
The where function uses introspection to determine if the condition is true.
The problem is that the where function returns an Array of Objects instead an Array of "whatever was passed."
[b]Is there a way to have the method return the dynamic type you want?
Or is there an easy way to cast an entire array of Objects into an array of the type you want?[/b]
I need to do more work on the "Where" method to include more operators but here is the code:
[code]Function Where(extends o() as Object, field as string, operator as string, value as variant) As Object()
dim r() as Object
For i as integer = 0 to o.Ubound
if o(i).introspectProperty(field) = value then
r.Append(o(i))
end
Next
return r
End Function
Function introspectProperty(extends wv as Object, propertyName as string, altValue as variant = nil) As variant
dim mProperties() as Introspection.PropertyInfo = Introspection.GetType(wv).getProperties
For j as integer = 0 to ubound(mProperties)
if mProperties(j).name = propertyName then
return mProperties(j).Value(wv)
end
Next
return altValue
End Function
Eventually you could do something like
dim j() as Object= l.Where(“X”,">", 2).Where(“Y”,"<",20).Where(“Y”,">",-10).OrderBy(“X”).Limit(5)[/code]
I know I love how convenience this is in c#
Sub downcast(origArr() As Object, newArr() As Object)
dim last As integer = origArr.Ubound
redim newArr(last)
for i As integer = 0 to last
newArr(i) = origArr(i)
next
End Sub
example use for your example…
[code]dim j() as Object= l.Where(“X”,"=", 0)
dim jAsPoint() As REALbasic.Point
downcast(j, jAsPoint)
//or
dim j() As REALbasic.Point
downcast(l.Where(“X”,"=", 0), j)
//or maybe this can be built into your framework for something like
dim j() as REALbasic.Point
l.Where(“X”,">", 2).Where(“Y”,"<",20).Where(“Y”,">",-10).OrderBy(“X”).Limit(5).DownCast(j)[/code]
But I’m a bit troubled by the syntax and I’m not sure if I should do it this way
Instead of
x = 2
it is actually more like
2 = x
which is kind of goofy.
Can anyone think of a way to reverse how this works?
In otherwords so
Sub Selection(extends o2() as object, assigns o() as object)
dim last As integer = o2.Ubound
redim o(last)
for i As integer = 0 to last
o(i) = o2(i)
next
End Sub
Anyone know if you can implement something like operator_assigns or operator_assignsRight like how you implement operator_compare etc…
[quote=59107:@Brock Nash]
Anyone know if you can implement something like operator_assigns or operator_assignsRight like how you implement operator_compare etc…[/quote]
Not using extends