Solved

How to work with ArrayList in C#?

Posted on 2009-07-14
3
464 Views
Last Modified: 2013-12-17

Hello group,

I'm reading a log file where I need 4 parameters from each line of it so I'm using ArrayList but the problem is that due to being new to C# I'm not sure if this works I just know how to create and add to an ArrayList so basically what I need is to know how to create a dynamic array, since the lines of log file are not defined and could change.
Any help is appreciated.

Regards,
ak
0
Comment
Question by:akohan
3 Comments
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 250 total points
ID: 24851986
An ArrayList is a dynamic array.

You would create a new one by

  ArrayList list = new ArrayList();

You can add entries by using the Add member function:

   if (Directory.Exists(directory))
  {
      String[] e = Directory.GetFiles(directory, "*.dll");
      for (int i = 0; i < e.Length; i++)
     {
          list.Add(e[i]);
     }
  }

If you search for C# ArrayList sample in MSDN or Google you'll get cillions of working samples.

e. g. at  http://msdn.microsoft.com/en-us/library/z9hsy596(VS.80).aspx?ppud=4
0
 

Author Comment

by:akohan
ID: 24853302


Thanks indeed.

regards,
ak
0
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 24855485
Unless you are stuck with framework 1.1, you should not use ArrayList at all. You should use a strongly types List<T> instead.

As you have several values from each line, you can for example use a list of string arrays:

List<string[]> log = new List<string[]>();
foreach (string line in File.ReadAllLines("log.txt")) {
   log.Add(line.Split(' '));
}

Or you can create a class to represent a log line:

public class LogLine {
   public string Param1 { get; private set; }
   public string Param2 { get; private set; }
   public string Param3 { get; private set; }
   public string Param4 { get; private set; }
   public LogLine(string line) {
      string[] params = line.Split(' ');
      Param1 = params[0];
      Param2 = params[1];
      Param3 = params[2];
      Param4 = params[3];
   }
}

The you use a list of LogLine objects:

List<LogLine> log = new List<LogLine>();
foreach (string line in File.ReadAllLines("log.txt")) {
   log.Add(new LogLine(line));
}
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Automated testing suggestions? 2 47
Regular expression help 2 25
Help with preventing downloading a zip file 10 37
DataTables + iCheck + pagination Issue 2 23
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question