OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Arrays inside user-defined types

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
richhill
OOo Advocate
OOo Advocate


Joined: 16 Jun 2004
Posts: 418
Location: Mesa, AZ

PostPosted: Fri Apr 08, 2005 9:42 pm    Post subject: Arrays inside user-defined types Reply with quote

Excel VBA allow arrays in customized data types

Code:
Type MyArray
   id As String
  Comps(50) As Double
End Type


Apparently, OOo does not allow this type of array construct inside a Type.
What is the workaround?
_________________
OOo Calc tips: http://www.openofficetips.com
Back to top
View user's profile Send private message Visit poster's website
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Sat Apr 09, 2005 8:20 am    Post subject: Reply with quote

Copy, Paste and then Run this example. See what happens.
I did this in OOo 1.1.4 on Win XP Pro.
Code:

Type MyDooDad
   cID
   aComps
End Type

Sub Main
   ' Create a MyDooDad, set its ID.
   oMyDooDad1 = MakeMyDooDad( "Fred" )
   ' Now set some of its array elements.
   oMyDooDad1.aComps( 0 ) = "house"
   oMyDooDad1.aComps( 1 ) = "houses"
   oMyDooDad1.aComps( 2 ) = "mouse"
   oMyDooDad1.aComps( 3 ) = "mice"
   
   ' Create a MyDooDad, set its ID.
   oMyDooDad2 = MakeMyDooDad( "Jane" )
   ' Now set some of its array elements.
   oMyDooDad2.aComps( 0 ) = "goose"
   oMyDooDad2.aComps( 1 ) = "geese"
   oMyDooDad2.aComps( 2 ) = "loose"
   oMyDooDad2.aComps( 3 ) = "lease"
   
   PrintADooDad( oMyDooDad1 )
   PrintADooDad( oMyDooDad2 )
End Sub


' This function dynamically creates and returns a MyDooDad.
Function MakeMyDooDad( cID )
   Dim oMyDooDad As MyDooDad
   With oMyDooDad
      .cID = cID
      .aComps = DimArray( 50 )
   End With
   MakeMyDooDad = oMyDooDad
End Function


Sub PrintADooDad( oMyDooDad )
   Print oMyDooDad.cId + " has a " + oMyDooDad.aComps( 0 ) + " with a " + oMyDooDad.aComps( 2 )
End Sub



Comming from a background of using languages like LISP, Python, etc., I prefer the style of using functions that dynamically allocate things rather than statically declare them. Therefore, I never use the Dim statement, unless absolutely necessary. The actual semantics of objects in OOo Basic match my style of use. However, the question of which style to write your code, is purely, a question of style.

The Dim statement only makes you think you are statically allocating an instance with local extent (as opposed to local scope). Hence, the MakeMyDooDad function, while it uses a Dim statement, actually returns the object that it created, thus proving that while the variable name has local scope, the variable is merely a reference to an underlying object that has dynamic extent.



See also....

Structs in StarBasic
http://www.oooforum.org/forum/viewtopic.phtml?t=18948
http://www.oooforum.org/forum/viewtopic.php?t=12032
http://www.oooforum.org/forum/viewtopic.php?t=10967

Array Semantics
http://www.oooforum.org/forum/viewtopic.php?t=4615
http://www.oooforum.org/forum/viewtopic.php?t=4608

Any reason to use Dim statement?
http://www.oooforum.org/forum/viewtopic.php?t=5845
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
richhill
OOo Advocate
OOo Advocate


Joined: 16 Jun 2004
Posts: 418
Location: Mesa, AZ

PostPosted: Sat Apr 09, 2005 10:25 am    Post subject: Reply with quote

DannyB,

I thank you for the detailed answer to my question. I'll respond later today - as I have also been doing some experimentation of my own. It is all related to developing a OOo Calc Add-In for handing large numbers.

right now - I'm doing a makeover of my daughter's bedroom Laughing

All the best!

Dave (richhill)
_________________
OOo Calc tips: http://www.openofficetips.com
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group