[Gambas-user] Object array code gives weird results

Timothy Marshal-Nichols timothy.marshal-nichols at ...247...
Fri May 19 12:35:10 CEST 2006


Thanks

8-{)} Timothy Marshal-Nichols
<mailto: timothy.marshal-nichols at ...247...>


> -----Original Message-----
> From: gambas-user-admin at lists.sourceforge.net
> [mailto:gambas-user-admin at lists.sourceforge.net]On Behalf Of Jose J.
> Rodriguez
> Sent: Friday, 19 May 2006 00:12
> To: gambas-user at lists.sourceforge.net
> Subject: [Gambas-user] Object array code gives weird results
>
>
> The weirdest things happen to me in Gambas, lol. This is in 1.9.29
> (patched). From the info and examples I've managed to search out, this
> seems correct and it does work (to a point). I have the following
> module and a Form1 with a TextView1 component. There are 2 classes
> (see at the bottom). The funny thing is that the line:
>
>  Form1.TextView1.Text &= "<br>" & PartInfo[pcounter - 1].Device
>
> works perfectly in the main parsing loop:
>
> /dev/hda1
> /dev/hda2
> /dev/hda4
> /dev/hda5
> /dev/hda6
> /dev/hda7
>
>  but the later loop:
>
>    FOR n = 0 TO PartInfo.Max
>       Form1.TextView1.Text &= "<br>" & PartInfo[n].Device
>    NEXT
>
> just gives this:
>
> /dev/hda7
> /dev/hda7
> /dev/hda7
> /dev/hda7
> /dev/hda7
> /dev/hda7
>
>
> Here's the code, as you will see, only the device is being parsed so
> far, the rest is filler data until someone helps me figure out what is
> wrong ;).
>
> Regards,
> Joe1962
>
>
>
> ' Gambas module file
>
>    'Drive and partition declarations:
>    PUBLIC DiskInfo AS NEW Object[]
>    PUBLIC PartInfo AS NEW Object[]
>
>
> PUBLIC SUB Main()
>    Form1.Show
>    DrivePartInfo
> END
>
>
> PUBLIC SUB DrivePartInfo()
>    DIM sTemp AS String
>
>    'Get available drives and partitions:
>    EXEC ["fdisk", "-l"] TO sTemp
>    Parse_fdisk(sTemp)
>
> END
>
>
> 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
>          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.

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
>
>
>
> CDiskInfo.class:
> ' Gambas class file
> PUBLIC Device AS String
> PUBLIC Bytes AS Long
>
>
>
> CPartInfo.class:
> ' Gambas class file
> PUBLIC Device AS String
> PUBLIC Bootable AS Boolean
> PUBLIC StartBlock AS Long
> PUBLIC EndBlock AS Long
> PUBLIC Blocks AS Long
> PUBLIC ID AS String
>
>
> -------------------------------------------------------
> Using Tomcat but need to do more? Need to support web services, security?
> Get stuff done quickly with pre-integrated technology to make
> your job easier
> Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
> http://sel.as-us.falkag.net/sel?cmd=k&kid0709&bid&3057&dat1642
> _______________________________________________
> Gambas-user mailing list
> Gambas-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user
>






More information about the User mailing list