why body is required in a constructor. ?

Last post 03-25-2009 2:05 AM by Ganesh@Nilgris. 7 replies.

Sort Posts:

  • why body is required in a constructor. ?

    03-16-2009, 10:29 AM

     This is my scnerio. i have a class called student.

    public class student : studentbase

    {

    public string name;

    public int age;

    public student(); - > i get a compilation telling "extern" key or body must be declared.

    }

    i did nt understand. any idea..

     

     

    Jai Ganesh. J , GSD ,India

    Please Mark As Answer If my reply helped you.

    http://www.onlinepersonaldairy.com. A site to have personal details online. Enjoy.
  • Re: why body is required in a constructor. ?

    03-16-2009, 10:42 AM
    • All-Star
      96,253 point All-Star
    • SGWellens
    • Member since 01-02-2007, 9:27 PM
    • Minneapolis
    • Posts 7,788
    • Moderator
      TrustedFriends-MVPs

    A constructor is a function.  Functions must have bodies.

    Steve Wellens

    My blog
  • Re: why body is required in a constructor. ?

    03-16-2009, 11:05 AM
    • Participant
      764 point Participant
    • Sgro
    • Member since 06-10-2006, 7:55 AM
    • Posts 344

     You can't declare a constructor like that. Besides the fact that an empty constructor with no paramters is always implicitly declared (in other words, you don't have to write it), it is declared this way:

     

    public student() { }

     

    and NOT

    public student();

    Matteo Mosca
    Web Developer
    IWA Member
  • Re: why body is required in a constructor. ?

    03-16-2009, 11:40 AM

     

    SGWellens:

    A constructor is a function.  Functions must have bodies.

    i understand wht u have said. but the problem is i have seen a code as explained in the above post and i dont get compilation errors. but the same class is used in a standlone project i am getting compilation errors.

     

    Jai Ganesh. J , GSD ,India

    Please Mark As Answer If my reply helped you.

    http://www.onlinepersonaldairy.com. A site to have personal details online. Enjoy.
  • Re: why body is required in a constructor. ?

    03-16-2009, 11:57 AM
    Answer
    • All-Star
      96,253 point All-Star
    • SGWellens
    • Member since 01-02-2007, 9:27 PM
    • Minneapolis
    • Posts 7,788
    • Moderator
      TrustedFriends-MVPs

    If you look closely, you'll see two curly braces:  {}

    This is the function body, it's an empty body, but you must provide a body even if it's empty.

    Steve Wellens

    My blog
  • Re: why body is required in a constructor. ?

    03-16-2009, 6:26 PM
    • Contributor
      5,416 point Contributor
    • Paul Linton
    • Member since 04-30-2008, 3:16 AM
    • Posts 934

    Ganesh@Nilgris:
    i have seen a code as explained in the above post and i dont get compilation errors

    Show us this other code and we will explain to you why it is different.

    Got a c# problem? Try .NET Book Zero from Charles Petzold, it's a free pdf.
  • Re: why body is required in a constructor. ?

    03-18-2009, 8:09 AM
    • Member
      380 point Member
    • Bitnbytes
    • Member since 12-30-2008, 5:44 PM
    • USA
    • Posts 79
    You probably have seen some abstract class in bigger project. And these were classes derived from it. And now you have tried to do the same in standalone project and now you are getting compilation error. You can not have an empty body in non-abstract definitions.
     
    N

     

    SGWellens:

    A constructor is a function.  Functions must have bodies.

    i understand wht u have said. but the problem is i have seen a code as explained in the above post and i dont get compilation errors. but the same class is used in a standlone project i am getting compilation errors.

     


    Jai Ganesh. J , GSD ,India

    Please Mark As Answer If my reply helped you.

    http://forums.asp.net/p/1397507/3010616.aspx#3010616

  • Re: why body is required in a constructor. ?

    03-25-2009, 2:05 AM

    bitnbytes:
    You probably have seen some abstract class in bigger project. And these were classes derived from it. And now you have tried to do the same in standalone project and now you are getting compilation error. You can not have an empty body in non-abstract definitions.
     
    N
    SGWellens:

    A constructor is a function.  Functions must have bodies.

     

     

    i understand wht u have said. but the problem is i have seen a code as explained in the above post and i dont get compilation errors. but the same class is used in a standlone project i am getting compilation errors.

     


    Jai Ganesh. J , GSD ,India

    Please Mark As Answer If my reply helped you.

    http://forums.asp.net/p/1397507/3010616.aspx#3010616

    i guess that the problem might be i have copied the code from the metaform of the class and not from the orginal class. Thanks for all the people who have responded.

     

    Jai Ganesh. J , GSD ,India

    Please Mark As Answer If my reply helped you.

    http://www.onlinepersonaldairy.com. A site to have personal details online. Enjoy.
Page 1 of 1 (8 items)