xsharp.eu • code from c# to x#
Page 1 of 1

code from c# to x#

Posted: Thu Dec 14, 2017 12:52 pm
by Juraj
Hi All,

I need send mesage from pc to another pc. I found an example on net in C#, but I would like to use it in x# in core dialect. How to translate How to translate a designated line?
Or is there a better solution?

Sample:
//************************************************************
using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text;

namespace TcpEchoServer
{
public class TcpEchoServer
{
public static void Main()
{
Console.WriteLine("Starting echo server...");

int port = 1234;
TcpListener listener = new TcpListener(IPAddress.Loopback, port);
listener.Start();

TcpClient client = listener.AcceptTcpClient();
NetworkStream stream = client.GetStream();
// ************* This row ********************************************************************
StreamWriter writer = new StreamWriter(stream, Encoding.ASCII) { AutoFlush = true }
// *******************************************************************************************
StreamReader reader = new StreamReader(stream, Encoding.ASCII);

while (true)
{
string inputLine = "";
while (inputLine != null)
{
inputLine = reader.ReadLine();
writer.WriteLine("Echoing string: " + inputLine);
Console.WriteLine("Echoing string: " + inputLine);
}
Console.WriteLine("Server saw disconnect from client.");
}
}
}
}
//**********************************************************************************

Thanks for advice

Juraj

code from c# to x#

Posted: Thu Dec 14, 2017 1:28 pm
by NickFriend
Hi Juraj,

Not sure about the precise syntax in X#, but the C# is equivalent to

StreamWriter writer = new StreamWriter(stream, Encoding.ASCII);
writer.AutoFlush = true;

HTH

Nick

code from c# to x#

Posted: Thu Dec 14, 2017 1:34 pm
by FFF
Jurai,
the line:

Code: Select all

VAR writer := StreamWriter {stream, Encoding.ASCII} 
	writer:AutoFlush := TRUE 
IIRC, there's new syntax added to X# for setting the property already in the constructor, but this should work...

EDIT: got curious:

Code: Select all

VAR writer := StreamWriter{stream, Encoding.ASCII}{AutoFlush := TRUE}
was my idea, but help mentions: " This will only work if there are public properties", and AutoFlush is protect...
Wonder, how C# circumnavigates that hurdle ;)
Karl

code from c# to x#

Posted: Thu Dec 14, 2017 1:59 pm
by Juraj
thank you for the quick reply

Juraj

code from c# to x#

Posted: Thu Dec 14, 2017 2:23 pm
by NickFriend
Hi Karl,

I think you'll find AutoFlush is a public property.

Nick

code from c# to x#

Posted: Thu Dec 14, 2017 2:37 pm
by FFF
Nick,
hmm - in XIDE i had written "writer:", got in the pop-up list "PR Autoflush" - and read that as "protect", possibly this is "property"?
If so, i shouldn't get:
error XS9002: Parser: unexpected input 'writer'
for
LOCAL writer AS StreamWriter
writer := StreamWriter{stream, Encoding.ASCII}{AutoFlush:=TRUE} // here

or, probably, i misunderstand the concept ;)

Karl
PS: for this variation:
VAR writer := StreamWriter{stream, Encoding.ASCII}{AutoFlush:=TRUE}
i see:
error XS9002: Parser: unexpected input 'VAR'

@Chris, as an aside: the popup-dialog "errorpanel copied to clipboard", OK, is a PITA. Show a "Copied" and autodissappear after 1sec :)

code from c# to x#

Posted: Thu Dec 14, 2017 2:43 pm
by NickFriend
I don't use XIDE so I'm not sure, but logically only (publicly) visible methods and properties will show in the Intellisense.

I imagine the compiler error is something to do with the in-line instantiation syntax - I remember there was a discussion about this between Robert and Phil a while back, but I can't pin down the thread at this moment.

Nick

code from c# to x#

Posted: Thu Dec 14, 2017 3:54 pm
by Chris
Hi Karl (& all),

In x# this feature works at the moment only when instantiating on object through a default constructor (with no arguments):

Code: Select all

CLASS TestClass
	EXPORT nField AS INT
	PROPERTY Prop AS STRING AUTO
	CONSTRUCTOR()
		SUPER()
	RETURN
	CONSTRUCTOR(nAdditionalConstructor AS INT)
		SUPER()
	RETURN
END CLASS

FUNCTION Start() AS VOID
	VAR o := TestClass{}{nField := 1 , Prop := "test"}
	? o:nField
	? o:Prop
RETURN	
If you try to use the constructor with the int param (so call it with TestClass{123}), it will not work. Not sure why it was implemented like that, but I suspect when this feature was added in the language, it was only supported like that by the c# compiler that we were using back then, too. Since then c# got more sophisticated in this matter, but we didn't add the new syntax to remain on par. Just a guess, not absolutely sure, but will open a bug report about this and Robert or Nikos will check further.

About XIDE and the "Pr" in front of identifiers in the member completion list, yes it stands for "Property". "Fi" for Fields, "Me" for methods etc. About the dialog when copying contents from the Errors window, really this is that much annoying? Do you copy/ contents THAT often? But I just realized I still haven't added an option to copy only the current line...aargh, sorry about that, will do it now so I won't forget it again.

Chris