How to create objects dynamically


Sometimes you don't know how much objects the program will need, or you have to create a lot of objects.
Than it's easy to put them into an array.

pic.png

' Gambas class file
PRIVATE karren AS Object[]

PUBLIC SUB _new()
  DIM i AS Integer
  DIM tmpcar AS CCar
  karren = NEW Object[]
  FOR i = 0 TO 3
    tmpcar = NEW CCar(Str(i))
      'Str(i): See Constructor of the CCar.class
      'this sets the Brand of the Car
      'and it has to be a String
    karren.Add(tmpcar)
  NEXT
END

PUBLIC SUB Button1_Click()
    Label1.Text = karren[0].getbrand()
END
PUBLIC SUB Button2_Click()
    Label1.Text = karren[1].getbrand()
END
PUBLIC SUB Button3_Click()
    Label1.Text = karren[2].getbrand()
END
PUBLIC SUB Button4_Click()
    Label1.Text = karren[3].getbrand()
END

OopCcar

-- JochenGeorges - 04 Jan 2005