Page 1 of 3
USING syntax ? - help please ...
Posted: Wed May 02, 2018 2:01 pm
by Phil Hepburn
Hi Guys,
Sorry, I must not have been paying attention at the Conference in Cologne, although I did learn a great deal (LOTS) ;-0) Thanks to all session providers.
Please can you put me right on the correct syntax to use for "begin/end USING", that we can now use like in C#.
I wish to have a StreamReader object which is opened then closed and removed within my IF statement - simple images attached of my so far fail attempts.
Please be aware that my brain is a bit fuddled as I have just spent a few hours coding Async/Await/Task samples. Much watch some cricket !
- helpForum_using_01.png (10.99 KiB) Viewed 714 times
- helpForum_using_02.png (10.74 KiB) Viewed 714 times
TIA,
Phil,
Wales, UK (at the moment )
USING syntax ? - help please ...
Posted: Wed May 02, 2018 2:19 pm
by wriedmann
Hi Phil,
this is my own sample for the using statement:
Code: Select all
using System.IO
using System.Text
function Start( ) as void
local cFileName as string
local aBytes as byte[]
cFileName := "c:tempUsing.txt"
System.Console.WriteLine("start working with" + cFileName )
begin using var oFile := FileStream{ cFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite } // as FileStream
aBytes := Encoding.ASCII.GetBytes( "Hello world!" )
oFile:Write( aBytes, 0, aBytes:Length )
end using
// file is not only out of scope, but also disposed, i.e. closed
System.Console.WriteLine("end working with" + cFileName )
System.Console.Write( "press a key...." )
System.Console.ReadKey()
return
I was not able to use the
as FileStream in the using statement, only
var worked.
Wolfgang
USING syntax ? - help please ...
Posted: Wed May 02, 2018 2:22 pm
by FFF
Hi Phil,
well, there IS a help file, even offline
From what i read:
BEGIN USING VAR oTest := Test{}
oTest:DoSomething()
END USING
....
Or, maybe i didn't understand the problem
BTW, i didn't find in help, how multiple vars are handled - if one has to nest them, or one "End Using" to unbound them all...
Karl
USING syntax ? - help please ...
Posted: Wed May 02, 2018 2:27 pm
by wriedmann
Hi Karl,
BTW, i didn't find in help, how multiple vars are handled - if one has to nest them, or one "End Using" to unbound them all...
Fabrice said you can nest them, and this is how it works also in C#.
Code: Select all
begin using var v1 := ...
begin using var v2 := ....
do something
end using
end using
Wolfgang
USING syntax ? - help please ...
Posted: Wed May 02, 2018 3:30 pm
by Phil Hepburn
Hi Karl,
Yes, and I was using the on-line help from the web site / forum.
Problem with all new stuff however, is when is/was uploaded or added, etc., etc,.
The on-line details I found were the basis of my attempts, and what I posted to you guys.
Oh! - I see, there are at least two places in the on-line HELP which have different versions of syntax! Or there was a couple of hours ago. Now I see at least two versions of the same (and improved) syntax. Who has been busy this afternoon ? Can't now find the one with 'eos' and the ?s in my last images.
Fingers crossed that this now works.
Thanks Wolfgang for you sample.
Regards,
Phil.
USING syntax ? - help please ...
Posted: Wed May 02, 2018 3:34 pm
by Phil Hepburn
Karl (and all),
This is what I had previously found and tried to use and interpret.
- helpForum_using_03.png (49.25 KiB) Viewed 714 times
This may be a correct and fuller way to define the syntax BUT, as a standard user guy I find it hard to understand.
HTH,
Phil
USING syntax ? - help please ...
Posted: Wed May 02, 2018 4:14 pm
by wriedmann
Hi Phil,
the problem with this documentation page is that it was auto-generated from the language definition, and someone from the devteam can explain what every word exactly means.
The word "eos" means "end of statement", I think.
To be sure, you could check also
https://docs.xsharp.it where I had added exact the sample after I returned from the conference:
http://docs.xsharp.it/doku.php?id=using
I have to explain it better, I know - there needs to be said the used object must implement the IDisposable interface.
Wolfgang
USING syntax ? - help please ...
Posted: Wed May 02, 2018 5:42 pm
by Phil Hepburn
Wolfgang - Thanks, and that explains why I could not USE a 'StringBuilder' class / object ;-0((
- helpForum_using_04.png (13.21 KiB) Viewed 714 times
That is a shame.
Are there any other restrictions as to what the USING class can or can't be ?
Cheers,
Phil.
USING syntax ? - help please ...
Posted: Wed May 02, 2018 6:48 pm
by wriedmann
Hi Phil,
since the X# compiler is using the Roslyn compiler, this is true also for C#. So the X# team is not to blame for this:
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement
Wolfgang
USING syntax ? - help please ...
Posted: Wed May 02, 2018 7:25 pm
by Chris
Guys,
Well, what BEGIN USING does is to implicitly call Dispose() on the object after it is used, so if the class does not implement IDisposable (which provides the Dispose() method) then it wouldn't make much sense
OK, it actually also implicitly uses a TRY...FINALLY block and enforces a limited scope (when exiting it, it can now be collected) for the declared variable(s), which you can also enforce manually if you want, for any local, no matter if it is an IDisposable or not, by using BEGIN..END SCOPE:
Code: Select all
BEGIN SCOPE
// All vars declared here will be visible only inside this block (scope)
LOCAL oAnyVar AS MyType
// ...
END SCOPE
Chris