SOLVED: Multiline statement

Post

Posted
Rating:
#1 (In Topic #740)
Trainee
Love Gambas and it is working very well. I have some long SQL type statements, and my limited mind cannot figure out the best way to accomplish it without just multiple lines of SQLString = SQLString & xyz
SQLString = SQLString & abc
SQLString = SQLString & edf….

I tried the VB method of an Underscore following by the next line beginning with an & but that did not work. With all the ' ", ' " required to insert variables I am unsure as to how to elegantly split these up over multiple lines.

Thanks in advance for any help.
Online now: No Back to the top

Post

Posted
Rating:
#2
Guru
BruceSteers is in the usergroup ‘Guru’

CMWhitley said

Love Gambas and it is working very well. I have some long SQL type statements, and my limited mind cannot figure out the best way to accomplish it without just multiple lines of SQLString = SQLString & xyz
SQLString = SQLString & abc
SQLString = SQLString & edf….

I tried the VB method of an Underscore following by the next line beginning with an & but that did not work. With all the ' ", ' " required to insert variables I am unsure as to how to elegantly split these up over multiple lines.

Thanks in advance for any help.

There are these ways…

Code (gambas)

  1. SQLString &= "More text"
  2. SQLString &= "Even More text"
  3.  

or these methods will work as multiline…

Code (gambas)

  1. SQLString = "some text\n" &
  2.                     "some more text\n" &
  3.                    "yet some more"
  4.  
you can even omit the &
Ie.

Code (gambas)

  1. SQLString = "some text\n"
  2.                     "some more text\n"
  3.                    "yet some more"
  4.  
Will work

Similar for commands/arrays using the comma ,
Ie..
MyCommand(arg1,
                       arg2,
                       arg3)

MyArray[Item1,
             Item2,
             Item3]

But note you will get problems with blank lines and positioning of ) ]

Ie.
Message.Question(
                 "My question"
                    )
 
that will fail.

Message.Question(
                 "My question")

will work

MyArray = [
                Item1,
                Item2
                 ]
Will work

MyArray = [
                Item1,

                Item2
                 ]
will fail


also when using multiline methods the IDE will loose the autocomplete ability when typing.
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Regular
thatbruce is in the usergroup ‘Regular’

CMWhitley said

Love Gambas and it is working very well. I have some long SQL type statements, and my limited mind cannot figure out the best way to accomplish it without just multiple lines of SQLString = SQLString & xyz
SQLString = SQLString & abc
SQLString = SQLString & edf…

I tried the VB method of an Underscore following by the next line beginning with an & but that did not work. With all the ' ", ' " required to insert variables I am unsure as to how to elegantly split these up over multiple lines.

OK unthink that idea immediately.  Its like trying to replace chocolate syrup with a sardine in a milkshake.

I was sure that there is a page in the wiki about multi-line string constants but I can't see it. So,

In the IDE (or more specifically in the source code for a class) a string can be continued on the next line simply by making it like this:

Code

Dim sMary as "Mary had a little lamb, "
"So what, I hear you ask "
"I had a little omelet"

Which is pretty easy, the string sMary is the theoretical "concatenation" of what looks like three strings. It's not, it's actually one string.

Now here is the bad news
You need to see the spaces!
If you try "SELECT *"
"FROM myStupidTable;"

You will get "SELECT *FROM myStupidTable"
which aint gunna work.

Online now: No Back to the top

Post

Posted
Rating:
#4
Trainee
 Thank you gentleman! Those tips are exactly what I needed to make for a cleaner code.
Take care.
Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
I forgot to mention!

If you build your queries in an external tool, say something like dBeaver, you can copy your query and then in the IDE do a paste special (Shift + Ctl + V). In the popup there is an option down the bottom to paste "as multi-line string".

b

Online now: No Back to the top

Post

Posted
Rating:
#6
Trainee

thatbruce said

I forgot to mention!

If you build your queries in an external tool, say something like dBeaver, you can copy your query and then in the IDE do a paste special (Shift + Ctl + V). In the popup there is an option down the bottom to paste "as multi-line string".

b

Now that's something worth remembering.  Thank you!
Online now: No Back to the top
1 guest and 0 members have just viewed this.