user222427
user222427

Reputation:

C# Trouble adding to a generic list

This is a follow up question from another post but that post was answered. I have a for loop that I want to add three items to a generic class and I'm not sure how to do that. How do I add those items?

private static void TestResults()
{
    List<Record> Records = new List<Record>();

    for (int i = 0; i < ProxyList.Count; i++)
    {
        string[] split = List[i].Split('|');
        // This is what i dont know how to do
        //  split[0] = Name, split[1] = SomeValue and split[3] = OrderNr
    }
}

class Records
{
    public static string Name { get; set; }
    public static int SomeValue { get; set; }
    public static int OrderNr { get; set; }
}

Upvotes: 2

Views: 640

Answers (3)

Amen Ayach
Amen Ayach

Reputation: 4348

I'm not sure why you use static !!! but try this :

private static void TestResults()
    {
List<Record> Records = new List<Record>();

for (int i = 0; i < ProxyList.Count; i++)
        {;
            string[] split = List[i].Split('|');
            Records.Add(new Record() {Name = Namesplit[0] , SomeValue = split[1], OrderNr = split[3]}) ;

        }
}

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500385

Well for one thing, your properties must not be static. You want different data for each instance, right? So they need to be instance properties. Personally I'd also make the type immutable, but that's another matter:

class Record // Changed from Records; each object is only a single record...
{
    public string Name { get; set; }
    public int SomeValue { get; set; }
    public int OrderNumber { get; set; }
}

private static List<Record> ConvertRecords(IEnumerable<string> lines)
{
    List<Record> records = new List<Record>();

    foreach (string line in lines)
    {
        string[] split = line.Split('|');
        Record record = new Record {
            Name = split[0],
            SomeValue = int.Parse(split[1]),
            OrderNumber = int.Parse(split[2]);
        };
        records.Add(record);
    }
}

As of C# 3 / .NET 3.5, a more idiomatic approach would be to use LINQ:

private static List<Record> ConvertRecords(IEnumerable<string> lines)
{
    return (from line in lines
            let split = line.Split('|')
            select new Record {
                Name = split[0],
                SomeValue = int.Parse(split[1]),
                OrderNumber = int.Parse(split[2]);
            }).ToList();
    }
}

... on the other hand, that's relatively advanced if you're really just starting to learn the language.

To be honest, Stack Overflow is better for asking specific questions than structured learning - I suggest you get hold of a good book, such as C# 4 in a Nutshell.

Upvotes: 2

JaredPar
JaredPar

Reputation: 754665

The first step is to associate the fields with instances of Records vs. the type itself. This is done by removing the static modifier

class Records
{
    public string Name { get; set; }
    public int SomeValue { get; set; }
    public int OrderNr { get; set; }
}

To actually create instances try the following

for (int i = 0; i < ProxyList.Count; i++) {
  string[] split = List[i].Split('|');
  Records record = new Records() {
    Name = split[0]
    SomeValue = Int32.Parse(split[1])
    OrderNr = Int32.Parse(split[2])
  };
  Records.add(record);
}

This particular example uses an object initializer to combine the acts of creating the object and settings its fields. This could be expanded into the longer form as follows

for (int i = 0; i < ProxyList.Count; i++) {
  string[] split = List[i].Split('|');
  Records record = new Records();
  record.Name = split[0];
  record.SomeValue = Int32.Parse(split[1]);
  record.OrderNr = Int32.Parse(split[2]);
  Records.add(record);
}

The Int32.Parse method will throw an exception if the item in the original string wasn't a number. If this is a possibility (think bad input) then you'll want to wrap the creation of Records with a try / catch statement

Upvotes: 4

Related Questions