I am having difficulting locating a piece of code that will successfully read a file and store the information in a separate array.
Someone has suggested
var lines = File.ReadAllLines("peter_testresults.txt");
in a past question but “var” is not recognized.
CODE:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//return a pass/fail
//return number of correct answers
//return number of incorrect answers
//return index figures of incorrect values
correctAnswers ={B,D,A,A,C,A,B,A,C,D,B,C,D,A,D,C,C,B,D,A}
private string determinePassOrFail (); //return a pass/fail
string[] lines= File.ReadAllLines("peter_testresults.txt");
var c = correctAnswers.Where((x, i) => x.Equals(lines[i])).Count();
private int numberCorrectAnswers(); //return number of correct answers
{
}
private int numberIncorrectAnswers(); //return number of incorrect answers
{
}
private string questionsIncorrectAnswers(); //return index figures of incorrect
values
{
}
}
}
If you can’t use
var(implicitly typed local variables) then that suggests you’re using a C# 2 compiler (e.g. VS2005), and therefore almost certainly .NET 2. (I’m assuming you’re not stuck with C# 1…)Alternatively, you could be trying to use this as a field:
That won’t work as fields can’t be implicitly typed – but I’d argue that doing that IO in a field initializer is a bad idea anyway…
This should still work though:
as
File.ReadAllLineswas first introduced in .NET 2.As an aside, I’d strongly advise you to try to upgrade to a more modern version of C#…