❔ 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
Protip: use autoproperties
Under button 3 im trying to combine the list from my textfile to my list called BilLista
Read the file's lines, iterate over them, split each by... tab? Whatever the separator is
Something like this
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
Yeah I don't know what those errors are
Don't have the error codes memorized, alas
The first error is quite self-explanatory
The second is because of my mistake that I fixed
What is the solution?
To the first one? Either make the calling method asynchronous, or use the synchronous version,
ReadAllLines()
For the latter, read the edited codeSo I remove the async?
Re-read my message
Yes, you remove the
Async
suffix
And remove the await
Thank you
Im a hs student so im not really familiar with those methods
Grateful for your time
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.