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

Jim's StarOffice Basic Syntax Tips (a series)...

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
jbotte
Power User
Power User


Joined: 27 Jan 2004
Posts: 57
Location: NC, USA

PostPosted: Wed Feb 04, 2004 1:34 pm    Post subject: Jim's StarOffice Basic Syntax Tips (a series)... Reply with quote

I would like to post an ongoing stream of bits and pieces of information on how to start using StarOffice Basic. Since I'm learning by embarking upon the development of a major chunk of enterprise software (nothing like trial by fire...), I expect I will be running into all the problems that other newbies like myself are going to encounter (OOo newbie... otherwise 25 years of software development in C/C++/Tcl/Basic/Assembler/Forth/etc. in large and small environments...).

If there are no objections, I will start posting them tomorrow (I already have 6 written up that I've distributed internally at our company). FYI, my plan would be to reply to this thread and not start a new topic for each...
Back to top
View user's profile Send private message
jbotte
Power User
Power User


Joined: 27 Jan 2004
Posts: 57
Location: NC, USA

PostPosted: Thu Feb 12, 2004 12:13 pm    Post subject: Newbie jbotte's Basic Syntax Tip #1 -- Dim statements Reply with quote

If you have anything to add (or subtract), please feel free to reply to this,
please just use the Subject
Re: Basic Syntax Tip #1 -- <your summary>,
so it's easy to follow related concepts.


When I was learning, I saw code snippets that said it was perfectly reasonable to specify the following:
Code:
Dim sString1, sString2, sString3 As String  ' This is okay... sort of, see below...

However, the following did NOT work when I tried to do it:
Code:
Dim aStruct1, aStruct2 As New com.sun.star.util.URL  ' DOES NOT WORK!!!

I found that I had to use:
Code:
' This is a correct way to instantiate New structures
Dim aStruct1 As New com.sun.star.util.URL
Dim aStruct2 As New com.sun.star.util.URL

But what I just found out recently reading the Developer's Guide (a two line code snippet in a larger example in section 3.4.3 OpenOffice.org Basic->Mapping of UNO and Basic Types->Mapping of Simple Types... it also has a good table showing the min and max values of the various numeric types), was that if you had code like the following:
Code:
Dim sString1, sString2 As String  ' Works, but probably NOT what you wanted!

it declares sString2 as type String but declares sString1 as type Variant, which was probably not what was expected. To do multiple declarations per line, you have to do something like:
Code:
Dim sString1 As String, sString2 As String

So the proper way to have declared multiple New Uno objects was to use a statement like:
Code:
Dim aStruct1 As New com.sun.star.util.URL, aStruct2 As New com.sun.star.util.URL

... probably just as easy to use seperate lines, but it makes it clearer how the Dim statement actually works.

The following is the code snippet referred to above from the Developer's Guide updated to include all data types available and all postfix shortcuts:
Code:
Dim vA, vB           ' Type of vA and vB are Variant
Dim vC As Variant    ' Type of vC is Variant
Dim oO As Object     ' Type of oO is Object
Dim nI As Integer    ' Type of nI is Integer (16 bits)
Dim nL As Long       ' Type of nL is Long (32 bits)
Dim sS As String     ' Type of sS is String
Dim dD As Date       ' Type of dD is Date
Dim cM As Currency   ' Type of cM is Currency
Dim fP As Single     ' Type o p is Single
' *** The type only refers to the preceding variable
Dim fF, fG As Double ' ATTENTION! Type of fF is Variant!
                     '   Only the type of fG is Double

' Usage of Postfixes
Dim nI%              ' is the same as Dim nI As Integer
Dim nL&              ' is the same as Dim nL As Long
Dim fP!              ' is the same as Dim fP As Single
Dim fD#              ' is the same as Dim fD as Double
Dim sS$              ' is the same as Dim sS As String
Dim cM@              ' is the same as Dim cM As Currency

' Note: When using variable names declared with postfixes, you need to
'   leave the postfix on when using the variables, for example:
For nI% = 0 to 10
    ' Some useful code...
Next nI%

There is also a Basic type called "Any" that the developer's guide doesn't give any hint as to what it is (or could be) used for, although the StarOffice Programmer's Tutorial lumps Variant and Any into the same category, so there may be no difference (buyer beware!). There is also the fact that the UNO type "type" maps to the Basic construct "com.sun.star.reflection.XIdlClass" (I haven't needed to use this yet, so I haven't really explored it). The Developer's Guide->3.4.3 OpenOffice.org Basic->Mapping of UNO and Basic Types->Mapping of Simple Types also mentions that there are two mappings from UNO to Basic that exist that are "only used internally": UNO char to Basic Char and UNO void to Basic Void. Again, there's no mention as to why these mappings exist and whether there's anything we need to know about them (best to avoid unless you really know what you're doing!).

There is another mysterious warning in the Developer's Guide->3.4.3 OpenOffice.org Basic->Mapping of UNO and Basic Types->Mapping of Simple Types that states:
Quote:
Always use the type Variant to declare variables for UNO Basic objects, not the type Object. The OpenOffice.orgBasic type Object is tailored for pure OpenOffice.orgBasic objects and not for UNO OpenOffice.org Basic objects. The Variant variables are best for UNO Basic objects to avoid problems that can result from the OpenOffice.org Basic specific behavior of the type Object:
Code:
Dim oService1    ' Ok
oService1 = CreateUnoService( "com.sun.star.anywhere.Something" )

Dim oService2 as Object    ' NOT recommended
oService2 = CreateUnoService( "com.sun.star.anywhere.SomethingElse" )

There's no explanation of what "problems" might occur or what the "OpenOffice.org Basic specific behavior" might be, but keep this in mind (but note that I've had issues choosing between Object and Variant in Global/Private declarations in a module: where it worked if I declared a service as Variant in a local variable, but it would not work if I declared it as a Global Variant, only if I declared it as a Global Object... I haven't investigated this any further, if anyone has seen this or knows what the problem is, please feel free to answer this puzzle).

If you're doing major UNO programming in Basic, it would probably be worth your while to start by reading the Developer's Guide section 3.4.3 OpenOffice.org Basic even if you don't fully understand it at first because it explains how Basic language constructs are mapped onto UNO constructs (things like type promotion and demotion are important to know about as it could trip you up on occasion).

And lastly, there appears to be a "convention" used in some places in the OpenOffice/StarOffice Basic community. In the StarOffice Programmer's Tutoria->1.6 Typographic Conventions, the following chart is included (translated into fixed pitch font here):
Quote:
Certain conventions are used in the StarBasic examples throughout this book. The first letter of a variable always indicates its type as decribed in the following table.
Code:
Letter     Meaning
a          Structure
b          Boolean (True or False)
e          Enumeration -- can only have one of a limited set of values
f          Float or double
m          Array (aka Sequence)
n          Integer or long
o          Object, service, or interface
s          String
x          Interface, to indicate that only operations of a
           particular interface of an object are used
v          Variant, Any

There's no indication of what conventions are used for Date or Currency... any suggestions?

Also, don't forget that variable names (or "markers" as they are technically called) are case insensitive, so vStruct1 is the same variable name as vStRuCt1. But there are some areas related to URLs and UNO constants and enumerations that are case sensitive. More on that later though...

Hope this is helpful to someone, sometime... this knowledge was hard won... Smile

Thanks again to everyone online for posting their findings and code snippets and to those who actively helped me to the point I'm at now (which is still newbie to OOo Basic, but no longer a completely foundering newbie...).
Back to top
View user's profile Send private message
jbotte
Power User
Power User


Joined: 27 Jan 2004
Posts: 57
Location: NC, USA

PostPosted: Fri Feb 20, 2004 9:19 am    Post subject: Newbie jbotte's Basic Syntax Tip #2 -- String parameters Reply with quote

If you have anything to add (or subtract), please feel free to reply to this,
please just use the Subject Re: Basic Syntax Tip #2 -- <your summary>,
so it's easy to follow related concepts.


When calling a function or subroutine, it's convenient to pass dynamically built strings; however, the type of the parameter seems to be determined by the first data type encountered, so the following snippet (it's tested code if you want to try it out yourself...):
Code:
Dim nI As Integer
For nI = 0 To 2
    MsgBox(nI + " ***")
Next nI
Outputs:
Code:
0
1
2

Whereas:
Code:
Dim nI As Integer
For nI = 0 To 2
    MsgBox("nI = " + nI + " ***")  ' Note that it starts with a string now...
Next nI
Outputs:
Code:
nI = 0 ***
nI = 1 ***
nI = 2 ***

This wreaked havoc with a more complex invocation that I was trying to use (note: this does not work):
Code:
Dim nI As Integer
Dim mProperties(2) As New com.sun.star.beans.PropertyValue
mProperties(0).Name = "Dobbs"
mProperties(0).Value = "Bob"
mProperties(1).Name = "Uncle"
mProperties(1).Value = "Bob"
mProperties(2).Name = "Apples"
mProperties(2).Value = "Bob"

For nI = 0 To UBound(mProperties())
    MsgBox(nI + ": " + mProperties(nI).Name + "=" + mProperties(nI).Value)  ' DOES NOT WORK!!!

Next nI

It must have assumed that the desired contruct was Integer because the first component in the parameter was an Integer and then throws a Data Type Mismatch exception when I try to execute the function call. Substituting in the following MsgBox invocation does work:
Code:
    MsgBox("mProperties(" + nI + "): " + mProperties(nI).Name + "=" + mProperties(nI).Value)  ' THIS IS OKAY

NOTE: The same issue exists for any assignment... For example (does not work):

Code:
Dim nI As Integer
Dim mProperties(0) As New com.sun.star.beans.PropertyValue
Dim sValue As Variant

nI = 0
mProperties(nI).Name = "Dobbs"
mProperties(nI).Value = "Bob"
sValue = nI + ": " + mProperties(0).Name + "=" + mProperties(0).Value  ' DOES NOT WORK
MsgBox sValue

But if you use the following assignment instead, it does work (note: using the statement "Dim sValue As String" doesn't solve the problem... it seems to happen before the type of the destination is checked to receive the result of the parsing):
Code:
sValue = "mProperties(" + nI + "): " + mProperties(0).Name + "=" + mProperties(0).Value  ' THIS IS OKAY


Hope this saves you a few minutes of frustration in your coding...
Back to top
View user's profile Send private message
nom
OOo Enthusiast
OOo Enthusiast


Joined: 17 Dec 2003
Posts: 153
Location: NSW, Australia

PostPosted: Fri Feb 20, 2004 3:03 pm    Post subject: Reply with quote

Hi jbotte,

Thanks for the tips.
It might be an idea to put this thread in the code snippet forum for reference purposes.
Continue posting!

nom
Back to top
View user's profile Send private message
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