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.....
Skip(1)will skip the first character of the string of first line returned byreader.ReadLine(). Sincereader.ReadLine()by itself skips the first line,Skip(1)is completely superfluous.First create a class which can store your fields
(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
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!
The
usingstatement ensures that theStreamReaderis closed when finished.