C
C#2y ago
Wasabi2320

❔ How do I add a textfile into my list

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Filhantering { class Bil { private string regNr; private string märke; private string modell; private string årsmodell; public string RegNr { get { return regNr; } set { regNr = value; } } public string Märke { get { return märke; } set { märke = value; } } public string Modell { get { return modell; } set { modell = value; } } public string Årsmodell { get { return årsmodell; } set { årsmodell = value; } } public Bil(string regNr1, string märke1, string modell1, string årsmodell1) { ; this.RegNr = regNr1; this.Märke = märke1; this.Modell = modell1; this.Årsmodell = årsmodell1; } public override string ToString() { return " " + this.regNr + " " + this.Märke + " " + this.Modell + " " + this.Årsmodell; } } }
13 Replies
Angius
Angius2y ago
Protip: use autoproperties
Wasabi2320
Wasabi2320OP2y ago
Under button 3 im trying to combine the list from my textfile to my list called BilLista
Angius
Angius2y ago
Read the file's lines, iterate over them, split each by... tab? Whatever the separator is
var lines = await File.ReadAllLinesAsync("file.txt");
foreach (var line in lines)
{
var parts = line.Split('\t');
Bilista.Add(new Bil(parts[0], parts[1], parts[2], parts[3]))
}
var lines = await File.ReadAllLinesAsync("file.txt");
foreach (var line in lines)
{
var parts = line.Split('\t');
Bilista.Add(new Bil(parts[0], parts[1], parts[2], parts[3]))
}
Something like this
Wasabi2320
Wasabi2320OP2y ago
var lines = await File.ReadAllLinesAsync("file.txt"); foreach (var line in lines) { var parts = line.Split('\t'); BilLista.Add(new Filhantering(parts[0], parts[1], parts[2], parts[3])); } if I use that code then I get 2 errors CS4033 and CS0118
Angius
Angius2y ago
Yeah I don't know what those errors are Don't have the error codes memorized, alas
Angius
Angius2y ago
The first error is quite self-explanatory The second is because of my mistake that I fixed
Wasabi2320
Wasabi2320OP2y ago
What is the solution?
Angius
Angius2y ago
To the first one? Either make the calling method asynchronous, or use the synchronous version, ReadAllLines() For the latter, read the edited code
Wasabi2320
Wasabi2320OP2y ago
So I remove the async?
Angius
Angius2y ago
Re-read my message Yes, you remove the Async suffix And remove the await
Wasabi2320
Wasabi2320OP2y ago
Thank you Im a hs student so im not really familiar with those methods Grateful for your time
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server