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 !
TIA,
Phil,
Wales, UK (at the moment )
USING syntax ? - help please ...
USING syntax ? - help please ...
Hi Phil,
this is my own sample for the using statement:
I was not able to use the as FileStream in the using statement, only var worked.
Wolfgang
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
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
USING syntax ? - help please ...
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
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
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
USING syntax ? - help please ...
Hi Karl,
Wolfgang
Fabrice said you can nest them, and this is how it works also in C#.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...
Code: Select all
begin using var v1 := ...
begin using var v2 := ....
do something
end using
end using
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
- Phil Hepburn
- Posts: 743
- Joined: Sun Sep 11, 2016 2:16 pm
USING syntax ? - help please ...
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.
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.
- Phil Hepburn
- Posts: 743
- Joined: Sun Sep 11, 2016 2:16 pm
USING syntax ? - help please ...
Karl (and all),
This is what I had previously found and tried to use and interpret.
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
This is what I had previously found and tried to use and interpret.
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 ...
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
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
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
- Phil Hepburn
- Posts: 743
- Joined: Sun Sep 11, 2016 2:16 pm
USING syntax ? - help please ...
Wolfgang - Thanks, and that explains why I could not USE a 'StringBuilder' class / object ;-0((
That is a shame.
Are there any other restrictions as to what the USING class can or can't be ?
Cheers,
Phil.
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 ...
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
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
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
USING syntax ? - help please ...
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:
Chris
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 Pyrgas
XSharp Development Team
chris(at)xsharp.eu
XSharp Development Team
chris(at)xsharp.eu