fbpx
Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1
  • 2

TOPIC: USING syntax ? - help please ...

USING syntax ? - help please ... 10 months 3 weeks ago #1

  • Phil Hepburn
  • Phil Hepburn's Avatar Topic Author
  • Offline
  • Posts: 737
  • Karma: 7
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 )
Attachments:

Please Log in or Create an account to join the conversation.

USING syntax ? - help please ... 10 months 3 weeks ago #2

  • wriedmann
  • wriedmann's Avatar
  • Away
  • Posts: 1423
  • Karma: 6
Hi Phil,

this is my own sample for the using statement:
using System.IO                              
using System.Text

function Start( ) as void                     
local cFileName as string
local aBytes as byte[]
	                         
cFileName := "c:\temp\Using.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
Wolfgang Riedmann
Meran, South Tyrol, Italy
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it

Please Log in or Create an account to join the conversation.

USING syntax ? - help please ... 10 months 3 weeks ago #3

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

Please Log in or Create an account to join the conversation.

USING syntax ? - help please ... 10 months 3 weeks ago #4

  • wriedmann
  • wriedmann's Avatar
  • Away
  • Posts: 1423
  • Karma: 6
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#.
begin using var v1 := ...
  begin using var v2 := ....
    do something
  end using
end using

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it

Please Log in or Create an account to join the conversation.

USING syntax ? - help please ... 10 months 3 weeks ago #5

  • Phil Hepburn
  • Phil Hepburn's Avatar Topic Author
  • Offline
  • Posts: 737
  • Karma: 7
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.

Please Log in or Create an account to join the conversation.

USING syntax ? - help please ... 10 months 3 weeks ago #6

  • Phil Hepburn
  • Phil Hepburn's Avatar Topic Author
  • Offline
  • Posts: 737
  • Karma: 7
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
Attachments:

Please Log in or Create an account to join the conversation.

USING syntax ? - help please ... 10 months 3 weeks ago #7

  • wriedmann
  • wriedmann's Avatar
  • Away
  • Posts: 1423
  • Karma: 6
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
Wolfgang Riedmann
Meran, South Tyrol, Italy
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it

Please Log in or Create an account to join the conversation.

USING syntax ? - help please ... 10 months 3 weeks ago #8

  • Phil Hepburn
  • Phil Hepburn's Avatar Topic Author
  • Offline
  • Posts: 737
  • Karma: 7
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.
Attachments:

Please Log in or Create an account to join the conversation.

USING syntax ? - help please ... 10 months 2 weeks ago #9

  • wriedmann
  • wriedmann's Avatar
  • Away
  • Posts: 1423
  • Karma: 6
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
Wolfgang Riedmann
Meran, South Tyrol, Italy
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it

Please Log in or Create an account to join the conversation.

USING syntax ? - help please ... 10 months 2 weeks ago #10

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:
BEGIN SCOPE
  // All vars declared here will be visible only inside this block (scope)
  LOCAL oAnyVar AS MyType
  // ...
END SCOPE

Chris
XSharp Development Team
chris(at)xsharp.eu

Please Log in or Create an account to join the conversation.

USING syntax ? - help please ... 10 months 2 weeks ago #11

  • Phil Hepburn
  • Phil Hepburn's Avatar Topic Author
  • Offline
  • Posts: 737
  • Karma: 7
Thanks Chris,

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

Cheers,
Phil.

Please Log in or Create an account to join the conversation.

USING syntax ? - help please ... 10 months 2 weeks ago #12

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

Please Log in or Create an account to join the conversation.

USING syntax ? - help please ... 10 months 2 weeks ago #13

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
XSharp Development Team
chris(at)xsharp.eu

Please Log in or Create an account to join the conversation.

USING syntax ? - help please ... 10 months 2 weeks ago #14

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
XSharp Development Team
The Netherlands
This email address is being protected from spambots. You need JavaScript enabled to view it.

Please Log in or Create an account to join the conversation.

USING syntax ? - help please ... 10 months 2 weeks ago #15

  • Phil Hepburn
  • Phil Hepburn's Avatar Topic Author
  • Offline
  • Posts: 737
  • Karma: 7
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.

Please Log in or Create an account to join the conversation.

USING syntax ? - help please ... 10 months 2 weeks ago #16

  • wriedmann
  • wriedmann's Avatar
  • Away
  • Posts: 1423
  • Karma: 6
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
Wolfgang Riedmann
Meran, South Tyrol, Italy
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it

Please Log in or Create an account to join the conversation.

USING syntax ? - help please ... 10 months 2 weeks ago #17

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

Please Log in or Create an account to join the conversation.

USING syntax ? - help please ... 10 months 2 weeks ago #18

  • wriedmann
  • wriedmann's Avatar
  • Away
  • Posts: 1423
  • Karma: 6
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
Wolfgang Riedmann
Meran, South Tyrol, Italy
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it

Please Log in or Create an account to join the conversation.

USING syntax ? - help please ... 10 months 2 weeks ago #19

Wolfgang,
Nesting BEGIN USING is not really necessary.
This works, just like it does in C#
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
XSharp Development Team
The Netherlands
This email address is being protected from spambots. You need JavaScript enabled to view it.

Please Log in or Create an account to join the conversation.

USING syntax ? - help please ... 10 months 2 weeks ago #20

  • wriedmann
  • wriedmann's Avatar
  • Away
  • Posts: 1423
  • Karma: 6
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
Wolfgang Riedmann
Meran, South Tyrol, Italy
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it

Please Log in or Create an account to join the conversation.

  • Page:
  • 1
  • 2