Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / November 2007

Tip: Looking for answers? Try searching our database.

a question about inheritance in .net

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jeff - 26 Nov 2007 11:12 GMT
hey

.net 2.0

Below I declare 2 classes

********  class A ***********
public class A{
   protected int _id;
   public int Id
   {
       get { return _id; }
       set { _id = value }
   }

   public A (int id)
   {
       this.Id = id;
   }
}

********  class B ***********
public class B : A
{
   private string _name;
   public string Name
   {
       get { return _name; }
       set { _name = value }
   }

   public B (int id, string name)
   {
       this.Id = id;
       this.Name = name;
   }
}

In my .net code I've defined 2 similar classes. When I build the code I get
an compile error on the constructor of class B, it gives this error messsage
"No overload for method A takes 0 arguments"..
what am I missing here??
Tom Porterfield - 26 Nov 2007 11:24 GMT
> hey
>
[quoted text clipped - 38 lines]
> "No overload for method A takes 0 arguments"..
> what am I missing here??

If you do not create a constructor for your class, C# will create one
for you automatically.  Since you have created your own constructor in
A, you do not get the C# created default constructor.

You can either add your own default constructor to A - Ex:

public A()
{
}

Or you can chain to the constructor you have in A from B's constructor.  Ex:

public B (int id, string name) : base (id)
{
    this.Name = name;
}
Signature

Tom Porterfield

Chris Diver - 26 Nov 2007 11:31 GMT
> hey
>
[quoted text clipped - 38 lines]
> "No overload for method A takes 0 arguments"..
> what am I missing here??

Hi,

I'm not a C# developer but I think I know whats going on.

The base class's (A) constructor is always called before the derived
classes constructor. In your case it is trying to call it with no
arguments, changing the constructor signature in B to.

public B (int id, string name):base(id)

Will make it pass the id parameter to your constructor in A.

The alternative would be to get rid of the constructor in A, but thats
up to you.

HTH,
Chris

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.