Translating C# to X#

This forum is meant for questions and discussions about the X# language and tools
Post Reply
User avatar
Kees Bouw
Posts: 172
Joined: Wed Nov 06, 2019 11:35 am
Location: Netherlands

Translating C# to X#

Post by Kees Bouw »

There are a lot of C# code examples on the internet and sometimes I have trouble translating them, in the most efficient way, to X#. For example this piece of code:

Code: Select all

// Draw the custom border to appear 3-dimensional.
e.Graphics.DrawLines(SystemPens.ControlLightLight, new Point[] {
    new Point (0, e.Bounds.Height - 1), 
    new Point (0, 0), 
    new Point (e.Bounds.Width - 1, 0)
});
What would be the best X# equivalent?
(The code is on this page: https://learn.microsoft.com/en-us/dotne ... esktop-9.0)
Thanks in advance :)
User avatar
Chris
Posts: 5472
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Re: Translating C# to X#

Post by Chris »

Hi Kees,

The 2nd parameter is a (fixed size) array (of Point(s)), given it's elements on definition. For an INT array, the X# syntax to do this would be

aArrayOfInts := <INT> {1,2,3,6,7,0}

For an array of point, you need to instantiate Point objects withing the definition. It helps to divide this in two lines, by using an intermediate variable, to make it easier to read:

Code: Select all

LOCAL aPoints AS Point[] // single dim fixed size array of Point
aPoints := <Point> { Point {0, e:Bounds.Height - 1}, Point {0, 0}, Point {e:Bounds.Width - 1, 0} } // specify all the array elements at once

e:Graphics:DrawLines(SystemPens.ControlLightLight, aPoints)
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
User avatar
Kees Bouw
Posts: 172
Joined: Wed Nov 06, 2019 11:35 am
Location: Netherlands

Re: Translating C# to X#

Post by Kees Bouw »

Hi Chris,

Thank you very much!
Chris wrote: Mon Oct 13, 2025 10:31 am aArrayOfInts := <INT> {1,2,3,6,7,0}
Would this be the same as:

Code: Select all

aArrayOfInts := ARRAY OF INT
aArrayOfInts := {1,2,3,6,7,0}
Or does the < > make it into something special? It looks like some sort of cast.
Chris wrote: Mon Oct 13, 2025 10:31 am LOCAL aPoints AS Point[] // single dim fixed size array of Point
Would this be the same as:

Code: Select all

aPoints := ARRAY OF Point
Or is for example Point[] a dotnet array and an ARRAY OF Point an X# array?
Chris wrote: Mon Oct 13, 2025 10:31 am aPoints := <Point> { Point {0, e:Bounds.Height - 1}, Point {0, 0}, Point {e:Bounds.Width - 1, 0} } // specify all the array elements at once
It does work but I don't really understand the <> {} syntax. I also can't find it in the Help. Do you know where this is explained a little more?

Different topic: the horizontal line that you can add in this editor is hardly visible, it is grey on grey.

Thanks!

Kees.
User avatar
robert
Posts: 4892
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Re: Translating C# to X#

Post by robert »

Kees,

ARRAY OF INT is not the same as INT[]
ARRAY OF INT produces a dynamic array, just like the arrays in VO, but its elements are of type INT and not of type USUAL.

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
User avatar
Chris
Posts: 5472
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Re: Translating C# to X#

Post by Chris »

Hi Kees,

The <> syntax was introduced by Don in vulcan, so we used it also in X# for compatibility. I think the logic behind it was to use a similar pattern to the syntax used universally for generics, where you specify the generic type like this List<INT> or Dictionary<STRING,Point>.

If you don't like this, in X# you can also use this syntax:

aPoints := Point[] {3} { Point {0, e:Bounds.Height - 1}, Point {0, 0}, Point {e:Bounds.Width - 1, 0} }

The first {3} is used to denote the size of the array, rest of the line lists the array elements. "Point[]" is the universal way in .Net for specifying fixed size arrays.
Chris Pyrgas

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