How to use base keyword with example ? [Resolved]

Posted by Akiii under C# on 6/8/2012 | Points: 10 | Views : 7477 | Status : [Member] | Replies : 17
Hi,

Can anyone tell me how to call a Base Class Functionality by base keyword from a derived class ?



Thanks and Regards
Akiii




Responses

Posted by: Ajay.Kalol on: 6/8/2012 [Member] Starter | Points: 50
Posted by: CGN007 on: 6/8/2012 [Member] Silver | Points: 50

Up
0
Down

Resolved
Please have look at this..
http://www.dotnetfunda.com/codes/code2567-virtual-override-example.aspx

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: perfectchourasia-9163 on: 6/8/2012 [Member] Starter | Points: 25

Up
0
Down
In C# base keyword is used to call the base class constructor and base class field.

First the values are received by the derived class constructor and then passed to the base class constructor.



class A

{

int i;

A(int n, int m)

{

x = n;

y = m

Console.WriteLine("n="+x+"m="+y);

}

}

class B:A

{

int i;

B(int a, int b):base(a,b)//calling base class constructor and passing value

{

base.i = a;//passing value to base class field

i = b;

}

public void Show()

{

Console.WriteLine("Derived class i="+i);

Console.WriteLine("Base class i="+base.i);

}

}

class MainClass

{

static void Main(string args[])

{

B b=new B(5,6);//passing value to derive class constructor

b.Show();

}

}

OUTPUT

n=5m=6
Derived class i=6
Base class i=5

if it is correct then check it resolved.

ER sandeep chourasia
sandeepchrs@yahoo.com (on facebook)
http://www.aspnetcodes.com/

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: CGN007 on: 6/8/2012 [Member] Silver | Points: 25

Up
0
Down
Other links
1.http://www.dotnetfunda.com/codes/code358-use-of-base-keyword-in-csharp.aspx
2.
http://www.dotnetperls.com/base


Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 6/8/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi @CGN007..

you have a good example but can you tell me one thing ? Is base keyword only used when we have virtual and override keyword ?


Thanks and Regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 6/8/2012 [Member] Bronze | Points: 25

Up
0
Down
@sandeep ....there is some problem in your code. Please check it and try to post your code in code blocks !


Thanks and Regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 6/8/2012 [Member] Bronze | Points: 25

Up
0
Down
@Ajay.....let me check your example and i will let you know !


Thanks and Regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: CGN007 on: 6/8/2012 [Member] Silver | Points: 25

Up
0
Down
@Akii

A derived class can call a constructor defined in its base class by using the base keyword.
Synatx
derived-constructor(parameter-list) : base(arg-list) {

// body of constructor
}

Here, arg-list specifies any arguments needed by the constructor in the base class. Notice the
placement of the colon.

http://www.vkinfotek.com/oops/base-class-constructor-csharp-net.html

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: CGN007 on: 6/8/2012 [Member] Silver | Points: 25

Up
0
Down
@Akii
Please See this in MSDN http://msdn.microsoft.com/en-us/library/ms173115%28v=vs.100%29.aspx

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 6/8/2012 [Member] Bronze | Points: 25

Up
0
Down
@Ajay.Kalol.....The link that you provided is very good !


Thanks and Regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 6/8/2012 [Member] Bronze | Points: 25

Up
0
Down
@CGN007......i think you are talking about constructor chaining !


Thanks and Regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: CGN007 on: 6/8/2012 [Member] Silver | Points: 25

Up
0
Down
@Akii
We are discussing about base keyword.It can use in both situation as i explained...
I think you got my point.

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: CGN007 on: 6/8/2012 [Member] Silver | Points: 25

Up
0
Down
@Akii
In constructor chaining,We can use the base key word,Hope you already see that example in MSDN.

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 6/8/2012 [Member] Bronze | Points: 25

Up
0
Down
@CGN007 ....yes I got your point. Thanks for the help !


Regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: CGN007 on: 6/8/2012 [Member] Silver | Points: 25

Up
0
Down
@Akii
You are welcome,man...Also thanks for the question.
And its time mark as answer,that will helps those who search the same...!!!


Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: CGN007 on: 6/8/2012 [Member] Silver | Points: 25

Up
0
Down
@see this also
Using base to Access a Hidden Name http://www.dotnetspider.com/resources/429-Using-base-Access-Hidden-Name.aspx

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ajay.Kalol on: 6/8/2012 [Member] Starter | Points: 25

Up
0
Down
Always WelCome

Ajay
ajaypatelfromsanthal.blogspot.in

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response