code from c# to x#

This forum is meant for questions and discussions about the X# language and tools
Post Reply
Juraj
Posts: 161
Joined: Mon Jan 09, 2017 7:00 am

code from c# to x#

Post 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
NickFriend
Posts: 248
Joined: Fri Oct 14, 2016 7:09 am

code from c# to x#

Post 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
FFF
Posts: 1581
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

code from c# to x#

Post 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
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
Juraj
Posts: 161
Joined: Mon Jan 09, 2017 7:00 am

code from c# to x#

Post by Juraj »

thank you for the quick reply

Juraj
NickFriend
Posts: 248
Joined: Fri Oct 14, 2016 7:09 am

code from c# to x#

Post by NickFriend »

Hi Karl,

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

Nick
FFF
Posts: 1581
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

code from c# to x#

Post 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 :)
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
NickFriend
Posts: 248
Joined: Fri Oct 14, 2016 7:09 am

code from c# to x#

Post 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
User avatar
Chris
Posts: 4922
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

code from c# to x#

Post 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
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
Post Reply