Skip to main content

C#-Specific Syntax Sugar

This page only covers DarraPnS C# SDK A Unique and Convenient Way to Write. They Not a new feature. ..it simply repackages existing methods into a more intuitive C# syntax (tuple decomposition, ProcessData.In[i].AsInt16 indexing, etc.).

Optional; leaving it out does not affect the standard entry. —— Connected / State / ErrorCode, ProcessData.Read / ProcessData.Write, In[i] / Out[i] Use them as usual. Syntax sugar is just another way of writing code.

Namespace —— Darra.PnS. using Darra.PnS; That's all; there's no need to install any additional extension packages.


I. Tuple Deconstruction (Deconstruct)​

Tuple decomposition starting with C# 7. DarraPnS Two overloads: one line to extract the connection state, runtime state, and error code.

Get Connection + Status​

Writing StyleCall
Standards APIbool c = device.Connected; PnSState s = device.State;
Syntax Sugarvar (connected, state) = device;
var (connected, state) = device;
if (!connected)
Console.WriteLine(state);

Get Connection + Status + Error Code​

var (connected, state, error) = device;
if (error != PnSRuntimeErrorCode.Ok)
Console.WriteLine(device.ErrorMessage);

When to Use​

  • Logging / Diagnostic Loop: Write it a few times less device.Connected, device.State.
  • switch / pattern matching: is var (c, s, _) Break it down into separate lines.

II. Automatic Conversion of Process Data Indexes​

ProcessData.In[i] / ProcessData.Out[i] read and write by type from the current offset. Use In for reading I, and Out for writing Q.

Writing StyleCall
Standards APIobject v = device.ProcessData.In[2].AsValue(PnSDataType.Integer16);
Syntax Sugarshort v = device.ProcessData.In[2].AsInt16;
Standards APIdevice.ProcessData.Out[2].WriteValue(PnSDataType.Integer16, 123);
Syntax Sugardevice.ProcessData.Out[2].AsInt16 = 123;
byte i0 = device.ProcessData.In[0].Content;
short iw = device.ProcessData.In[2].AsInt16;
float f = device.ProcessData.In[8].AsFloat;

device.ProcessData.Out[0].Content = 0x11;
device.ProcessData.Out[2].AsInt16 = 123;
device.ProcessData.Out[8].AsFloat = 1.5f;

Write In[i].Content / In[i].AsInt16 Equal to throw InvalidOperationException(I read-only).

When to Use​

  • Just a few fields: I don't want to create a struct just for a few words.
  • Many fields: Or ProcessData.Read / ProcessData.Write.

List of Skills​

GroupingContent
Deconstruction of the Running StateDarraPnS.Deconstruct Two overloads: (connected, state) / (connected, state, error). error For PnSRuntimeErrorCode
Automatic Conversion of Process DataContent / AsInt8 / AsUInt8 / AsInt16 / AsUInt16 / AsInt32 / AsUInt32 / AsInt64 / AsUInt64 / AsFloat / AsDouble / AsBool / this[int] / Bits[i] / SetBit / AsValue / WriteValue