0

I have 2 arrays of objects and I want to add one to the other. This seems simple enough but nothing seems to work. Here is what I am trying now.

Module[] StaticModules;
Module[] DynamicModules;

Info = Database.Devices[inx];
//Modules = Database.GetDeviceModules(Info);

StaticModules = Database.GetDeviceModules(Info);
DynamicModules = Database.GetDeviceModules(new int[] { 23, 24, 25, 26, 27, 28, 29, 30 });

Array.Resize(ref StaticModules, StaticModules.Length + DynamicModules.Length);
Array.Copy(StaticModules, DynamicModules, DynamicModules.Length);

Modules = StaticModules;

Everything seems ok until the copy which seems to take forever and doesn't appear to do a thing. At one point I had the watch window open and after the copy all my vars turned red and I saw a message that said I had to refresh because the last function timed out.

3
  • 1
    the first parameter to Copy should be the source, your resize call suggests you think it is the destination. Commented May 18, 2016 at 18:42
  • You are trying to copy the DynamicModules values into StaticModules, is that correct? Commented May 18, 2016 at 18:47
  • Do you have to use arrays or can use more dynamic structures like List? Commented May 18, 2016 at 18:50

4 Answers 4

3

Your code seems overly complicated. Why not do:

Modules = StaticModules.Concat(DynamicModules).ToArray();
Sign up to request clarification or add additional context in comments.

1 Comment

I tried the concat route but I couldn't get that to work either. I'll try what you suggest here. I think i was doing Modules.Concat(DynamicModules) and not seeing any change.
1

Array.Copy method has several overloads. You might need to use this one Array.Copy. The overload you chose overwrites elements of the destination array as they pastes items starting form index 0. Also as @muratgu said first argument is the source array.

Module[] StaticModules;
    Module[] DynamicModules;

    Info = Database.Devices[inx];
    //Modules = Database.GetDeviceModules(Info);

    StaticModules = Database.GetDeviceModules(Info);
    var len = StaticModules.Length;

    DynamicModules = Database.GetDeviceModules(new int[] { 23, 24, 25, 26, 27, 28, 29, 30 });



    Array.Resize(ref StaticModules, StaticModules.Length + DynamicModules.Length);
    Array.Copy(DynamicModules, 0, StaticModules, len, DynamicModules.Length);

    Modules = StaticModules;

Comments

1

You can use CopyTo for this.

Module[] StaticModules;
Module[] DynamicModules;
Module[] FinalModules = new Module[StaticModules.Length + DynamicModules.Length];

StaticModules.CopyTo(FinalModules, 0);
DynamicModules.CopyTo(FinalModules, StaticModules.Length);

Comments

0

This should work for you. I tried it in a compiler to see the results.

Array.Copy(DynamicModules, 0, StaticModules, DynamicModules.Length, DynamicModules.Length);

Full code:

Module[] StaticModules;
Module[] DynamicModules;

Info = Database.Devices[inx];
//Modules = Database.GetDeviceModules(Info);

StaticModules = Database.GetDeviceModules(Info);
DynamicModules = Database.GetDeviceModules(new int[] { 23, 24, 25, 26, 27, 28, 29, 30 });

Array.Resize(ref StaticModules, StaticModules.Length + DynamicModules.Length);
Array.Copy(DynamicModules, 0, StaticModules, DynamicModules.Length, DynamicModules.Length);

Modules = StaticModules;

Comments

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.