LB Booster
« Passing Arrays from Inside an Object »

Welcome Guest. Please Login or Register.
Apr 1st, 2018, 04:28am



ATTENTION MEMBERS: Conforums will be closing it doors and discontinuing its service on April 15, 2018.
We apologize Conforums does not have any export functions to migrate data.
Ad-Free has been deactivated. Outstanding Ad-Free credits will be reimbursed to respective payment methods.

Thank you Conforums members.
Speed up Liberty BASIC programs by up to ten times!
Compile Liberty BASIC programs to compact, standalone executables!
Overcome many of Liberty BASIC's bugs and limitations!
LB Booster Resources
LB Booster documentation
LB Booster Home Page
LB Booster technical Wiki
Just BASIC forum
BBC BASIC Home Page
Liberty BASIC forum (the original)

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Passing Arrays from Inside an Object  (Read 533 times)
JosephE
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 35
xx 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 
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx 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.
User IP Logged

JosephE
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 35
xx 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 » User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx 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.
User IP Logged

JosephE
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 35
xx 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?
User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

| |

This forum powered for FREE by Conforums ©
Terms of Service | Privacy Policy | Conforums Support | Parental Controls