4

I'm new to C#, I need to do the following:

I need to declare a List

List<string[]> myList = new List<string[]>();

Now the conditions and adding to the list should work as follows (in pseudo code):

for int i = 0 to N
if nNumber == 1 add string[i] to myList[0]
else to myList[1]

In the end, I would have 2 members in my list, each member having several strings.

I am not sure how I can add these strings to the members as the method List(Of T).Add inserts members to the list and I do not want this.

6
  • 2
    Your list currently holds string[] which are string arrays. It's not clear if this is intended or you meant to hold a list of strings List<string>. Commented Nov 24, 2011 at 11:47
  • memebr 1 myList[0] will have string1, string2, etc, and the second member myList[1] will having another bunch of strings Commented Nov 24, 2011 at 11:50
  • 1
    ... i.e., a List<List<string>> Commented Nov 24, 2011 at 11:50
  • @Sunscreen: Why use an array? will it always have exactly 2 members? Commented Nov 24, 2011 at 11:51
  • I just need 2 members. If there is another imlementation is very welcome... Commented Nov 24, 2011 at 12:05

4 Answers 4

3
        var myList = new List<string>[] { new List<string>(), new List<string>() }; 

        for (int i = 0; i <= 12; i++)
        {
            if (i == 1)
                myList.Last().Add(i.ToString());
            else
                myList.First().Add(i.ToString());
        }
Sign up to request clarification or add additional context in comments.

Comments

3

From what I can see, you don't want a list of string arrays, you want a 2-element array of lists of strings:

var myList = new List<string>[] { new List<string>(), new List<string>() };

Now you should be able to implement the algorithm exactly as you specified.

7 Comments

It sounds promissing, and how can I add? possibly like that myList[0].Add() ??
Though I am using C# 2.0 and I cannot declare varcutom type... i sthere away to do it?
@Sunscreen: Then replace var with List<string>[].
Again compalins that Iit cannot convert List<string>[] to List<string>[]
@Sunscreen: Can you copy the message in verbatim? Cannot convert List<string>[] to List<string>[] doesn't make sense...
|
3

This can be also performed as List of List of string as below:

    List<List<string>> strList = new List<List<string>>();
    for (int i = 0; i < N; i++)
    {
        if (nNumber == 1)
            strList[0].Add(i.ToString());
        else
            strList[1].Add(i.ToString());
    }

Comments

1

In my csv parsing class, I have the following defined

enum gic_rpt_columns { AGYDIV, STS, GICID, LASTNAME, FIRSTNAME, COVERAGE, DESCRIPTION , PREMIUM, RUNDATE, BILMO, EMPLOYERPERCENT };

public class csv_parser
{
    .
    .
    .
    private string[]        m_column_headings;
    List<string[]>          m_csv_array = new List<string[]>();
    .
    .
    .

The following code will add my column headings and then the rows in the csv file

               using (TextFieldParser parser = new TextFieldParser(m_csv_path))
                {
                    parser.TextFieldType = FieldType.Delimited;
                    parser.SetDelimiters(",");

                    if (!parser.EndOfData)
                    {
                        m_column_headings = parser.ReadFields();
                        m_csv_array.Add(m_column_headings);
                    }

                    while (!parser.EndOfData)
                    {
                        string[] fields = parser.ReadFields();
                        m_csv_array.Add(fields);
                        m_row_count++;
                    }
                }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.