| View previous topic :: View next topic |
| Author |
Message |
m2mike General User

Joined: 20 Nov 2005 Posts: 8
|
Posted: Mon Nov 21, 2005 6:57 pm Post subject: The Collection data type |
|
|
I would like to try to declare a collection like this:
| Code: |
Dim Col As Collection |
This does not seem to be allowed though. When I try to compile, it tells me that a collection is an unknown data type.
This is also not recognized by the editor as the word, "Collection" remains green instead of turning blue.
| Code: |
Set Col = New Collection
|
This seems strange. I don't have working code yet. Is there a way around it? Is there an equivalent data type that can be used? I am used to MS. Thank you. |
|
| Back to top |
|
 |
B Marcelly Super User

Joined: 12 May 2004 Posts: 1414 Location: France
|
Posted: Tue Nov 22, 2005 11:33 am Post subject: |
|
|
| Code: | | This seems strange. |
Not really. You are now with OpenOffice.org Basic and API. This is not Microsoft VBA.
Read document StarOffice 7 Software Basic Programmer's Guide. There is a link to it at http://api.openoffice.org/TipsAndTricks/external.html
________
Bernard |
|
| Back to top |
|
 |
m2mike General User

Joined: 20 Nov 2005 Posts: 8
|
Posted: Tue Nov 22, 2005 3:11 pm Post subject: |
|
|
I did a search through the StarOffice 7 guide for the word, "collection" and found nothing. Is there anywhere else I can look?
I am trying to get this code to work. I think it works in Microsoft VBA, but it will not work in OpenOffice.
This is simply a macro that will generate a random 4 digit number and then prinit it on a line. I would like no number to be repeated. The code doesn't work yet. The compiler does not like this line: fourdigitnumber = Format(Ar, "0000").
See this thread: http://www.oooforum.org/forum/viewtopic.phtml?t=27471
Does StarOffice 7 have an online reference like java does? Can I lookup classes and methods somewhere? Any help is appreciated.
| Code: |
Sub FourDigit_Generator
oDoc = thisComponent
oText = oDoc.getText
oVC = oDoc.CurrentController.getViewCursor
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
rem Dim Col As Collection 'The collection we will use to store the numbers
Dim Ar(0 To 9999) As Integer 'The array to store the values from the collection
Dim i As Integer 'Counter for loops
Dim X As Integer 'Variable to store the random generated number
Dim fourdigitnumber As String
Randomize 'Just once to ensure that we get random values
Set Col = New Collection 'Get the collection ready to use
For i = 0 To 9999 'The possible numbers that we can have as a result is all the numbers from 1 to 100 so
Col.Add i 'add all the possible numbers to the collection
Next
For i = 0 To 9999 'Now to get the 100 numbers we added in the previous loop
X = RandomInteger(1, Col.Count) 'Get a random item from the collection (that exists for sure)
Ar(i) = Col.Item(X) 'Add it to the array
Col.Remove X 'Remove it so we don't add it again
fourdigitnumber = Format(Ar, "0000")
oText.insertString(oVC,fourdigitnumber,false)
rem oVC.goDown(1,false)
dispatcher.executeDispatch(document, ".uno:InsertPara", "", 0, Array())
Next
rem ShowInTextBox Text1, Ar 'Just to print the data and see it
End Sub
Private Function RandomInteger(Lowerbound As Integer, Upperbound As Integer) As Integer 'The random number generator code
RandomInteger = Int((Upperbound - Lowerbound + 1) * Rnd + Lowerbound)
End Function
|
|
|
| Back to top |
|
 |
pitonyak Administrator


Joined: 09 Mar 2004 Posts: 3618 Location: Columbus, Ohio, USA
|
Posted: Tue Nov 22, 2005 7:31 pm Post subject: |
|
|
The included help discusses a lot of the syntax and such. I also cover a lot of it in my books and some small subset of it in my free document. Marcelly has an excellent book if you can read French.
You have several issues going on here. you mention that this does not work:
| Code: | | fourdigitnumber = Format(Ar, "0000"). |
the following macro should convince y ou that the Format function works
| Code: | Sub Main
Print Format(3, "0000")
End Sub |
The problem is that Ar is an array and you can not print an entire array.
Now, for the collection. A collection data type is NOT included directly with OOo so you can not use it; sorry. You could, however create a similar program
| Code: | Option Explicit
Sub FourDigit_Generator()
Dim oDoc
Dim oText
Dim oVC
oDoc = thisComponent
'?? oText = oDoc.getText
oVC = oDoc.CurrentController.getViewCursor
REM This is safer
oText = oVC.getText()
'?? document = ThisComponent.CurrentController.Frame
'?? dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
rem Dim Col As Collection 'The collection we will use to store the numbers
Dim Ar(0 To 9999) As Integer 'The array to store the values from the collection
Dim i As Integer 'Counter for loops
Dim X As Integer 'Variable to store the random generated number
Dim fourdigitnumber As String
Randomize 'Just once to ensure that we get random values
' Set Col = New Collection 'Get the collection ready to use
'The possible numbers that we can have as a result is all the numbers from 0 to 9999 so
For i = 0 To 9999
Ar(i) = i
' Col.Add i 'add all the possible numbers to the collection
Next
Dim iTemp
For i = 0 To 9999
X = RandomInteger(0, 9999)
iTemp = Ar(i)
Ar(i) = Ar(X)
Ar(x) = iTemp
Next
For i = 0 To 9999 'Now to get the 100 numbers we added in the previous loop
' X = RandomInteger(1, Col.Count) 'Get a random item from the collection (that exists for sure)
' Ar(i) = Col.Item(X) 'Add it to the array
' Col.Remove X 'Remove it so we don't add it again
' fourdigitnumber = Format(Ar, "0000")
' oText.insertString(oVC,fourdigitnumber,false)
oText.insertString(oVC,Format(Ar(i), "0000"), false)
oText.insertControlCharacter(oVC, com.sun.star.text.ControlCharacter.LINE_BREAK, False)
rem oVC.goDown(1,false)
' dispatcher.executeDispatch(document, ".uno:InsertPara", "", 0, Array())
Next
rem ShowInTextBox Text1, Ar 'Just to print the data and see it
End Sub
Private Function RandomInteger(Lowerbound As Integer, Upperbound As Integer) As Integer 'The random number generator code
RandomInteger = Int((Upperbound - Lowerbound + 1) * Rnd + Lowerbound)
End Function |
_________________ --
Andrew Pitonyak
http://www.pitonyak.org/oo.php |
|
| Back to top |
|
 |
|
|
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
|