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

TOPIC: code from c# to x#

code from c# to x# 1 year 3 months ago #1

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

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

code from c# to x# 1 year 3 months ago #2

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

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

code from c# to x# 1 year 3 months ago #3

Jurai,
the line:
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:
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

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

Last edit: by FFF. Reason: additional info

code from c# to x# 1 year 3 months ago #4

thank you for the quick reply

Juraj

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

code from c# to x# 1 year 3 months ago #5

Hi Karl,

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

Nick

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

code from c# to x# 1 year 3 months ago #6

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 :)

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

code from c# to x# 1 year 3 months ago #7

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

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

code from c# to x# 1 year 3 months ago #8

Hi Karl (& all),

In x# this feature works at the moment only when instantiating on object through a default constructor (with no arguments):
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
XSharp Development Team
chris(at)xsharp.eu

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

  • Page:
  • 1