Click to See Complete Forum and Search --> : Token error


RajaCode
May 24th, 2009, 06:56 AM
I am confused about this problem. The code is syntactically correct however when placing the code into a new class it refuses to compile and brings up errors of token problems.

Any ideas why this occurs?



using System;

namespace MCTSExample2
{



class foo1
{

static void Main(string[] args)
{
SByte a=0;
Byte b=0;
Int16 c=0;
Int32 d=0;
Int64 e=0;
String f = "t";
Exception g = new Exception();


object[] types ={a,b,c,d,e,f,g};

foreach (object obj in types)
{
string type;
if (obj.GetType().IsValueType)
type = "Value Type";
else
type = "Reference type";

Console.WriteLine("{0}: {1}", obj.GetType(), type);
}

;

Console.ReadLine();


}
}

class lala
{
string ui = "Microsoft .NET Framework Application Development Foundation";
string[] ui2 = ui.Split(' ');

Array.Sort(ui2)
//when all the code in this class is put into the main method compiles correctly
}



}

Loldemar
May 24th, 2009, 08:29 AM
Hi,

Well, your code isn't compiling. Why don't you put string splitting and array sorting into a method, so code looks like this:


class lala
{
string ui = "Microsoft .NET Framework Application Development Foundation";

// this method does the string splitting etc.
public void ArraySorterMethod()
{
string[] ui2 = ui.Split(' ');
Array.Sort(ui2);

// output string values or whatever you like to do
foreach(string s in ui2)
Console.WriteLine(s);
}
}


that way you can call the code from main method.

RajaCode
May 24th, 2009, 10:51 AM
Hi,

Well, your code isn't compiling. Why don't you put string splitting and array sorting into a method, so code looks like this:


class lala
{
string ui = "Microsoft .NET Framework Application Development Foundation";

// this method does the string splitting etc.
public void ArraySorterMethod()
{
string[] ui2 = ui.Split(' ');
Array.Sort(ui2);

// output string values or whatever you like to do
foreach(string s in ui2)
Console.WriteLine(s);
}
}


that way you can call the code from main method.


Why doesn't it work with a method? Why cant I just place it into the class without a method?

JonnyPoet
May 24th, 2009, 05:07 PM
Why doesn't it work with a method? Why cant I just place it into the class without a method?What would call your code ?
IMHO you have a big basic misunderstood about a class. Code is always run by a method which is simple called from anywhere. In your first example the code is simple part of the main method so it is called when the main method is run.

Maybe you need to read some very basic books about OOP and about programming itself, because you will need all this basic things before you are able to use the tools.
A class is a pattern for an object which is basically storage space for fileds of different data on the heap and access methods fo this fields called Properties, methods to handle different things called methods. events can be fired when different things occur inside the class or it may delegate events from any other class to different places in the program.

So this is very rough, but a short overview of what you need to confront, when talking about a class.

Code is a followup of different commands where data are connected together or results are calculated from different data. Data are used to be processed by this commands. This way you would easily understad that this commands needs to be started which is done by use of a method stub,wherein the commands are processed from top to bottom or to any leave-point like a return command.

This should be enough explanation to understand that code cannot be stored on the heap, it needs to be storedin the process area and is called when the methods stub is called.