0

I have this array on a foreach loop:

StreamReader reader = new StreamReader(Txt_OrigemPath.Text);
reader.ReadLine().Skip(1);
string conteudo = reader.ReadLine();            
string[] teste = conteudo.Split(new[] { '*' }, StringSplitOptions.RemoveEmptyEntries);

foreach (string s in teste)
{
    string oi = s;
}

The line i'm reading contains a few fields like matriculation, id, id_dependent, birthday ... I have a CheckedListBox where the user selects wich fields he wants to select and what order he wants, according to this selection and knowing the order of each value in the array like(I know the first is matriculationthe second is id and the third is name), how could I select some of the fields, pass it's value to some variable and order them according to the checkedlistbox's order ? Hope I could be clear.

I tried this:

using (var reader = new StreamReader(Txt_OrigemPath.Text))
            {
                var campos = new List<Campos>();
                reader.ReadLine();
                while (!reader.EndOfStream)
                {
                    string conteudo = reader.ReadLine();
                    string[] array = conteudo.Split(new[] { '*' }, StringSplitOptions.RemoveEmptyEntries);
                    var campo = new Campos
                    {
                        numero_carteira = array[0]
                    };
                    campos.Add(campo);
                }
            }

Now How may I run over the list and compare its values with those fields selected by the user from the checkedlistbox ? Because if I instance the class again out the {} it's values will be empty...

Person p = new Person();
string hi = p.numero_carteira;  // null.....
5
  • What do you think reader.ReadLine().Skip(1); does? Because it does bugger all. Commented Jan 31, 2013 at 20:03
  • what are you trying to accomplish in the first place? Commented Jan 31, 2013 at 20:04
  • nope, not clear at all. I get that you want to display items from file, and that you have a checkedlistbox to select and order the items. Since you have the items in a list, you just need to compare the items in the list with the items in the checkedlistbox and create a new list with the items select in the correct order, then put that data into whatever your output is (ListView?) Commented Jan 31, 2013 at 20:04
  • @Antonie - hopefully that's a typo, but either way, he's reading and discarding the first line in each file. Commented Jan 31, 2013 at 20:08
  • @DavidHope not in each file... It just skip the first line of the file... And How would I compare these? I tried but couldnt make it =\ Commented Feb 1, 2013 at 10:44

1 Answer 1

1

Skip(1) will skip the first character of the string of first line returned by reader.ReadLine(). Since reader.ReadLine() by itself skips the first line, Skip(1) is completely superfluous.

First create a class which can store your fields

public class Person
{
    public string Matriculation { get; set; }
    public string ID { get; set; }
    public string IDDependent { get; set; }
    public string Birthday { get; set; }

    public override string ToString()
    {
        return String.Format("{0} {1} ({2})", ID, Matriculation, Birthday);
    }
}

(Here I used strings for simplicity, but you could use ints and DateTimes as well, which requires some conversions.)

Now, create a list where the persons will be stored

var persons = new List<Person>();

Add the entries to this list. Do not remove empty entries when splitting the string, because otherwise you will lose the position of your fields!

using (var reader = new StreamReader(Txt_OrigemPath.Text)) {
    reader.ReadLine();  // Skip first line (if this is what you want to do).
    while (!reader.EndOfStream) {
        string conteudo = reader.ReadLine();
        string[] teste = conteudo.Split('*');
        var person = new Person {
            Matriculation = teste[0],
            ID = teste[1],
            IDDependent = teste[2],
            Birthday = teste[3]
        };
        persons.Add(person);
    }
}

The using statement ensures that the StreamReader is closed when finished.

Sign up to request clarification or add additional context in comments.

5 Comments

@OliverJacot-Descombes I dont want to remove empty entries, because if I the empty entry is some field the user wants, i'll replace the "" for something else, you know ? I'll try it now
Oliver it says invalid expression term 'string why ?
@Ghaleon: I typed code the directly into StackOverflow answer box without testing it in Visual Studio. In Visual Studio I found two errors. #1: One missing ")" at the end of the using line. #2: It should not read string[xy] but teste[xy] in Matriculation = teste[0] and so on. Fixed the answer. You used the string split option StringSplitOptions.RemoveEmptyEntries; therefore I said that you wanted to remove empty entries.
Yes, thanks... One last thing, how could I order them to write on the streamwriter.writeline(); ? I need to compare these fields with those that were selected by the user from the CheckedListBox. Because if I instance the class person again the values will be empty
If you override the ToString method of the Person class (see my edit above) to return what you want to display in your CheckedListBox, you can add all the persons to the CheckedListBox by simply setting its DataSource property to the persons list: myCheckedListBox.DataSource = persons;. Now you can get the actual person with: Person p = (Person)myCheckedListBox.SelectedItem;. Or if (myCheckedListBox.GetItemChecked(i)) { Person p = (Person)myCheckedListBox.Items[i]; }

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.