Zugriff auf SmartCard Reader mit PCSC

Deutschsprachiges X#-Forum – German language forum

Moderator: wriedmann

lagraf
Posts: 417
Joined: Thu Jan 18, 2018 9:03 am

Zugriff auf SmartCard Reader mit PCSC

Post by lagraf »

Ich versuche grad ein paar C# Beispiele nach X# zu übersetzen um auf einen USB SmartCardReader zuzugreifen. Dabei bin ich auf einen Code gestossen, den ich nicht übersetzen kann:

Code: Select all

int.TryParse(relin, out num)
In X# bekomme ich den Fehler "The name 'int' does not exist in the current context" (relin und num existieren).
Wie müßte der Code für X# lauten?
User avatar
wriedmann
Posts: 3644
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Zugriff auf SmartCard Reader mit PCSC

Post by wriedmann »

Hallo Franz,

Code: Select all

local nVal as int
nVal := 0
if int32.TryParse( cVal, ref nVal
....
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
Chris
Posts: 4562
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Zugriff auf SmartCard Reader mit PCSC

Post by Chris »

Guys,

Just a very minor correction, X# supports also the OUT keyword (which has slightly different semantics to REF), so this can be translated 1:1 from c# to X# as:

Code: Select all

Int32.TryParse( relin, OUT num)
Chris Pyrgas

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

Zugriff auf SmartCard Reader mit PCSC

Post by lagraf »

Hallo Wolfgang, Chris!
Was ist der Unterschied zwischen REF und OUT?
Bei folgendem Beispiel bekomme ich eine Warning "warning XS0219: The variable 'readerNames' is assigned but its value is never used", obwohl ich readerNames als Leerarray initialisiere (Dialect Core):

Code: Select all

LOCAL readerNames AS STRING[]
readerNames := STRING[]{ 0 }
sc := reader:Status(OUT readerNames, OUT state, OUT proto, OUT atr)
Wenn ich statt OUT readerNames aber REF readerNames verwende, dann compiliert der Code ohne Warning.
User avatar
wriedmann
Posts: 3644
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Zugriff auf SmartCard Reader mit PCSC

Post by wriedmann »

Hallo Franz,
den Unterschied zwischen REF und OUT lasse ich Chris erklären.
Die Fehlermeldung ist klar:

Code: Select all

warning XS0219: The variable 'readerNames' is assigned but its value is never used
Dieser Variablen wurde ein Wert zugewiesen, aber dieser Wert wurde anschließend nie verwendet.
Augenscheinlich trickst REF diese Kontrolle aus, dann eigentlich sollte sie auch hier kommen.
Ich nehme aber an, dass es hierfür einen Grund gibt.
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
lagraf
Posts: 417
Joined: Thu Jan 18, 2018 9:03 am

Zugriff auf SmartCard Reader mit PCSC

Post by lagraf »

Hallo Wolfgang,
ist "readerNames := STRING[]{ 0 }" nicht eine Anlage eines Leerarrays und somit bereits eine Zuweisung?
User avatar
wriedmann
Posts: 3644
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Zugriff auf SmartCard Reader mit PCSC

Post by wriedmann »

Hallo Franz,
es geht hier nicht um die Zuweisung (das wäre eine andere Fehlermeldung), sondern um die Verwendung der Variablen.

Code: Select all

function Hallo() as void
local c1 as string
local c2 as string

c2 := "Franz"

return
gibt zwei verschiedene Fehlermeldungen:

Code: Select all

warning XS0168: The variable 'c1' is declared but never used
warning XS0219: The variable 'c2' is assigned but its value is never used
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
lagraf
Posts: 417
Joined: Thu Jan 18, 2018 9:03 am

Zugriff auf SmartCard Reader mit PCSC

Post by lagraf »

Hallo Wolfgang,
ok verstehe, dann prüft der Compiler nicht nur, ob die Var vor einer Verwendung initialisiert wurde, sondern ob sie auch weiterverwendet wird, damit kein Code liegenbleibt der nichts bewirkt.
User avatar
Chris
Posts: 4562
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Zugriff auf SmartCard Reader mit PCSC

Post by Chris »

Guys,

In short, the difference between REF and OUT is that :
- with REF, you are supposed to pass an initialized value to the function/method and expect that possibly this function might modify its value when it returns
- with OUT, you are not supposed to pass a value to the function (the var is supposed to be uninitialized), but it's mandatory for the function to assign a value to this var, which you will receive back in your calling code.

Normally when a function expects an OUT param, then it is mandatory to pass it with OUT as well, and the same is true for REF, but for compatibility with vulcan, X# allows to mix those keywords, which is not really good, probably we should make the compiler stricter, at least when using a compiler option.

For the same reason X# is more lenient in the REF/OUT rules than c#, but for a complete list of "official" differences between OUT and REF in .Net, you can have a look here:

https://www.c-sharpcorner.com/UploadFil ... n-C-Sharp/
https://www.geeksforgeeks.org/differenc ... n-c-sharp/

this also explains the "IN" modifier keyword when declaring parameters:

https://www.pluralsight.com/guides/csha ... parameters

.
Chris Pyrgas

XSharp Development Team test
chris(at)xsharp.eu
User avatar
wriedmann
Posts: 3644
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Zugriff auf SmartCard Reader mit PCSC

Post by wriedmann »

Hi Chris,
thank you very much!
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Post Reply