Reputation: 1216
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 matriculation
the 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.....
Upvotes: 0
Views: 153
Reputation: 112762
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.
Upvotes: 1