Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to work with ArrayList in C#?

Posted on 2009-07-14
3
Medium Priority
?
471 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 1000 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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 tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

670 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