[Gambas-user] Object array code gives weird results
ron
ronstk at ...239...
Fri May 26 06:29:56 CEST 2006
On Friday 19 May 2006 12:35, Timothy Marshal-Nichols wrote:
> > PUBLIC SUB Parse_fdisk(sTemp AS String)
> > DIM vDI AS NEW CDiskInfo
> > DIM vPI AS NEW CPartInfo
> > DIM arrTemp1 AS NEW String[]
> > DIM arrTemp2 AS NEW String[]
> > DIM pos AS Long
> > DIM n AS Long
> > DIM pcounter AS Long
> >
> > arrTemp1 = Split(sTemp, Chr$(10))
> >
> > FOR n = 0 TO arrTemp1.Max
> > pos = InStr(arrTemp1[n], "/dev/")
> > IF pos = 1 'Parse a partition.
> > arrTemp2 = Split(arrTemp1[n], Space$(1), "", TRUE)
> > INC pcounter
see next below
> > vPI.Device = arrTemp2[0]
> > vPI.Bootable = FALSE
> > vPI.StartBlock = 0
> > vPI.EndBlock = 1024
> > vPI.Blocks = 25
> > vPI.ID = "06"
> > PartInfo.Push(vPI)
>
> On the above line you a pushing an object. So at the end of the loop
> all items in the array are set to the same object.
----------------------------------------^^^^^^^^^^^^
>
Here you add a vPI object to the PartInfo.
Because it should be every time a new fresh vPI object you need to
create the new object.
Add before using vPI for *new* info storage a *new* instance of it
vPI = New CPartInfo
You are now using all the time the same and only 1 vPI object !!
> To overcome the problem push the value of the object or create a new object
> with the value you want.
>
> > Form1.TextView1.Text &= "<br>" & PartInfo[pcounter - 1].Device
> > END IF
> > NEXT
> >
> > FOR n = 0 TO PartInfo.Max
>
> So on the line below you are always looking the same object. You
--------------------------------------------------^^^^^^^^^^^^
> always get the value you last set this object to.
---------------------------^^^^^^^^^^^^^^^^^^^^
>
> You might not like this behaviour. But it is the same as in most
> programming languages with objects.
>
> > Form1.TextView1.Text &= "<br>" & PartInfo[n].Device
> > NEXT
> >
> > END
Ron
More information about the User
mailing list