Wish "With" statement?

udg

Expert
Licensed User
Longtime User
Hi,

I don't know if it was asked before or even if there's already a BASiC counterpart, but would you consider the introduction of a statement like "with" in Pascal/Delphi?

In that language is used this way:

with MyObject do
begin
left = 12
top = 20
text = "hello"
end

Or using it on an Array we could have:

with MyNewArray do
begin
if lenght > 0 then ...
clear
end

Just my 2cents to the wishlist..

Umberto
 

DonManfred

Expert
Licensed User
Longtime User
in delphi it is
B4X:
with MyObject do begin
    left = 12;
    top = 20;
    text = "hello";
end;
 

nikolaus

Member
Licensed User
Longtime User
Just curious since I never was into Delphi. What does this do more than setting some object properties?
 

udg

Expert
Licensed User
Longtime User
@nikolaus
Quoting from DelphiBasics:
The With keyword is a convenience provided by Delphi for referencing elements of a complex variable, such as a record or object.

It simplifies the code by removing the need to prefix each referenced element with the complex variable name.
The VB way of prepending a dot could be safer for disambiguity concerns, but I'm an old Turbo Pascal 1.0 fan...
 

John H. Guillory

Member
Licensed User
Longtime User
Just my $0.02 worth. I eat, sleep, and breath pascal! But the with clause is one I just don't see much use in.
I know, there's a little advantage when dealing with large arrays of records. Basically, instead of making a far pointer call to access each element of the record, you access it near pointer as a local variable. The problem I have with it is some folks like to confuse things like:

With Message, File, Configuration, TempMemory DO BEGIN
Area := 1;
Description := 'next';
File := '\files\myfile.zip';
END;

Now, with the above, does area belong to Message or File? Does File specify the file the message is stored in, or the file your adding to the file database? If Area exist in Message and File, which one gets set, or both? Too many IF's, and too many programmers leave too much to guessing!
Personally, with the way Basic4Android handles things, I don't see myself using it. I won't complain if a feature is added... What you need to consider is how it's going to effect the output. If Java has no with statement (which I don't think it does), then it will just end up saving you time of having to type eg. MessageRec. in front of the variable name. In which case, it wouldn't save you any space or speed up anything.
 

udg

Expert
Licensed User
Longtime User
Hi John,

I agree with you about the perils inherent in the with-statement misuse, indeed a long debated point among Pascal programmers.
But as a knive could be used to slice bread and to kill, so the with-statement could be put in good or bad use. It's all up to us, programmers (and cooks ..eheh).
Anyway, to avoid most problems an eventual with-statement alike could be restricted to a single variable name (or object), something like:
with <single complex var> do
<statements>
</with>

Umberto
 

Dave O

Well-Known Member
Licensed User
Longtime User
+1

I miss this from my Delphi days - made my code easier to write AND read.
 

qsrtech

Active Member
Licensed User
Longtime User
-1
From what I understand, Delphi is moving away from the "With". I don't use it anymore as it causes a lot of issues (in delphi code). In theory it's a good "cheat" but in reality it seems to open the door to problems so I would not be too concerned about getting "with" in the language. I'd rather see real useful stuff added like optional parameters (with default values), "class" const, variables and methods (they work class wide vs instance wide), built in "result" variable for functions, templates for layouts, views, etc (for example, I prefer my panels to not have rounded corners).
 

sorex

Expert
Licensed User
Longtime User
I also remember this from vb6/vbscript/asp. when I first saw it I couldn't really get what it was doing.

When I look at it now I see the benefit of less typework.

On the other hand my mind doesn't think of using it as the traditional methods come up faster.

But it could be a nice addition ofcoz.
 

EddyW

Member
Licensed User
Longtime User
in VB6 its had some performance benefits also

if you used some think like this

B4X:
with form.subform.object
     .left = 10
     .right = 20
     .text = "ABC"
end with

it opens the form, subform , object and set the 3 options
but if you did it like :

B4X:
form.subform.object.left = 10
form.subform.object.right = 20
form.subform.object.text = "ABC"

then it opens the form, subform, object set option and then close all and with next line it opens form, subform, object again.
with example as above the performance gain was minimal but for reading it was easier but if you had big forms, subforms and lot of
objects to set the performance benefit could be big (special with big form or recordsets)

i dont know if in b4a there is a performance benefit but for reading i would like to have it.
 
Last edited:

aeric

Expert
Licensed User
Longtime User
+1 Save some time when you do Find and Replace. Less typing and more readable code. It makes VB6 programmer like me happy.

B4X:
Dim lbl As Label
lbl.Initialize("")
lbl.Gravity = Gravity.LEFT
lbl.Text = Text
lbl.TextSize = 16
lbl.TextColor = Colors.Black
lbl.Color = Colors.Transparent
Dim lbl2 As Label
lbl2.Initialize("")
lbl2.Gravity = Gravity.RIGHT
lbl2.Text = Text2
lbl2.TextSize = 14
lbl2.TextColor = Colors.Black
lbl2.Color = Colors.Transparent

B4X:
Dim lbl As Label
With lbl
    .Initialize("")
    .Gravity = Gravity.LEFT
    .Text = Text
    .TextSize = 16
    .TextColor = Colors.Black
    .Color = Colors.Transparent
End With
Dim lbl2 As Label
With lbl2
    .Initialize("")
    .Gravity = Gravity.RIGHT
    .Text = Text2
    .TextSize = 14
    .TextColor = Colors.Black
    .Color = Colors.Transparent
End With
 
Top