Page 1 of 1

with ... endwith structure

PostPosted: Sun Feb 13, 2011 11:39 am
by Boudewijn
While referencing object on forms or the form itself it would be nice to see a possibility like

with thisform
{
.property1.value = somevalue
.property2.value = someOtherValue
.objectOnTheForm.property1.value = YetAnotherValue
.objectOnTheForm.Method_or_Event([parameters])
.Method_or_event([parameters])

with .objectOnTheForm // a "with structure" nested in the with structure
{
.property1.value = "xyz"
.property2.value = 123
.property3.value = globals.someFunctionReturningAValue()
}
}

this way one could be saved from typing the reference to the form/object on each and every line. Thus a real time saver.
When I could add a reference to the base object (form or whatever) that would be really great, as in

with thisform as SomeBaseForm in LibraryName
{
.customProperty1.value = somevalue
}

Re: with ... endwith structure

PostPosted: Sun Feb 13, 2011 12:13 pm
by ROCLASI
Hi Boudewijn,

You can put objects into variables and use them as those objects like so:
Code: Select all
var f = forms.thisform; // form object
f.property1.value = somevalue
f.property2.value = someOtherValue
f.objectOnTheForm.property1.value = YetAnotherValue
f.objectOnTheForm.Method_or_Event([parameters])
f.Method_or_event([parameters])

var e = f.elements.objectOnTheForm; // element object
e.property1.value = "xyz"
e.property2.value = 123
e.property3.value = globals.someFunctionReturningAValue()


Hope this helps.

Re: with ... endwith structure

PostPosted: Sun Feb 13, 2011 12:18 pm
by Peter de Groot
You could try,

with(forms.main){
elements.lbl_text.text = 'test';
}

Re: with ... endwith structure

PostPosted: Sun Feb 13, 2011 11:40 pm
by Boudewijn
to roclasi,

an object is just that, an object and not a variable. so if I add an object to a form then I want to be able to access that object and not the variable.
I know, I belong to the spoiled bunch of developers who are called "VFP developers", the crowd that is used to OOP in its purest form.
Since Servoy (the company) wants the VFP devvies to join the Servoy crowd I speak out what I am used to. and what I would like to see in Servoy as well.
No compromis.

BL

Re: with ... endwith structure

PostPosted: Sun Feb 13, 2011 11:59 pm
by ROCLASI
Well excuse me Your Highness.
With such an attitude (in this and other posts) I wish you good luck with getting help.

I am out. No compromis.

Re: with ... endwith structure

PostPosted: Mon Feb 14, 2011 12:04 am
by Boudewijn
roclasi,

I am asking questions. Addressing me as "highness" is a red light.
Totally confused about the complexity of the object model and indeed I have quite an outspoken opinion how things should work.
If you don't have the answers... just say so.

BL

Re: with ... endwith structure

PostPosted: Mon Feb 14, 2011 12:49 am
by Harjo
Boudewijn,

you dont have to be that rude!
You are not talking to some kind of rookie here....
(yes I have read your other post's also, and I understand his reaction.)
Robert is trying to help you out here, and is NOT an employee of Servoy, or responsible of how Servoy works, whatsoever.

He even received a community spirit award last week, on the ServoyWorld conference in Amsterdam, for his contribution to the forum!
So, be nice, please!

Re: with ... endwith structure

PostPosted: Mon Feb 14, 2011 11:29 am
by pbakker
Boudewijn,

The code structure you ask for is already possible at Peter showed:
with(forms.main){
elements.lbl_text.text = 'test';
}


However, while you might be used to this, it's considered bad practice to use in in the JavaScript world. Such bad practice even that in future JavaScript specification the "with" statement is even deprecated.

So, better not use it.

Paul

Re: with ... endwith structure

PostPosted: Mon Feb 14, 2011 7:12 pm
by wiseguy
Here's a nice little article about this ... http://www.yuiblog.com/blog/2006/04/11/ ... d-harmful/.

Re: with ... endwith structure

PostPosted: Mon Feb 14, 2011 9:04 pm
by jgarfield
Boudewijn wrote:While referencing object on forms or the form itself it would be nice ...[stuff]....
this way one could be saved from typing the reference to the form/object on each and every line. Thus a real time saver.


In case this is what you are looking for, this hasn't been made clear yet.

If you are working in a function on form MyForm you do not have to use this syntax

Code: Select all
forms.MyForm.formVar1 = 'x';
forms.MyForm.formVar2 = 123;
forms.MyForm.foundset.fieldX = 42.13
forms.MyForm.elements.lblInfo = 'Verbosity";


you can just do

Code: Select all
formVar1 = 'x';
formVar2 = 123;
foundset.fieldX = 42.13
elements.lblInfo = 'Verbosity";


Technically in that example you don't even need the reference to foundset, but we find it's good practice to reference it that way so that it's clear that is where the action is happening.

As far as using with(blah) {} vs assigning what you are working with to a local variable, as far as javascript is concerned there's not much if a difference.

Code: Select all
var a = elements.lblInfo


All this does is create a reference to elements.lblInfo, it points to the same memory and everything...it's semantically the same...in fact, elements.lblInfo is itself just another variable in javascript that is a reference to a Java object in the underlying framework.

The difference that you get with using with(blah) {} is (if I recall my finer points of javascript correctly) that it creates another node on the scope chain, changing what the this keyword is looking at. However, like Paul said, this is a language feature that is scheduled to be deprecated in a future version of JavaScript, so unless you want to have to change your code when the Servoy JavaScript engine gets updated, it might not be a great idea to use it.

If you are really looking for that kind of block scoping JavaScript does provide another pattern you can use which looks something like this
Code: Select all
(function (o)
{
     o.property1.value = 1
     o.property2.value = 'x'
     o.method()
})(objectToWorkWith);


Where you create a one off anonymous function and pass your object in to do all the work on. You get your localized block scope, your shortened typing times, but you still have to reference your object with o.

As you can see, there are several ways to accomplishing the same concept that you are talking about, however whenever you switch languages you should expect to have to endure a few syntactical and paradigm changes.