Solved

How to work with ArrayList in C#?

Posted on 2009-07-14
3
470 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

Question has a verified solution.

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

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

615 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