LB Booster
« Passing Arrays from Inside an Object »

Welcome Guest. Please Login or Register.
Apr 1st, 2018, 05:09am



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 537 times)
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