Thread: "Press any key to continue" Error on console

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    1

    "Press any key to continue" Error on console

    Hi guys! 1st post here, I hope i do it correctly!

    Anyway, I am trying to answer this question:

    Define a class Tutor, which inherits the Person class. Tutor should represent a tutor who teaches one or more modules at Bedfordshire University. A Tutor should have a few extra properties in addition to those inherited from Person, such as an office location (as a string), a list of course taught, (array of string) and any others. Write out C# code for this class showing inheritance relationship, the new data fields and a set of C# properties for setting and getting their values
    using this code [I haven't done the array's yet]

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace Tutor
    {
        public class Person
    {
    	private string name;
    	private string age;
    
        public Person ()
            {
    		name = "" ;
            age =  "";
            }
        public Person (string n, string a)
            {
    	    this.name = n;
            age = a;
            }
    
        public string PrintDescription ()
            {
    	    return "Name "  + n + "Age:" + a;
            }
    }
    
       public class Student : Person 
        {
           private string university;
           private string id;
           private string officeLocation;
           public void Main (string [] arg);
    
    
           public Student()
               {
                   university = "";
                   id = "";
                   officeLocation = "";
               }
           public Student (string n, string a, string u, string id, string ol)
           {
                this.name = n;
                age = a;
                university = u;
                id = id;
                officeLocation = ol;
           }
    
    	    public string PrintDescription2 ()
    
            {
                return "University: " + u + "ID: " + id + "Office Location: " + ol;
            }
    
           static void Main(string[] args)
           {
               Person x = new Person("Takeshi Kitano", "49");
               Student y = new Student("Luton", "0519900", "D-Block 2345");
               Console.WriteLine(x.Person);
               Console.WriteLine(y.Student);
               Console.Read();
           } 
    
        }
    
    }
    But I keep getting this message:

    http://xs208.xs.to/xs208/06423/untitled1212.JPG

    Any help on the issue? Thanks in advance for the help!!!

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    To begin with, try giving us the code that actually compiles. That code will not. While you're working through the compiler errors, here's a few more things you might think about:
    1. Do you really need a default constructor for either class? If not, take it out.
    2. Even if you do need those default constructors, you can initialize your fields without them. Take, for instance, your Person class:
      Code:
      public class Person
      {
         private string name = string.Empty;
         private string age = string.Empty;
         ...
    3. You can nest constructors. Notice that your Student class won't compile right now because it attempts to assign values to private fields of its base class. Fortunately, you've already got the constructor you really want in your Person class, so you can use it:
      Code:
      public Student (string n, string a, string u, string id, string ol)
         : base(n,a)
      {
         university = u;
         id = id;
         officeLocation = ol;
      }
    4. It's bad form to use string concatenation to build strings. Each concatenation creates a whole new string. Instead, you might use string.Format or a Stringbuilder:
      Code:
          public string PrintDescription ()
      {
         //return "Name "  + n + "Age:" + a; //Not this
      
         return string.Format("Name {0} Age: {1}", n, a); //maybe this
      
         /* //Or maybe this block
         Stringbuilder strBuild = new Stringbuilder();
         strBuild.Append("Name ");
         strBuild.Append(n);
         strBuild.Append(" Age: ");
         strBuild.Append(a);
         return strBuild.ToString();
         */
      }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by pianorain
    It's bad form to use string concatenation to build strings. Each concatenation creates a whole new string.
    Does it? Java is smart enough to automatically use StringBuilder for such things, thus producing code equal to your second example, while keeping the nice syntax. Is C# not?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  2. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 10:00 AM
  3. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  4. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM