Author |
Topic: Passing Arrays from Inside an Object (Read 532 times) |
|
JosephE
New Member
member is offline
Gender:
Posts: 35
|
|
Passing Arrays from Inside an Object
« Thread started on: Mar 1st, 2015, 6:33pm » |
|
I'm getting errors with this code. If you run it, you'll see exactly why.
My only guess is that you can't pass instance arrays to a function?
Code:new list as ScalableData
for i = 1 to 100
print "adding item: "; i
call list::add i
next i
class ScalableData
dim storage(10)
dim expandBy
dim length
dim amount
sub ScalableData
expandBy = 10
length = 0
amount = 10
end sub
sub add item
length = length + 1
if length > amount then
amount = amount + expandBy
call preserve storage(), amount
end if
storage(length) = item
end sub
end class
function nsize(array())
' Returns the upper index limit of the numeric array array().
s = 2^24
do
on error goto [Error]
[Error]
s = int(s / 2)
temp = array(nsize + s)
nsize = nsize + s
loop until s = 0
end function
sub preserve byref array(), newSize
' Redimensions the given one-dimensional numeric array to the
' specified newSize.
u = nsize(array()) ' Get the upper limit
redim ArrayLib(u)
for i = 0 To u
ArrayLib(i) = array(i) ' Get old contents
next i
redim array(newSize) ' Resize given array.
for i = 0 To u
array(i) = ArrayLib(i) ' Replace old contents
next i
end sub
|
|
Logged
|
|
|
|
Richard Russell
Administrator
member is offline
Posts: 1348
|
|
Re: Passing Arrays from Inside an Object
« Reply #1 on: Mar 1st, 2015, 7:11pm » |
|
on Mar 1st, 2015, 6:33pm, JosephE wrote:My only guess is that you can't pass instance arrays to a function? |
|
You certainly can't! If you pass an array to a 'conventional' function, it must be a 'conventional' array. That is fundamental (you can't do it in BBC BASIC either).
Richard.
|
|
Logged
|
|
|
|
JosephE
New Member
member is offline
Gender:
Posts: 35
|
|
Re: Passing Arrays from Inside an Object
« Reply #2 on: Mar 1st, 2015, 7:49pm » |
|
Okay, that makes sense.
How about this? Why doesn't this work? As you can see, I really want a scaling list data type.
Code:new list as List
for i = 1 to 100
print "adding item: "; i
call list::add i
next i
class List
dim temp(1)
dim storage(10)
dim expandBy
dim size
dim amount
sub List
expandBy = 10
size = 0
amount = 10
end sub
sub add item
size = size + 1
if size > amount then
amount = amount + expandBy
call this::expand amount
end if
storage(size) = item
end sub
private sub expand newSize
redim temp(size)
for i = 0 to size
temp(i) = storage(i)
next i
redim storage(newSize)
for i = 0 to size
storage(i) = temp(i)
next i
redim temp(1)
end sub
end class
Edit:
Even with try/catch it isn't catching it! (swap the sub in below) Code: private sub expand newSize
try
redim temp(size)
for i = 0 to size
temp(i) = storage(i)
next i
redim storage(newSize)
for i = 0 to size
storage(i) = temp(i)
next i
redim temp(1)
catch
print err$
end try
end sub
|
« Last Edit: Mar 1st, 2015, 8:48pm by JosephE » |
Logged
|
|
|
|
Richard Russell
Administrator
member is offline
Posts: 1348
|
|
Re: Passing Arrays from Inside an Object
« Reply #3 on: Mar 1st, 2015, 9:16pm » |
|
on Mar 1st, 2015, 7:49pm, JosephE wrote:How about this? Why doesn't this work? |
|
The same reason. Class 'arrays' aren't conventional arrays: just as you can't pass them as parameters you can't REDIM them either. There is no class property which is 'resizable'.
If you think about it you shouldn't be surprised. Each instance of the class (each 'object') has its own private array; if you were allowed to do what you want to do you, different instances of the class could end up having different sized arrays! It simply can't work.
If there was only ever one instance of a class, the 'object' and the 'class' would be the same thing. Then everything would be much simpler: object/class properties could be conventional variables, arrays etc. But then it wouldn't be Object Oriented Programming in the usual sense!
The only way it can work is for the size of the array to be determined when the class is defined, then every instance of the class has the same-sized array.
Richard.
|
|
Logged
|
|
|
|
JosephE
New Member
member is offline
Gender:
Posts: 35
|
|
Re: Passing Arrays from Inside an Object
« Reply #4 on: Mar 1st, 2015, 10:21pm » |
|
Okay, that being said, is there any way I can cheat around that so I can have dynamically expanding lists?
|
|
Logged
|
|
|
|
|