Why won't this simple program compile???

S

steve smith

Hi I have just downloaded the Borland C# Builder and the Micorsoft
..Net framework SDK v1.1 from the borland webist, and i am trying to
get a simple program to run, however I keep getting errors, any ideas
why this might be happening?

Program i am running is:

namespace ExamProblem
{
using System;
using System.IO;
using System.Collections;

class Start
{
[STAThread]
static void Main(string[] args)
{
Parser.ParseFile("c:/input.txt", "c:/output.txt");
}
}

class Parser
{
System.IO.TextReader r = System.IO.File.OpenText(@"C:\input.txt");
int x;
string s = r. ReadToEnd();

string[] mySplit = new string[3000];
/* char[] splitter = {' '};
arInfo = s.Split(splitter); */

string[] mySplit = s.Split(' ');
for(int x = 0; x < mySplit.Length; x++)
{
Console.WriteLine(mySplit[x] + "<br>");
}
r.Close();

}
}

Errors i get are:
[C# Error] sage.cs(30): Invalid token 'for' in class, struct, or
interface member declaration
[C# Error] sage.cs(30): Invalid token '<' in class, struct, or
interface member declaration
[C# Error] sage.cs(30): Invalid token ';' in class, struct, or
interface member declaration
[C# Error] sage.cs(30): Invalid token '++' in class, struct, or
interface member declaration

etc, any ideas what this mite be? Thanks.
 
J

Joe Mayo [C# MVP]

Hi Steve,

Your code needs to be in a method named ParseFile. I made a couple other
adjustments to get it to compile also:

namespace ExamProblem
{
using System;
using System.IO;
using System.Collections;

class Start
{
[STAThread]
static void Main(string[] args)
{
Parser.ParseFile("c:/input.txt", "c:/output.txt");
}
}

class Parser
{
public static void ParseFile(string input, string output)
{
System.IO.TextReader r = System.IO.File.OpenText(@"C:\input.txt");
// int x;
string s = r. ReadToEnd();

//string[] mySplit = new string[3000];
/* char[] splitter = {' '};
arInfo = s.Split(splitter); */

string[] mySplit = s.Split(' ');
for(int x = 0; x < mySplit.Length; x++)
{
Console.WriteLine(mySplit[x] + "<br>");
}
r.Close();
}

}
}

Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top