C# Syntax erklärungsbedürftig

Deutschsprachiges X#-Forum – German language forum

Moderator: wriedmann

lagraf
Posts: 530
Joined: Thu Jan 18, 2018 9:03 am
Location: A

C# Syntax erklärungsbedürftig

Post by lagraf »

Hallo,
ich habe von Globaltrust ein C# Sample erhalten, wo die Zugriffe auf die Globaltrust RKSV Smartcard gezeigt werden. Daraus möchte ich mir die benötigten Codesequenzen in mein X# Modul einbauen.

Allerdings compiliert die XIDE das C# Sample mit einigen Fehlern. Kann mir jemand erklären, was folgende Befehle bedeuten und wie ich die abändere, damit der Compiler sie akzeptiert?

Code: Select all

int sw = TransmitCommand(_reader, sendBuffer, out _);	// out _ führt zu CS0103 und CS1503
_context?.Dispose();					// ? führt zu CS1525 und CS1003
LG
User avatar
SHirsch
Posts: 301
Joined: Tue Jan 30, 2018 8:23 am
Location: Germany

Re: C# Syntax erklärungsbedürftig

Post by SHirsch »

Da hast du vermutlich nicht die neuesten Compiler. Aus dem Code ist leider nicht zu sehen, welcher Typ als dritter Parameter angegeben ist.
Gehen wir mal von int aus. Dann müsste der Code so aussehen:
int result = 0;
int sw = TransmitCommand(_reader, sendBuffer, out result);
if (_context != null)
{
_context.Dispose();
}

Stefan
User avatar
wriedmann
Posts: 4079
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Re: C# Syntax erklärungsbedürftig

Post by wriedmann »

Meines Wissens nach bindet die XIDE einen C#-Compiler ein, der Teil des .NET Frameworks ist, und nicht den aus dem Visual Studio.
Das lässt sich aber in den XIDE-Einstellungen ändern.
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
lagraf
Posts: 530
Joined: Thu Jan 18, 2018 9:03 am
Location: A

Re: C# Syntax erklärungsbedürftig

Post by lagraf »

Hallo Stefan,
der originale Sourcecode von Globaltrust ist so wie ich ihn beschrieben habe und ich gehe ich davon aus, dass er korrekt ist:

könnte beim Transmit der 3. Parameter "out _" ev. Return ignorieren bedeuten (out null)?
_context mit einem ? sagt mir gar nichts?

Ich compiliere mit XIDE 2.24 und X# 2.24.0.1
Welcher Compiler wird verwendet wenn ich ein C# Programm compiliere?
LG
User avatar
Chris
Posts: 5630
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Re: C# Syntax erklärungsbedürftig

Post by Chris »

Guys,

Yeah, XIDE uses a c# compiler version found in a default location. I have no idea how to find automatically the latest/newer version, so you have to do it manually. Just go to the Program Files folder and search for csc.exe. There will be plenty, just pick the most recent and set this in Tools/Preferences/Compiler/c#. Then it should work.

But Franz, do you really want to use the c# version of the code, or do you prefer to translate it to X#?
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
User avatar
Chris
Posts: 5630
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Re: C# Syntax erklärungsbedürftig

Post by Chris »

Btw, the exact code translation to older c# versions is

Code: Select all

typeofparameter _discard;
int sw = TransmitCommand(_reader, sendBuffer, out _discard);

if (_context != null)
  _context.Dispose();
Where "typeofparameter" is the type of that 3rd param that is used by the TransmitCommand() method.
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
lagraf
Posts: 530
Joined: Thu Jan 18, 2018 9:03 am
Location: A

Re: C# Syntax erklärungsbedürftig

Post by lagraf »

Hi Chris,
your code compiles correct, thanks to you and Stefan! I will search for a newer csc.exe and install it.

First I want the C# sample to run, then I will extract some routines I need and translate it to X# (my RKSV App is in X#).

But I have another C# construct that brings error at ConsoleWriteLine:
error CS0103: The name 'X2' does not exist in the current context

Code: Select all

private static int TransmitCommand(ICardReader reader, byte[] sendBuffer, out byte[] responseData)
...
            byte sw1 = receiveBuffer[receivedBytes - 2];
            byte sw2 = receiveBuffer[receivedBytes - 1];
            int statusWord = (sw1 << 8) | sw2;
            return statusWord;

Other methods
...
            int sw = TransmitCommand(_reader, sendBuffer, out responseData);
//          Console.WriteLine($"   SW1: 0x{sw:X2}");	// Original Code
            Console.WriteLine("   SW1: 0x{0}", sw:X2);	// My corrected Code
How can I correct it?
Regards Franz
User avatar
Chris
Posts: 5630
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Re: C# Syntax erklärungsbedürftig

Post by Chris »

Hi Franz,

The "original code" version should work, why did you change it? Did you get a compiler error (maybe due to old c# compiler) ?

The "$" symbol stands for intrpolated string in c#, so you can include variable names within the string. An alternative way to write it is:

Code: Select all

Console.WriteLine("   SW1: 0x{0:X2}" , sw);

where {0} would stand for the first param you pass in WriteLine() after the string (so the "sw" value) and {0:X2} adds formatting to how the value is shown, in this case in heXadecimal format with 2 digits.
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
FFF
Posts: 1743
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

Re: C# Syntax erklärungsbedürftig

Post by FFF »

Chris wrote: Wed Nov 12, 2025 12:45 pm ... Just go to the Program Files folder and search for csc.exe. There will be plenty (++), just pick the most recent and set this in Tools/Preferences/Compiler/c#. ...
Isn't that the recipe how to do it automatically? ;)

( (++): just had a look, on my machine there are FORTYSEVEN instances. )
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
lagraf
Posts: 530
Joined: Thu Jan 18, 2018 9:03 am
Location: A

Re: C# Syntax erklärungsbedürftig

Post by lagraf »

Hi together,
with the last code changes the C# app now runs with my csc.exe.
Thank you for help!
Franz
Post Reply