| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Wed Mar 24, 2004 2:50 pm Post subject: Macro Basic: "If" statement problem or is it the & |
|
|
I am getting a syntax error when I use a cascading IF, but as far as I can ascertain (from the Help and Sun's Basic Programming manual) this should at least run (I'm not sure about the logic of what I am trying to achieve!):
if (charCount > 0) and (len(numberToRemoveAsString)= 1) then outputString = left(inputString, charCount - len(numberToRemoveAsString) - 1 ) + right(inputString, len(inputString)-charCount)
oSelection.String = outputString
else if resultofand = -1 then outputString = left(inputString, charCount - 2 ) + right(inputString, len(inputString)- charCount)
oSelection.String = outputString
else MsgBox "That number is not present"
end if
This produces an error at the elseif: "else/elseif without if"
Also, I have to click the OK button twice to dismiss this message.
I have another version of this which does not produce a syntax error but also does not produce the correct results - the actual logic of the processing of the IFs is not what I would expect. It seems that only the first test is done, if that fails the macro continues after the "endif" and ignores the "elseif".
Why do none of the help examples include more than one line of code to be executed after an "if" or "elseif" |
|
| Back to top |
|
 |
JohnV Administrator

Joined: 07 Mar 2003 Posts: 8982 Location: Lexinton, Kentucky, USA
|
Posted: Wed Mar 24, 2004 3:28 pm Post subject: |
|
|
See if this works for you: | Code: | if (charCount > 0 and len(numberToRemoveAsString)= 1) then
outputString = left(inputString, charCount - len(numberToRemoveAsString) - 1 ) + right(inputString, len(inputString)-charCount)
oSelection.String = outputString
elseif resultofand = -1 then
outputString = left(inputString, charCount - 2 ) + right(inputString, len(inputString)- charCount)
oSelection.String = outputString
else MsgBox "That number is not present"
end if |
|
|
| Back to top |
|
 |
Guest
|
Posted: Thu Mar 25, 2004 2:20 pm Post subject: IF |
|
|
Thanks John V - that at least solved the syntax error and probably explained why I thought the "if" command did not work as expected. I feel really stupid but in my defence I have used languages that don't mind where you put the rest of the "then". Do you think there is a case to be made for the manual/help page to mention explicitly that the "then" statements should begin on a separate line?
Thanks again from the other side of the Atlantic. |
|
| Back to top |
|
 |
JohnV Administrator

Joined: 07 Mar 2003 Posts: 8982 Location: Lexinton, Kentucky, USA
|
Posted: Thu Mar 25, 2004 3:53 pm Post subject: |
|
|
Didn't Will Rogers say something like, "I never met a language that didn't have its syntax quirks."?
You can get away with continuing "then" on the same line if you use a colon or if you have a one line "If" statement. | Code: | Sub Main
a = 1
If (a = 1) then : b=1
ElseIf (a = 2) then : b = 2
Else b = 3
EndIf
If (true) then print b
End Sub |
Also note that it appears "ElseIf" needs to be one word, at least in this context. If you make it 2 words here you get an error. |
|
| Back to top |
|
 |
Guest
|
Posted: Sat Mar 27, 2004 1:26 pm Post subject: More on Basic |
|
|
| Can't comment on Will Rogers - he's no that well known over here. Back on subject, how did you find out that you can continue "then" on the same line. I didn't see it in the Sun manual. Is it a guess from using a different language or is there more/better documentation out there? |
|
| Back to top |
|
 |
pitonyak Administrator


Joined: 09 Mar 2004 Posts: 3618 Location: Columbus, Ohio, USA
|
Posted: Sat Mar 27, 2004 6:33 pm Post subject: |
|
|
I found a log of things that are not documented. Some of them I found by reading the source code, some I found by looking at other examples, and some I found because it worked in VB, I tried it in OOo, and it worked. _________________ --
Andrew Pitonyak
http://www.pitonyak.org/oo.php |
|
| Back to top |
|
 |
JohnV Administrator

Joined: 07 Mar 2003 Posts: 8982 Location: Lexinton, Kentucky, USA
|
Posted: Sun Mar 28, 2004 9:15 am Post subject: |
|
|
| Quote: | | how did you find out that you can continue "then" on the same line | When I get syntax errors I just try things to get rid of them. Because ":" is a command separator I tried that.
It's not easy to read code when more than one command exists on a line but it sure can make it shorter so I often code in that manner, especially if it just something simple.
Will Rogers was a famous American humorist ("down home" and folksy). His most often quote line is, "I never met a man I didn't like." or something close to that. |
|
| Back to top |
|
 |
|