| View previous topic :: View next topic |
| Author |
Message |
sek
Joined: 26 Mar 2010 Posts: 30
|
Posted: Fri Mar 26, 2010 10:51 am Post subject: How to get parent class variables into child class. |
|
|
Hi..,
I had a problem in a Inheritance Program.
My code is like this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Program
{
static void Main(string[] args)
{
child p=new child();
p.display();
Console.ReadLine();
}
}
class Parent
{
public int a=10;
}
class child:Parent
{
a=20;
public void display()
{
Console.WriteLine(a);
}
}
In Inheritance concept, we can get members and member functions from from base class to derived class. Right? So here i declared a variable "a" in parent class and i inherited parent class to child class. So as per the inheritance concept i should get the parent class variable and i shold be able to modify that variable. But here i am unable do this.
Please help.
Solution for this Really appreciable. _________________ Sekhar |
|
| Back to top |
|
 |
rajesh
Joined: 26 Mar 2010 Posts: 43
|
Posted: Fri Mar 26, 2010 11:30 am Post subject: Reply to sekhar's question |
|
|
Hi sekhar,
Good Day,
I think here you need to recheck the class definition once again
the Synatax of Class is
Class ClassName
{
Declaration of Variables;
Declaration and defintion methods();
}
Here in your program you are trying to do a action in out side of the Member Function. You can declare the variables only you cant do nothing else.., Not only in child class.. in any class.
instead of doing like that try to asign the value to the variable in a display method in you code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Program
{
static void Main(string[] args)
{
child p=new child();
p.display();
Console.ReadLine();
}
}
class Parent
{
public int a=10;
}
class child:Parent
{
public void display()
{
a=20;
Console.WriteLine(a);
}
}
Now you can the out put as 20.Its better to recheck the class syntax once again. _________________
***************
Best Regards
Rajesh Mani.
Nyros Technologies. |
|
| Back to top |
|
 |
|