.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> C# >> Post New QuestionBookmark and Share Subscribe to Forum

runtime error or compiletime error?

Posted By: Rama Krishna     Posted Date: September 26, 2010    Points:2   Category :C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace sa2
{
class A
{
public void A()
{
Console.WriteLine("method A");
}
class B:A
{
public new void A()
{
Console.WriteLine("method B");
}
}
public class program
{
static void Main(string[] args)
{
A a=new A();
B b=new B();
a=b;
b=(B)a;
b.A();
Console.ReadLine();
}
}
}
}


Responses
Author: Jean Paul             
Posted Date: September 27, 2010     Points: 5   

Dear Rama Krishna:

Reason for Error: A class should not contain a method with same name of class
(except for constructors). In your case class A is having method A

Please find the corrected code below:

namespace sa2
{
class A
{
public void A2()
{
Console.WriteLine("method A");
}

class B : A
{
public new void A2()
{
Console.WriteLine("method B");
}
}
public class program
{
static void Main(string[] args)
{
A a = new A();
B b = new B();
a = b;
b = (B)a;
b.A2();
Console.ReadLine();
}
}
}
}

Regards.


Jean Paul http://jeanpaulva.com/
Author: Deco             
Posted Date: September 27, 2010     Points: 5   

yes,

a class will have the same name for the default constructor, if you have the

same name in the method, it would mess up..

Regards,
Deco


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend