Senior & Expert .Net Developers Discussion Forum by Nyros Technologies

HIRE .Net Expert Developers Programmers Coders From India
Ruby on Rails PHP .Net Developers Community, Nyros Technologies, Kakinada
 
Log in  or IF not a member please REGISTER
Username:
Password:   


Keyword
Log in | Profile 

How to get parent class variables into child class.

 
Post new topic   Reply to topic    Senior & Expert .Net Developers Discussion Forum by Nyros Technologies Index -> Programming with C#
View previous topic :: View next topic  
Author Message
sek



Joined: 26 Mar 2010
Posts: 30

PostPosted: Fri Mar 26, 2010 10:51 am    Post subject: How to get parent class variables into child class. Reply with quote

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
View user's profile Send private message
rajesh



Joined: 26 Mar 2010
Posts: 43

PostPosted: Fri Mar 26, 2010 11:30 am    Post subject: Reply to sekhar's question Reply with quote

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.
_________________
Cool***************Cool

Best Regards
Rajesh Mani.
Nyros Technologies.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Senior & Expert .Net Developers Discussion Forum by Nyros Technologies Index -> Programming with C#
Page 1 of 1


 latest topics 
 Topics   Replies   Author   Views   Last Post 
No new posts How to Add Update Delete for gridview using Webservice
1 sanjay 31 Fri Sep 10, 2010 7:05 am
setty View latest post
No new posts Is there any free webhosts that support ASP.NET. ?
2 Balaji 634 Tue Sep 07, 2010 10:24 am
chrisadam View latest post
No new posts Hostings For ASP.NET 2.0 with SQL Server
2 Avash 676 Tue Sep 07, 2010 10:21 am
chrisadam View latest post
No new posts Exceptional Manger
1 suraj 51 Thu Sep 02, 2010 8:52 am
Rahul View latest post
No new posts background image for UITableView
0 kumar 55 Wed Sep 01, 2010 10:34 am
kumar View latest post
No new posts Types of Run time exceptions
1 Dheeraj 52 Wed Sep 01, 2010 8:58 am
venkat View latest post
No new posts Access Web Page content in Windows Forms without WebBrowser
3 mouli 273 Wed Sep 01, 2010 7:31 am
rajesh View latest post
No new posts Explain Interlocked Class & Daemon Thread's?
1 mukundh 76 Wed Sep 01, 2010 7:19 am
setty View latest post
No new posts Life Cycle Of Delegate
0 vinay 40 Wed Sep 01, 2010 7:14 am
vinay View latest post
No new posts Accessing methods defined in another page
1 suraj 37 Tue Aug 31, 2010 12:45 pm
Rahul View latest post




Hire an expert .Net developer / coder / programmer or development team from India now!!

Other Forums : Ruby on Rails   ::   PHP   |   Free unlimited HTML CSS templates download

Nyros Technologies   |   Kakinada City Portal   |   Developers Blog   |   About .Net Experts   |   More