0

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;
}

}
7
  • What are frames commands? Commented Mar 2, 2018 at 0:35
  • @LukeHutton I was referring to Frame1 -> Command, Frame2 -> Command or is not what are you asking? Commented Mar 2, 2018 at 0:50
  • Oh I see, I thought perhaps this was a known design pattern perhaps. What is your question then? By possible, do you mean will your code run? We need the ability to verify your requirements Commented Mar 2, 2018 at 0:56
  • I would like to be able to write this: Command = ConcatArrays(commandPart1, commandPart2, commandPart2) but it fails @LukeHutton Commented Mar 2, 2018 at 0:58
  • The linked page context is not related to handling Arrays. Command is undefined. Your class as it is won't compile (not valid C# code). The only thing that I'm pretty sure is working is the ConcatArrays() 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? Commented Mar 2, 2018 at 1:52

1 Answer 1

2

You cannot refer to static members below. For example, Frame1 member referencing commandPart1 static member defined below it in the static class.

One way to fix is to define the static members above where referenced. The following tests pass:

    [Test]
    public void Frame1CommandShouldIncludeParts1and2and2()
    {
        var expected = new byte[] {0x1, 0x02, 0x3, 0x4, 0x3, 0x4};
        var actual = Frame.Frame1.Command;
        Assert.AreEqual(expected, actual);
    }

    [Test]
    public void Frame2CommandShouldIncludeParts1and3()
    {
        var expected = new byte[] {0x1, 0x02, 0x5, 0x6};
        var actual = Frame.Frame2.Command;
        Assert.AreEqual(expected, actual);
    }

    public class RequestModel
    {
        public byte[] Command { get; set; }
        public int ReceiveLength { get; set; }
    }

    public static class Frame
    {
        private static readonly byte[] CommandPart1 = { 0x1, 0x02 };
        private static readonly byte[] CommandPart2 = { 0x3, 0x4 };
        private static readonly byte[] CommandPart3 = { 0x5, 0x6 };

        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 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;
        }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

The ConcatArrays() method could just be return list.SelectMany(a1 => a1.Select(a2 => a2)).ToArray(); (for the Lambda addicted :)
Thank you so much, Luke! I can't believe I wasted so much time for this :)) just because of position LOL

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.