locked
How to concatenate multiple string arrays into single string array? RRS feed

  • Question

  • Hi All,

    I want to concatenate the string arrays into single string array. I have done it this way but i get error saying null in disableitem.

    public void Disable(string Layer,FlowLayoutPanel fopanel,string Item, CheckBox chk)
        {
          XItem dis = new XItem(Layer, Item);       
          string[] disablea = somethg;
          string[] disableb = somethg; 
          string[] disablec = somethg;
          string[] disabled = somethg;
    
          string[] joinedArray = new string[disablea.Length + disableb.Length + disablec.Length + disabled.Length];
          disablea.CopyTo(joinedArray, 0);
          disableb.CopyTo(joinedArray, disablea.Length);
    
          foreach (string disableitem in joinedArray)
          {
            foreach (Control found in Controls.Find(disableitem, true))
            {
              found.Enabled = false;
            }
            
    
          }

    regards

    sahana

    Thursday, July 8, 2010 6:26 AM

Answers

  • How about joining multiple arrays like below

    string[] a1 = new string[] { "test", "test1", "test2" };
    string[] a2 = new string[] { "test3", "test4", "test5" };
    string[] a3 = new string[] { "test6", "test7", "test8" };
    string[] a4 = new string[] { "test9", "test10", "test11"};
    var s1 = a1.Union(a2).Union(a3).Union(a4);
    foreach (string svalue in s1)
       Console.WriteLine(svalue);


    Nanda - Misys Software Solutions,Bangalore
    • Marked as answer by b4usahana Thursday, July 8, 2010 7:10 AM
    Thursday, July 8, 2010 7:01 AM
  • Hi,

    Thanks for your post.

    The joined array length is disablea.Length + disableb.Length + disablec.Length + disabled.Length but you only copy disablea.Length + disableb.Length elements to it so the rest elements will be null.

    Thus, following code may resolve your problem:

    string[] joinedArray = new string[disablea.Length + disableb.Length];
    

    Sincerely,

    Yasser


    LEARN HOW WPF IS FLEXIBLE IN PRESENTATION DURING A QUICK SIMPLE WALKTHROUGH:
    Walkthrough: Displaying multi column ComboBox by using Windows Presentation Foundation (WPF) data templating
    • Marked as answer by b4usahana Thursday, July 8, 2010 7:10 AM
    Thursday, July 8, 2010 6:45 AM
  • hello sahana,

    Like below code u can concatenate multiple string array in to single string array very easyly.

    string[] arr1 = new string[] { "hello" };
    
    string[] arr2 = new string[] { "How r u" };
    
    string[] arr3 = new string[] { "Good Day" };
    
    string[] joinarr = new string[arr1.Length + arr2.Length + arr3.Length];
    
    arr1.CopyTo(joinarr, 0);
    
    arr2.CopyTo(joinarr, arr1.Length);
    
    arr3.CopyTo(joinarr, arr1.Length + arr2.Length);
    
    

    may be it helps u...

    if any thing unclear feel free to ask me.... :)

    thanks,

    Nans11 :)

    • Marked as answer by b4usahana Thursday, July 8, 2010 7:10 AM
    Thursday, July 8, 2010 7:01 AM

All replies

  •  

     

    what's detail of exception? happen on  foreach (Control found in Controls.Find(disableitem, true))
    or 
     foreach (string disableitem in joinedArray)

    it seems your way is correct. what's about Array.Copy for it?

    public static void Copy(Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length)

    Thursday, July 8, 2010 6:43 AM
  • Hi,

    Thanks for your post.

    The joined array length is disablea.Length + disableb.Length + disablec.Length + disabled.Length but you only copy disablea.Length + disableb.Length elements to it so the rest elements will be null.

    Thus, following code may resolve your problem:

    string[] joinedArray = new string[disablea.Length + disableb.Length];
    

    Sincerely,

    Yasser


    LEARN HOW WPF IS FLEXIBLE IN PRESENTATION DURING A QUICK SIMPLE WALKTHROUGH:
    Walkthrough: Displaying multi column ComboBox by using Windows Presentation Foundation (WPF) data templating
    • Marked as answer by b4usahana Thursday, July 8, 2010 7:10 AM
    Thursday, July 8, 2010 6:45 AM
  • hi Yasser,

    Ya i have joined araay lenght as disablea.Length + disableb.Length + disablec.Length + disabled.Length but you only copy disablea.Length + disableb.Length

    i want to copy all. How do i.

    string[] joinedArray = new string[disablea.Length + disableb.Length];
    

    if i do this only disablea and disableb will be copied right?

    regards

    sahana

    Thursday, July 8, 2010 6:54 AM
  • foreach (Control found in Controls.Find(disableitem, true))
    

    Hi ,

    Key specified was either empty or null.
    Parameter name: key

    This is the error i get in the above line .

    regards

    sahana

    Thursday, July 8, 2010 6:58 AM
  • hello sahana,

    Like below code u can concatenate multiple string array in to single string array very easyly.

    string[] arr1 = new string[] { "hello" };
    
    string[] arr2 = new string[] { "How r u" };
    
    string[] arr3 = new string[] { "Good Day" };
    
    string[] joinarr = new string[arr1.Length + arr2.Length + arr3.Length];
    
    arr1.CopyTo(joinarr, 0);
    
    arr2.CopyTo(joinarr, arr1.Length);
    
    arr3.CopyTo(joinarr, arr1.Length + arr2.Length);
    
    

    may be it helps u...

    if any thing unclear feel free to ask me.... :)

    thanks,

    Nans11 :)

    • Marked as answer by b4usahana Thursday, July 8, 2010 7:10 AM
    Thursday, July 8, 2010 7:01 AM
  • How about joining multiple arrays like below

    string[] a1 = new string[] { "test", "test1", "test2" };
    string[] a2 = new string[] { "test3", "test4", "test5" };
    string[] a3 = new string[] { "test6", "test7", "test8" };
    string[] a4 = new string[] { "test9", "test10", "test11"};
    var s1 = a1.Union(a2).Union(a3).Union(a4);
    foreach (string svalue in s1)
       Console.WriteLine(svalue);


    Nanda - Misys Software Solutions,Bangalore
    • Marked as answer by b4usahana Thursday, July 8, 2010 7:10 AM
    Thursday, July 8, 2010 7:01 AM
  • Hi Behara,

    Thanks for your answer. This is what iawas looking for.

    Regards

    sahana

    Thursday, July 8, 2010 7:11 AM
  • each of the rest of new array is null

    Thursday, July 8, 2010 7:49 AM