1

I am posting my question again because i can not add my answers in it so here is the code

static void Main(string[] args)
{
    string fileA= "B.txt";
    IList listA= new ArrayList();

    FileReader(fileA, ref listA);

    for (int i = 0; i < listA.Count; i++)
    {
        Console.WriteLine(listA[i].ToString());
    }

    Console.ReadKey();
}

public static void FileReader(string filename, ref IList result)
{
    using (StreamReader sr = new StreamReader(filename))
    {
        string firstName;
        string SecondName;

        while (!sr.EndOfStream)
        {
            firstName= sr.EndOfStream ? string.Empty : sr.ReadLine();
            SecondName= sr.EndOfStream ? string.Empty : sr.ReadLine();

            result.Add(new Person(firstName, SecondName));
        }
    }
}

and i am getting values in my list as [0] ={"firstname","lastname"} [1]={"firsname2","secondname2"}

these values are attached with the Person class so if i want to change the lastname value of index [1] then how to do it? i can get the index [1] values but how to access the Person variable which are linked to that index

Magnus Grindal Bakken
  • 2,083
  • 1
  • 16
  • 22
user2740970
  • 137
  • 3
  • 4
  • 6
  • 3
    Is there any reason why you're using the non-generic `IList` type instead of an `IList`? And why are you using `ref` when you don't need to? (See http://pobox.com/~skeet/csharp/parameters.html) – Jon Skeet Sep 03 '13 at 17:30
  • Can you describe the Person class? – Lajos Arpad Sep 03 '13 at 17:33
  • I am confused how this relates to http://stackoverflow.com/questions/18597619/c-sharp-how-to-read-from-specific-index-in-listperson – Daniel Kelley Sep 03 '13 at 17:38
  • can you use a Dictionary ?? where the key is the value of a running counter inside your assignment loop and you can keep adding Person[s] to that dictionary...you can then use the key of the dictionary as the index and access the relevant Person object....unless I misunderstood your requirement... – kd. Sep 03 '13 at 17:46
  • thanks mate i have found the solution thanks again – user2740970 Sep 03 '13 at 17:50

1 Answers1

0

You're using an ArrayList which isn't an appropriate data structure since it will throw away the type information (unless you're stuck with .NET 1.1).

Try using a List(T).

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

static void Main(string[] args)
{
    var file = "B.txt";
    var list = new List<Person>();

    ReadFile(file, list);

    list[1].LastName = "newValue";
}

private static void ReadFile(string file, List<Person> personList)
{
    var items = File.ReadLines(file)
                    // Take each value and tag it with its index
                    .Select((s, i) => new { Value = s, Index = i })
                    // Put the values into groups of 2
                    .GroupBy(item => item.Index / 2, item => item.Value)
                    // Take those groups and make a person
                    .Select(g => new Person { FirstName =  g.FirstOrDefault(), LastName = g.Skip(1).FirstOrDefault() });

    personList.AddRange(items);
}
Dustin Kingen
  • 20,677
  • 7
  • 52
  • 92