Solved

How to work with ArrayList in C#?

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

747 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now