Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array problem

Status
Not open for further replies.

princesszea

Technical User
Aug 11, 2008
33
GB
Hi,

I have 2 arrays which I’m comparing values which works fine.

So basically I get the results from 2 arrays and compare the values and then add the missing files to a thrid array. The problem I have is when I added the missing files to the array I will like to add some more information to the missing file array.

e.g
array 1 array2 missingFileNames
1 1 4
2 2 5
3 3
4
5



So basically array2 has more information that I need to add to the missingFileNames. So instead of 4 I have 4 , Tim and 5, john
At the moment array2 only returns 1 item .


Code:
            ArrayList array2 = FileProcessor. Array2();
            ArrayList array1 =  FileProcessor. array1 ();
            ArrayList missingFileNames = new ArrayList();
            
            if (array1.Count > 0 && array2.Count > 0)
            {
                foreach (string fileName in array2)
                {
                    if (!array1.Contains(fileName))
                    {
                        
                        missingFileNames.Add(fileName);
                    }
                }
            }
        }

Thanks in advance
 
Your code conflicts with your drawn example:

To make the drawing match the code, it should be:

Code:
ArrayList array2 = FileProcessor. Array2();
ArrayList array1 =  FileProcessor. array1 ();
ArrayList missingFileNames = new ArrayList();

if (array1.Count > 0 && array2.Count > 0)
    {
    foreach (string fileName in array1)
        {
        if (!array2.Contains(fileName))
            {
            missingFileNames.Add(fileName);
            }
        }
    }

HTH

Chew

10% of your life is what happens to you. 90% of your life is how you deal with it.
 
Also, keep in mind that when you work with ArrayList, "boxing" occurs, which means that your value-type (string) is converted into a reference-type (object) just before you "Add" the member. Likewise, unboxing occurs when you attempt to read/delete members of an ArrayList.

C.


10% of your life is what happens to you. 90% of your life is how you deal with it.
 
Thanks for your replies.

The problem is in array1 I'm getting one value string filename = items[0].ToString(); as this is the value that contains the filename to compare with array2. This works fine. what i need is to get items[0].ToString(); and items[1].ToString(); from array1. I need items[0].ToString(); to compare to the file name in array2 and if the file is not in array2 i need items[0].ToString(); and items[1].ToString(); to be added to the missingfilenames array. The code for array1 is below.

Thanks again


Code:
        public static ArrayList array1()
        {
            string csvfile = ConfigurationManager.AppSettings["CSVFile"].ToString
            string delimiter = ",";
         

            StreamReader sr = new StreamReader(csvfile);
            sr.ReadLine();
            ArrayList fileitems = new ArrayList();

            try
            {

                {
                    while (sr.EndOfStream == false)
                    {

                        string r = sr.ReadLine();
                        string[] items = r.Split(delimiter.ToCharArray());
                        string filename = items[0].ToString();
                       
                        if (r.Length == 0) continue;
                        fileitems.Add(filename.ToLower());
                    }
                }
            }
            finally
            {
                sr.Close();
            }
            return fileitems;
        }
 
if you want to add "more" information, create an object to hold the "extra" information and put them in to the array.
Code:
var dtos = new List<MyDto>();
dtos.Add(new MyDto{FileName = filename, I = 1, D = DateTime.Now});
where MyDto looks like this
Code:
class MyDto
{
  public string FileName {get;set;}
  public int I {get;set;}
  public DateTime D {get;set;}
}

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top