Sunday, September 28, 2014

Creating a function to read line in C#

So far my code is as follows:


...
namespace read_file
{
class Program
{
static void Main(string[] args)
{
TextReader namefile = new StreamReader(@"E:\Code in C Sharp\read-file\read-file\test.txt");
string line = namefile.ReadLine();

...
}

static public string[] Returnval(string line)
{
var Returnval = line.Split('\t');
return Returnval;
}
}
}




I tried to add a new line to Returnval function which is as follows:



string line = namefile.Readline();

I got an error saying namefile is not in context.




Another approach:



class Program
{
static TextReader namefile; // ***
static void Main(string[] args)
{
//TextReader namefile = new StreamReader(@"E:\Code in C Sharp\read-file\read-file\test.txt");
namefile = new StreamReader(@"E:\Code in C Sharp\read-file\read-file\test.txt"); // ***
string line = namefile.ReadLine();
...
}

static public string[] Returnval(string line)
{
string linex = namefile.ReadLine(); // ***
var Returnval = line.Split('\t');
return Returnval;
}
}



- Wayne


No comments:

Post a Comment