locked
How to merge two arrays RRS feed

  • Question

  • User-985212943 posted

    This should be easy but I'm having trouble doing it.  I need to combine two arrays of type Fileinfo into a new array so that I can sort it.  How would I combine these two arrays:

    Dim arrayJPG() As FileInfo = dirInfo.GetFiles("*.jpg")

    Dim arrayGif() As FileInfo = dirInfo.GetFiles("*.gif")

    Wednesday, April 4, 2007 5:14 PM

Answers

  • User187056398 posted

    You have to make a third array.  (I know this is in C# but it should translate easily).

            FileInfo[] Result1 = dirInfo.GetFiles("*.sys");
            FileInfo[] Result2 = dirInfo.GetFiles("*.txt");
    
            FileInfo[] AllResults = new FileInfo[Result1.Length + Result2.Length];
    
            Result1.CopyTo(AllResults, 0);
            Result2.CopyTo(AllResults, Result1.Length);
    
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 4, 2007 7:14 PM

All replies

  • User187056398 posted

    You have to make a third array.  (I know this is in C# but it should translate easily).

            FileInfo[] Result1 = dirInfo.GetFiles("*.sys");
            FileInfo[] Result2 = dirInfo.GetFiles("*.txt");
    
            FileInfo[] AllResults = new FileInfo[Result1.Length + Result2.Length];
    
            Result1.CopyTo(AllResults, 0);
            Result2.CopyTo(AllResults, Result1.Length);
    
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 4, 2007 7:14 PM
  • User-985212943 posted
    I get the following when I try to do a databind (trying to databind the results to a listbox):
    Object reference not set to an instance of an object.] 
    Wednesday, April 4, 2007 8:16 PM
  • User743145481 posted

    His code example is right; here's it translated into VB.NET:

    Dim Result1() As System.IO.FileInfo = dirInfo.GetFiles("*.sys")
    Dim Result2() As System.IO.FileInfo = dirInfo.GetFiles("*.txt")
    
    ' Note the -1 is there because VB.NET takes the initializer
    '  as the Upper Bound (Highest Index), while C# takes it as the length
    Dim AllResults(Result1.Length + Result2.Length - 1) As System.IO.FileInfo
    
    ' copy the .sys files to the front
    Result1.CopyTo(AllResults, 0)
    ' copy the .txt files to the end
    Result2.CopyTo(AllResults, Result1.Length)
    Wednesday, April 4, 2007 8:46 PM
  • User187056398 posted
    Nice.
    Wednesday, April 4, 2007 8:55 PM
  • User-985212943 posted

    ' Note the -1 is there because VB.NET takes the initializer
    '  as the Upper Bound (Highest Index), while C# takes it as the length
    Dim AllResults(Result1.Length + Result2.Length - 1) As System.IO.FileInfo


     Thanks Picky, 

    I had ran it through an online converter and it missed the -1 on   (Result1.Length + Result2.Length - 1)  Works great.

    Thursday, April 5, 2007 9:36 AM
  • User853444624 posted

    this is bit late :) Some one will find useful...check out this link

     http://codehighstreet.com/DiscussionBoard/Thread.aspx?TopicID=3

     

    Wednesday, September 24, 2008 1:14 PM
  • Wednesday, September 24, 2008 1:16 PM