Solved

How to work with ArrayList in C#?

Posted on 2009-07-14
3
465 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

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

830 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