Return base class in C#

How can i return data that is in base class form?

A aclass = new A();
B bclass = aclass.GetB();

does not work.

public class B
{
    protected string str1;
    protected string str2;
}

public class A:B
{
    public A()
    {
         base.str1 = "A";
         base.str2 = "B";
    }

    public B GetB()
    {
        return base;
    }
}

Answers


GetB() is completely unnecessary. B bclass = aclass; is sufficient as aclass is already a B.


I'm no C# expert, but return this; should work. I really don't see the point in doing this though.


I strongy advise not to use the following code and instead change the behavior of your class but the answer is the following:

public B GetB()
{
    return this as B;
}

but instead writing a unique method for returning the object casted to the base class you may use the following;

public class B
{
    protected string str1;
    protected string str2;
}

public class A : B
{
    public A()
    {
        str1 = "A";
        str2 = "B";
    }
}

and you can use as the following;

A a = new A();
B b = a;

Need Your Help

working bar charts/tables with sproutcore

jfreechart google-visualization sproutcore bar-chart

I am currently working on a task which want to display bar charts/tables on the website.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.