Similar C# like "Using" in XoJo?

Hello.
I would like to know if there is in Xojo something similar to C# like “Using”.

Example code in C#

if (!System.IO.File.Exists(path))

Can write it like this:

using System.IO;
if (!File.Exists(path))

In Xojo I have this situation:
Window1.FPmyMapS1.FlightPlanOnMapS1a.XmlFlightPlan.DepartureLat

Can I shorten the line above, for example, writing:
FS.DepartureLat
where FS refers to Window1.FPmyMapS1.FlightPlanOnMapS1a.XmlFlightPlan ?

Thank you.

https://documentation.xojo.com/api/language/using.html

somehow
var FS as FlightPlan = Window1.FPmyMapS1.FlightPlanOnMapS1a.XmlFlightPlan
FS.DepartureLat

or make a Method that return what you want.

this seems missing in Xojo but it was useful in VB6

With Self.PushButton1
.Caption = "Hello"
End With

Using (a pun) such long references is a code smell. You need to encapsulate your containers, your controls and your methods. Start by making all controls of your containers private. Then define a communication of the container back to the window.

2 Likes

https://documentation.xojo.com/api/language/using.html ?

For Namespaces and Modules

In the past I asked the possibility of creating “Aliases” (denied) just for such case. The explanation was that I could CREATE ANOTHER var to hold a shorter name. I was not satisfied, it’s not the same, it causes mem and speed penalties, but it’s the Xojo way.

Creating a Var with a pointer will make your code faster.

Asigning another Var instead of using something directly could be pretier code, convinient, faster to type BUT the extra step, will SLOW the code (negligible).

What Beatrix said is the recomended way, but not wallways is the best way.

You can do it creating a local variable that points to your object, (maybe this is what Tim was talking about)

Dim FS as WhatEverTypeIsThat = Window1.FPmyMapS1.FlightPlanOnMapS1a.XmlFlightPlan

FS.DepartureLat = X
Y = FS.DepartureLat
...

I don’t know how you imagined that, but using such workaround instead of a direct access via an alias causes the following at runtime in a closed scope:

  1. New var ref allocation.
  2. copy the ref target destination address to such var.
  3. Incrementing the reference count.
  4. here you do your job
  5. decrement the reference count.
  6. release the ref var memory

An alias would do only the step 4 : Do the job.

What’s an alias? A metalanguage (code that generate another code) concept, like macros, but maybe simpler and dumber, something solved at compile time and not at runtime, like:

Alias r As Window1.FPmyMapS1.FlightPlanOnMapS1a.XmlFlightPlan // r exists only at compile time
r.DepartureX = X
r.DepartureY = Y

That’s exactly the same as writing

Window1.FPmyMapS1.FlightPlanOnMapS1a.XmlFlightPlan.DepartureX = X
Window1.FPmyMapS1.FlightPlanOnMapS1a.XmlFlightPlan.DepartureY = Y

No extra allocations and processing, just the execution part that could be extra fast if the compiler can infer and hold the value of XmlFlightPlan in CPU registers to be reused right after (the repeating Alias “r”, helps that).