array in class

Started by Marco Ruiz, June 30, 2011, 05:44:03 PM

Previous topic - Next topic

Marco Ruiz

this is an attempt to use arrays in a class. The program works well.
I have a concern if ReDim detail () in asignaDetalle method is right or can generate a memory error.
Someone can tell me if this is right or how I can improve?
Is Ok?.


#compile exe
#dim all
#include "win32api.inc"

MACRO PropGet(PropName,PropType)=PROPERTY GET PropName() AS PropType:PROPERTY=PropName:END PROPERTY
MACRO PropSet(PropName,PropType)=PROPERTY SET PropName(BYVAL param AS PropType):PropName=param:END PROPERTY

macro crear_objeto(obj,iClase,cClase)
   dim obj as iClase
   let obj = class cClase
end macro

class c_detalle_documento
   instance codigo as string
   instance descripcion as string
   instance cantidad as long
   instance unidad as string

   interface i_detalle_documento
      inherit iunknown
     
      propGet(codigo,string)
      propSet(codigo,string)
      propGet(descripcion,string)
      propSet(descripcion,string)
      propGet(cantidad,long)
      propSet(cantidad,long)
      propGet(unidad,string)
      propSet(unidad,string)
     
   end interface
end class

class c_documento
   instance numero as string
   instance fecha  as string
   instance cantdet as long
   instance detalle() as i_detalle_documento
   
   interface i_documento
      inherit iunknown
     
      propGet(numero,string)
      propSet(numero,string)
      propGet(fecha,string)
      propSet(fecha,string)
      propGet(cantdet,long)
      propSet(cantdet,long)

      PROPERTY GET detalle(byval i as long) AS i_detalle_documento:PROPERTY=detalle(i):END PROPERTY
      PROPERTY SET detalle(byval i as long,o as i_detalle_documento):detalle(i)=o:END PROPERTY
     
      method asignaDetalle(byval cantdet as long) as long
         local i as long
         
         redim .detalle(1 to cantdet)
         for i = 1 to cantdet
            let detalle(i) = class "c_detalle_documento"
         next i
         method = %TRUE
      end method
     
      method Leer() as long
     
         numero = "001-0000002"
         fecha  = "01/07/2011"
         cantdet= 2
         me.asignaDetalle(2)
         
         detalle(1).codigo      = "200101006"
         detalle(1).descripcion = "producto 6"
         detalle(1).cantidad    = 10
         detalle(1).unidad      = "UNID"

         detalle(2).codigo      = "200101007"
         detalle(2).descripcion = "producto 7"
         detalle(2).cantidad    = 4
         detalle(2).unidad      = "UNID"
      end method
     
   end interface
end class

function pbmain() as long

   crear_objeto(oDoc,i_documento,"c_documento")
   
   oDoc.numero = "001-0000001"
   oDoc.fecha  = "01/07/2011"
   oDoc.cantdet= 5
   oDoc.AsignaDetalle(5)
   
   oDoc.detalle(1).codigo      = "200101001"
   oDoc.detalle(1).descripcion = "producto 1"
   oDoc.detalle(1).cantidad    = 100
   oDoc.detalle(1).unidad      = "UNID"

   oDoc.detalle(2).codigo      = "200101002"
   oDoc.detalle(2).descripcion = "producto 2"
   oDoc.detalle(2).cantidad    = 50
   oDoc.detalle(2).unidad      = "UNID"

   oDoc.detalle(3).codigo      = "200101003"
   oDoc.detalle(3).descripcion = "producto 3"
   oDoc.detalle(3).cantidad    = 50
   oDoc.detalle(3).unidad      = "UNID"

   oDoc.detalle(4).codigo      = "200101004"
   oDoc.detalle(4).descripcion = "producto 4"
   oDoc.detalle(4).cantidad    = 50
   oDoc.detalle(4).unidad      = "UNID"

   oDoc.detalle(5).codigo      = "200101005"
   oDoc.detalle(5).descripcion = "producto 5"
   oDoc.detalle(5).cantidad    = 50
   oDoc.detalle(5).unidad      = "UNID"
   
   msgbox "código      : " & oDoc.detalle(1).codigo      & $CRLF & _
          "descripción : " & oDoc.detalle(1).descripcion & $CRLF & _
          "código      : " & oDoc.detalle(2).codigo      & $CRLF & _
          "descripción : " & oDoc.detalle(2).descripcion

   oDoc.Leer()
   msgbox "código      : " & oDoc.detalle(1).codigo      & $CRLF & _
          "descripción : " & oDoc.detalle(1).descripcion & $CRLF & _
          "código      : " & oDoc.detalle(2).codigo      & $CRLF & _
          "descripción : " & oDoc.detalle(2).descripcion
         
end function