trouble with constructor

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ankitamca85
    New Member
    • Feb 2008
    • 12

    trouble with constructor

    Code:
    Public class class1
    {
    public class1(int i)
    {
    	messagebox.show(“parent class”);
    }
    }
    
    public class class2:class1
    {
    public void xyz()
    {
    messagebox.show(“child class”);
    }
    }
    why error coming-no overload for method class1 takes 0 arguments

    why not implicitly creating default const for base class
    Code:
    Public class class1
    {
    }
    
    public class class2:class1
    {
    public void xyz()
    {
    messagebox.show(“child class”);
    }
    }
    no error??why?

    help me out!!!!!
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    It tells you exactly what the problem is. You do not have a constructor that takes no arguments.
    If you look at the constructors of class1 in your first example, you will see that there is no default constructor for the class, only a constructor that takes an int.
    You would need to add the default (no arguments) constructor.

    Comment

    • ankitamca85
      New Member
      • Feb 2008
      • 12

      #3
      public class class1
      {
      public class1(int i)
      {
      messagebox.show ("parent class");
      }
      }

      o/p---parent class

      here there is no need to explicitly define default constructor...

      but in case below why it is need to define default constructor....


      1. Public class class1
      2. {
      3. public class1(int i)
      4. {
      5. messagebox.show (“parent class”);
      6. }
      7. }
      8.
      9. public class class2:class1
      10. {
      11. public void xyz()
      12. {
      13. messagebox.show (“child class”);
      14. }
      15. }

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        A blank constructor is "assumed" when no other constructors are declared. As soon as you declare a constructor, the default constructor is no longer assumed.
        Should not be hard.

        Comment

        Working...