xsharp.eu • USING syntax ? - help please ... - Page 2
Page 2 of 3

USING syntax ? - help please ...

Posted: Wed May 02, 2018 7:38 pm
by Phil Hepburn
Thanks Chris,

Will give this a go with the StringBuilder I was playing around with earlier.

Cheers,
Phil.

USING syntax ? - help please ...

Posted: Wed May 02, 2018 8:07 pm
by FFF
Chris,
FWIW, i don't get most of the use(fulness) of all this. If i know and want to get rid of a var at a certain point in my code, i can and should null it, with less keys to type and less visual clutter produced.
If i tend to reuse "names", i should better rethink my naming scheme and/or build smaller entities...
So, what remains?

Karl

USING syntax ? - help please ...

Posted: Wed May 02, 2018 8:31 pm
by Chris
Hi Karl,

It's a matter of personal style, preferences etc. Purists will tell you that you should always use scopes and similar stuff, others will say all this is nonsense, personally I am somewhere in between :)

I'd say to everyone just use the way you prefer, no matter what is the "recommended" way to do something or not, just make sure you know the advantages and disadvantages of each method..

Chris

USING syntax ? - help please ...

Posted: Thu May 03, 2018 6:09 am
by robert
Phil,
That notation in the docs is like the regular expression

END USING? garbage? eos

means:

END = mandatory end keyword
USING? = optional USING keyword
garbage? = optionally comments and other tokens until the end of the statement
eos = end of statement (either CRLF or semi colon)

END and USING are in capitals, they are tokens
garbage and eos are in lowercase, they are other parser rules.

Robert

USING syntax ? - help please ...

Posted: Thu May 03, 2018 9:01 am
by Phil Hepburn
Thanks for the explanation Robert.

I am about to spend a few hours on some 'Async/Await/Task' examples and so I will use the USING and scope stuff just to see how it works etc.

I am a bit like Chris, somewhere in between ;-0)

But over the years with .NET I do tend to declare variables much closer to where I need to use them - it feels right to me. And I try to dispose of some (external type) objects immediately after they have been used.

I can see what Microsoft are doing - trying to hide all/much code stuff that gets in the way of a more readable and descriptive business solution. They have done this in many places, some much bigger than others.

Must shoot - I think Alwyn wishes me to help him get going with XSharp, he's on the phone just now.

Best Regards,
Phil.

USING syntax ? - help please ...

Posted: Thu May 03, 2018 9:42 am
by wriedmann
Hi Karl,

if I remember correctly what Fabrice said in his session: the "using" statement is very useful because you cannot forget to close a file - the runtime system does it for you.

And there is another important thing: after the end of a "using" statement the relative variable is collected immediatly (and the Dispose() method is also called).

Of course you can do that also by hand, but then you have also to provide a try-catch block to make sure the file is also closed when an exception occurs.

Personally, I don't like all of these new language features because I think some of them make the code harder to read, but the "using" statement is IMHO a very useful addition, and I'm pretty sure I will start to use it.

Wolfgang

USING syntax ? - help please ...

Posted: Thu May 03, 2018 9:16 pm
by FFF
Wolfgang Riedmann wrote:if I remember correctly what Fabrice said in his session: the "using" statement is very useful because you cannot forget to close a file - the runtime system does it for you.
Interesting. Is there a somewhat more "generic" description, what we can assume the runtime to do - and what not?

TIA
Karl

USING syntax ? - help please ...

Posted: Fri May 04, 2018 2:41 am
by wriedmann
Hi Karl,

my earlier posted link to the MS documentation describes it:
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement
Wolfgang

USING syntax ? - help please ...

Posted: Fri May 04, 2018 10:42 am
by robert
Wolfgang,
Nesting BEGIN USING is not really necessary.
This works, just like it does in C#

Code: Select all

	BEGIN USING LOCAL oStream := FileStream{"aa", FileMode.Open}, ;
		oStream2 := FileStream{"bb", FileMode.Open} AS FileStream
		? oStream:ReadByte()
		? oStream2:ReadByte()
	END USING             
If you compile this and open it with reflector or ilspy you will see that under the hood indeed 2 nested using statements were generated.

Robert

USING syntax ? - help please ...

Posted: Fri May 04, 2018 12:18 pm
by wriedmann
Hi Robert,

thank you very much! Is is good to hear that this works also.

Personally I prefer the nested using statements because code is easier to read and to understand (IMHO readability is one of the most important advantages of X# over C#).

Wolfgang