Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable initalisation in while loop

I have a function that reads a file in chunks.

public static DataObject ReadNextFile(){ ...}

And dataobject looks like this:

public DataObject
{
   public string Category { get; set; }

   // And other members ...
}

What I want to do is the following basically

List<DataObject> dataObjects = new List<DataObject>();

while(ReadNextFile().Category == "category")
{
   dataObjects.Add(^^^^^ the thingy in the while);
}

I know it's probably not how it's done, because how do I access the object I've just read.

like image 961
Timo Willemsen Avatar asked Dec 13 '10 17:12

Timo Willemsen


2 Answers

This is subjective, but I hate this pattern (and I fully recognize that I am in the very small minority here). Here is how I do it when I need something like this.

var dataObjects = new List<DataObject>();
while(true) {
    DataObject obj = ReadNextFile();
    if(obj.Category != "category") {
        break;
    }
    dataObjects.Add(obj);
}

But these days, it is better to say

List<DataObject> dataObjects = GetItemsFromFile(path)
                                   .TakeWhile(x => x.Category == "category")
                                   .ToList();

Here, of course, GetItemsFromFile reads the items from the file pointed to by path and returns an IEnumerable<DataObject>.

like image 45
jason Avatar answered Sep 21 '22 14:09

jason


I think what you're looking for is:

List<DataObject> dataObjects = new List<DataObject>();

DataObject nextObject;
while((nextObject = ReadNextFile()).Category == "category")
{
   dataObjects.Add(nextObject);
}

But I wouldn't do that. I'd write:

List<DataObject> dataObject = source.ReadItems()
                                    .TakeWhile(x => x.Category == "Category")
                                    .ToList();

where ReadItems() was a method returning an IEnumerable<DataObject>, reading and yielding one item at a time. You may well want to implement it with an iterator block (yield return etc).

This is assuming you really want to stop reading as soon as you find the first object which has a different category. If you actually want to include all the matching DataObjects, change TakeWhile to Where in the above LINQ query.

(EDIT: Saeed has since deleted his objections to the answer, but I guess I might as well leave the example up...)

EDIT: Proof that this will work, as Saeed doesn't seem to believe me:

using System;
using System.Collections.Generic;

public class DataObject
{
    public string Category { get; set; }
    public int Id { get; set; }
}

class Test
{

    static int count = 0;

    static DataObject ReadNextFile()
    {
        count++;
        return new DataObject
        {
            Category = count <= 5 ? "yes" : "no",
            Id = count
        };
    }

    static void Main()
    {
        List<DataObject> dataObjects = new List<DataObject>();

        DataObject nextObject;
        while((nextObject = ReadNextFile()).Category == "yes")
        {
            dataObjects.Add(nextObject);
        }

        foreach (DataObject x in dataObjects)
        {
            Console.WriteLine("{0}: {1}", x.Id, x.Category);
        }
    }
}

Output:

1: yes
2: yes
3: yes
4: yes
5: yes

In other words, the list has retained references to the 5 distinct objects which have been returned from ReadNextFile.

like image 157
Jon Skeet Avatar answered Sep 17 '22 14:09

Jon Skeet