CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Token error

  1. #1
    Join Date
    Oct 2006
    Posts
    74

    Token error

    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?


    Code:
    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
        }
            
    
       
    }

  2. #2
    Join Date
    May 2009
    Location
    Finland
    Posts
    7

    Re: Token error

    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:

    Code:
      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.

  3. #3
    Join Date
    Oct 2006
    Posts
    74

    Post Re: Token error

    Quote Originally Posted by Loldemar View Post
    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:

    Code:
      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?

  4. #4
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Token error

    Quote Originally Posted by RajaCode View Post
    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.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured