• Welcome to Jose's Read Only Forum 2023.
 

trying to allocate structures that contain dynamic arrays (variable length)

Started by Murray Ruggiero, December 27, 2010, 04:37:43 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Murray Ruggiero

I am trying to allocate structures (UDTs) that contain various variables, including members that are arrays whose length I don't know in advance.  To make this even more complicated, the variable length arrays could be arrays of other structures.
I was told that this cannot be done, unless I do one of two things:
1. use pointers and allocate the memory later
2. make the structure into a class.

So I started on the pointer route, but then I thought - "how do I allocate the array memory?".  If I do it with a GlobalMem statement, then I have to lock/unlock and also deallocate eventually.

So I decided to try making the structure into a class.  But it does not compile.  Maybe I am not supposed to return the dynamic array as a property, or maybe there other reasons. 
But here is the code that I tried.  Any help is appreciated.

#COMPILE EXE
#DIM ALL

CLASS MYTYPE_AS_CLASS
  INSTANCE m_Yrs(1980 TO 2010) AS LONG  ' (this causes an error too)
  INSTANCE m_MyDynamicArray() AS STRING ' this causes an error later on when I try and return it as a property
  INSTANCE m_myID AS LONG
    INTERFACE MYTYPE_AS_INTERFACE
    INHERIT IUNKNOWN
    PROPERTY GET MyDynamicArray() AS STRING
       PROPERTY = m_MyDynamicArray
    END PROPERTY
    PROPERTY GET Yrs() AS LONG
       PROPERTY = m_Yrs
    END PROPERTY
    PROPERTY GET myID AS LONG
       PROPERTY = m_myID
    END PROPERTY
  END INTERFACE
END CLASS

FUNCTION PBMAIN () AS LONG
DIM ArrOfClass() AS LOCAL MYTYPE_AS_INTERFACE
LOCAL upperbound AS INTEGER
LOCAL i AS INTEGER

upperbound = 5 ' zero based
FOR i = 0 TO upperbound
    arrofclass(i) = CLASS "MYTYPE_AS_CLASS"
NEXT
END FUNCTION


Theo Gottwald

Murray, sorry to say but your script is full of errors of any kind  :D.
I will help you a bit with the first part.

To transfer the whole array out with a single call,
you could for example unse the JOIN() and PARSE() commands.

Please note that Memeory reservation is not done at compile-time but at runtime,
thats why a dim must be inside the runtime code (PBMAIN or FUNCTION) not in an Definition.


#COMPILE EXE
#DIM ALL

CLASS MYTYPE
  INSTANCE m_Yrs() AS LONG  ' (this causes an error too)
  INSTANCE m_MyDynamicArray() AS STRING ' this causes an error later on when I try and return it as a property
  INSTANCE m_myID AS LONG

     CLASS METHOD CREATE()
     DIM m_Yrs(1980 TO 2010)
     END METHOD
    INTERFACE iMYTYPE
    INHERIT IUNKNOWN
     
    PROPERTY GET MyDynamicArray() AS STRING
       PROPERTY = m_MyDynamicArray(1)
    END PROPERTY
    PROPERTY GET Yrs() AS LONG
       PROPERTY = m_Yrs(1)
    END PROPERTY
    PROPERTY GET myID AS LONG
       PROPERTY = m_myID
    END PROPERTY
  END INTERFACE
END CLASS   

José Roca