I am not even sure how to call what I want to achieve but... probably the code will explain it.
Basically, I would like to create the frames commands as combinations of some statically defined arrays.
I would like to do something like this:
Command = ConcatArrays(commandPart1, commandPart2, commandPart2)
But it fails inside ConcatArrays as the list elements seem to be null.
And to use externally like this:
Frame.Frame1.Command
The ConcatArrays I took it from here: https://stackoverflow.com/a/3063504/15872
Is it something like this possible?
Thanks a lot for any help, I am quite new to C#.
public static class Frame
{
public class RequestModel
{
public byte[] Command { get; set; }
public int ReceiveLength { get; set; }
}
public static RequestModel Frame1 = new RequestModel
{
Command = ConcatArrays(commandPart1, commandPart2, commandPart2)
ReceiveLength = 16,
};
public static RequestModel Frame2 = new RequestModel
{
Command = ConcatArrays(commandPart1, commandPart3)
ReceiveLength = 16,
};
private static byte[] commandPart1 = new byte[] { 0x1, 0x02 };
private static byte[] commandPart2 = new byte[] { 0x3, 0x4 };
private static byte[] commandPart3 = new byte[] { 0x5, 0x6 };
public static T[] ConcatArrays<T>(params T[][] list)
{
var result = new T[list.Sum(a => a.Length)];
int offset = 0;
for (int x = 0; x < list.Length; x++)
{
list[x].CopyTo(result, offset);
offset += list[x].Length;
}
return result;
}
}
Commandis undefined. Your class as it is won't compile (not valid C# code). The only thing that I'm pretty sure is working is theConcatArrays()method. A correct (assumed from code correspondance) link could be this answer Can I use an array initializer to build one byte array out of another?