Encapsulating code

Can code be encapsulated? Any equivalent to VB.NET’s region: http://msdn.microsoft.com/en-us/library/sd032a17.aspx. I could do with a way of encapsulating code. I could use an if statement like:

if true then //code group name

end if

or something but that could make the code confusing and it waists processing.

Thanks

Xojo has nothing line VB Regions

and apology but pet peeve here : WASTE not WAIST

one goes in the trash, the other is where you belt goes

[quote=90601:@Dave S]Xojo has nothing line VB Regions

and apology but pet peeve here : WASTE not WAIST

one goes in the trash, the other is where you belt goes[/quote]
Thanks. I did not understand what you said here ‘Xojo has nothing line VB Regions’. Sorry.

Confused… how can you not understand what I meant by VB Regions when in fact you provided a link to a webpage all about them?

Dave, I think maybe he was trying to make a joke/retort about line instead of like just like you commented on waist instead of waste. But I’m confused as well…

Yes. Create a class and define public and private properties. When instantiating the class, set the properties and then work on that data with private methods inside of the class. This would be my understanding of encapsulation, in short.

I would not call this “encapsulating”, just a way to organize code and to collapse and expand it in the IDE.

One could do something similar to what you suggested, by defining a public method in a modul:

Function Region(s As String) As Boolean Return True End Function

And now one could write, similar to VB:

#If Region("My fancy sql stuff") // Clear current default settings in database table listcolumns sql = "DELETE FROM " + imDB.prefix + "listcolumns WHERE DB_GUID = '" + Me.DB_GUID + "'" #EndIf

And in the IDE, this code can now be collapsed and expanded, just like in VB.

use

[code]#if true then

#endif[/code]

only processed at compile time so shouldn’t slow any. And it collapses.

[quote=90667:@Will Shank]…
#if true then

#endif[/quote]
Yes, which brings us back to the solution Oliver Scott-Brown has initially posted:

[code]if true then //code group name

end if[/code]

He simply adds a comment, and this is the shortest way to make code collapsible in the Xojo IDE, AND get information about what we can find inside of a collapsed section.

The difference is that Will’s solution does not add any processing time at runtime, so it’s completely invisible to the running app.

Or is that what you were saying? (I’m still a bit sleepy.)

One important note - if you use “if true” then any variables dimmed inside this code block will be scoped local to the code block. This can be either a help or a hinderance.

dim x as integer = 34
if true then
   dim y1,y2,y3,y4 as integer = 42
   for x as integer = 1 to 99
   next x
end if

// at this point,
// x = 34
// y1 thru y4 are not defined

Personally, I use the “if true” style sometimes when I need to define a bunch of temporary variables but I don’t want them to exist later on in the method.

[quote=90800:@Michael Diehr]One important note - if you use “if true” then any variables dimmed inside this code block will be scoped local to the code block. This can be either a help or a hinderance.

dim x as integer = 34
if true then
   dim y1,y2,y3,y4 as integer = 42
   for x as integer = 1 to 99
   next x
end if

// at this point,
// x = 34
// y1 thru y4 are not defined

Personally, I use the “if true” style sometimes when I need to define a bunch of temporary variables but I don’t want them to exist later on in the method.[/quote]
Thanks and that is a good idea for scoping variables btw.

I personally use Try & End Try to encapsulate code sections.

Try ~Do some awesome stuff here. End Try

[quote=91008:@Sam Rowlands]I personally use Try & End Try to encapsulate code sections.

Try ~Do some awesome stuff here. End Try[/quote]

Interesting : neither If/End If or Try/End Try are “correct” for this - we are both relying on their side-effects.

I wonder if a new construct such as “block/end block” might be useful?

Don’t forget; with Try, you can add Catch. It helps when try’ing to narrow down runtime exceptions on customer machines.