In: Computer Science
I keep getting an error that I cannot figure out with the below VS2019 windows forms .net framework windows forms
error CS0029 C# Cannot implicitly convert type 'bool' to 'string' It appears to be this that is causing the issue
string id;
while (id = sr.ReadLine() != null)
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
namespace Dropbox13
{
public partial class SearchForm : Form
{
private List allStudent = new List();
public SearchForm()
{
InitializeComponent();
}
private void SearchForm_Load(object sender, EventArgs e)
{
using (StreamReader sr = new StreamReader("student.txt"))
{
string id;
while (id = sr.ReadLine() != null)
{
string name = sr.ReadLine();
int score = int.Parse(sr.ReadLine());
//create a student oject
Student s = new Student(id, Name, score);
//add to the List
allStudent.Add(s);
}
}
}
//------------- SearchForm.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
namespace Dropbox13
{
public partial class SearchForm : Form
{
private List<Student> allStudent = new List<Student>();
public SearchForm()
{
InitializeComponent();
}
private void SearchForm_Load(object sender, EventArgs e)
{
using (StreamReader sr = new StreamReader(@"D:\student.txt"))
{
string id;
while ( (id = sr.ReadLine()) != null ) //read id and if its not null then proceed
{
string name = sr.ReadLine();
int score = int.Parse(sr.ReadLine());
//create a student oject
Student s = new Student(id, name, score);
//add to the List
allStudent.Add(s);
Console.WriteLine( s.toString() ); //display student info to console
}
}
}
}
}
//-------------- end of SearchForm.cs
/*------------- student.txt
s1
Alan
60
s2
Bob
70
s3
Cary
80
------------- end of student.txt */
//-------------- Student.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Dropbox13
{
class Student
{
private string id;
private string name;
private int score;
public Student(string id, string name, int score)
{
this.id = id;
this.name = name;
this.score = score;
}
public string toString()
{
return id + " "+ name + " " + score;
}
}
}
//-------------- end of Student.cs
You just forgotten the braces as follows
while ( (id = sr.ReadLine()) != null ) //read id and if its not null then proceed
I have displayed the student details on console for testing..
thanks...
Output