On May 3, 2006, at 4:59 PM, [EMAIL PROTECTED] wrote:
On May 03, 2006, at 22:27 UTC, Jason Essington wrote:
In fact, kudos for boiling the array example down to this more
simple one, which really gets to the heart of the matter: a
reference to a subclass type is NOT a reference to the superclass
type. Arrays are just another way of passing around references.
O.K. this is where the real difference lies ... in REALbasic, the
array is passes by reference (by default) however in java the
reference is passed by value (your head spinning yet?).
Hmm, I'm not sure I agree. I'm not an expert in Java, but your
description of RB isn't quite accurate. The array reference is
passed by value, but that's not the issue here anyway -- the issue
is that an array *contains* object references. I'm pretty sure the
same would be true of a Java array.
Actually that an array contains object references isn't a problem at
all, that's what we're after, we want to manipulate those reference
values.
So the solution is simply to rewrite our ArrayCopy method to look
something like:
sub ArrayCopy(ByVal srcArray() as Object, ByVal destArray() as
Object)
No, ByVal is always the default for parameters.
You may be right about this one ...
if it is not byref, then it is fine to pass an array of apples to a
method that wants an array of fruit, the same way it is fine to pass
an apple to a method that has a fruit parameter.
This is no different from what we had before.
destArray(i) = srcArray(i).clone()
And here is where the problem is, because destArray(i) is a
reference of type Object (as is srcArray(i) of course). So we
can't pass in some array that doesn't contain references of type
Object, but instead contains, say, references of type Date.
Yeah, this was an ill thought out example, there's a bit of an issue
with using Object, and I didn't do the whole cloneable thing right,
but going back to our fruit example and assuming that we have a clone
method in fruit, that we override in apple that will create a proper
clone ....
sub copyArray(ByVal srcArray As Fruit, ByVal destArray As Fruit)
dim n As Integer = ubound(srcArray)
redim destArray(n)
for i As Integer = 0 to n
destArray(i) = srcArray(i).clone()
next
break
// this will pitch an exception
//destArray(0) = new Orange()
end sub
then somewhere else:
Dim apples(15) As Apple
Dim moreApples(-1) As Apple
for i as integer = 0 to 15
apples(i) = new Apple()
apples(i).index = i
next
copyArray(apples, moreApples)
break
for each apple as Apple in apples
apple.grade = "Wowee!"
next
// at this point we have moreApples which holds the original state
of apples
// and apples which now has a barrel of Wowee! graded apples :-)
break
So, my original suggestion is Possible in RB!!! you just have to
declare the parameters ByVal, and be sure that the destination array
is at least big enough when you pass it in, and everything works as
expected.
Alas, no.
What part is missing?
I'll send you the functioning sample project :-)
-jason
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>
Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>