Click here to Skip to main content
15,890,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
namespace sir_shahzad_paper_OOP_2013
{
    class baseClass
    {
        protected int i = 13;
    }
    class Program : baseClass
    {
         int i=9;
         int b = 9;
        static void Main(string[] args)
        {
            Program p = new Program();
            Console.Write(p.i);
 
            
        }
    }
}


look above program
now console showing me the output 9. i want to show 13 as well. how can i do this.
Posted

C#
Console.WriteLine(i);
Console.WriteLine(base.i);
Result:

9
13

Edit: in response to feedback from Karthik and Muhamad:

Yes, I'm not on-target here, not thinking about the fact the method is static because it's the special 'Main method launch-pad for a Console App: you can never use the keyword 'base to access an ancestor class' fields, or methods, in a static method whether that static method is within a dynamic (non-static) class, or a static class !

And, of course, using static Classes you do not have inheritance from other classes at all: all static classes inherit from 'Object.

There is a way in a static class to call a method of a dynamic class, and in that method, you can access the base class it inherits from. Let me illustrate in code that's not a Console app, but done in WinForms:
C#
public class baseClass
{
    protected int i = 13;
}

public class inheritsFromBaseClass : baseClass
{
    private int i = 9;
    private int b = 9;

    public void showValues()
    {
        Console.WriteLine(i.ToString());
        Console.WriteLine(base.i.ToString());
    }
}

// here's the static class
public static class xProgram
{
    public static inheritsFromBaseClass childOfBase;

    // static class requires parameterless ctor
    static xProgram()
    {
        childOfBase = new inheritsFromBaseClass();
    }
}
If you invoke the static class 'xProgram, like this:
xProgram.childOfBase.showValues();
You create a static instance of the dynamic class, 'inheritsFromBase named 'childOfBase, and when you invoke that instance's 'showValues method, that method can, and does, use the word 'base to access its ancestor class' value for 'i.

Summary:

1. you are in a special 'zone when writing a Console Application if you try and use inheritance inside the static 'Main method.

2. there are ways to get access to a dynamic class' ancestor class from a static method (as shown in the code here). But, think about what's happened: the compelling reason to use dynamic classes is the facility to create multiple instances of them.

In this case there will be one, and only one, instance of the 'inheritsFromBaseClass encapsulated within the xProgram static class ! I suspect that's not a good thing.

Suggestion: consider prototyping your experiments in OOP using only dynamic classes.

Sorry if my response has "muddied the waters" here ! I have virtually no experience with Console applications.
 
Share this answer
 
v4
Comments
Karthik_Mahalingam 24-Dec-13 2:08am    
+5, correct.
Bill, i agree your answer
but we cannot use base keyword inside a static method..
Muhamad Faizan Khan 24-Dec-13 2:16am    
i did with base but base keyword is not showing to me
BillWoodruff 24-Dec-13 3:01am    
Good catch, Karthik ! Please let me know if my revised reply is focused, and useful
Karthik_Mahalingam 24-Dec-13 3:21am    
fine now.
TrushnaK 24-Dec-13 2:15am    
my 5+
try this:-
C#
namespace ConsoleApplication1
{
    class baseClass
    {
        protected int i = 13;
    }
    class prog : baseClass
    {
        int i = 9;
        int b = 9;
        public void printMethod()
        {
            Console.WriteLine(i); //this will give 9
            Console.WriteLine(base.i); //this will give 13
            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            prog p = new prog();
            p.printMethod();
        }
    }
}


you trying to add
Console.WriteLine(base.i);
this line into static main method.
------------------------------------
you seen the error detail base keyword is not avialable in static method.

why we can not use base keyword in static method? :-
Because base keyword is referring to the base class of the current class instance. But you do not have a current instance in static methods - they are static not instance.
 
Share this answer
 
v2
Comments
BillWoodruff 24-Dec-13 3:03am    
+5 good answer !
TrushnaK 24-Dec-13 3:09am    
thanx Billwoodruff
Hi Try like this


C#
using System;
namespace sir_shahzad_paper_OOP_2013
{

    class Program
    {

        int i = 9;

        static void Main(string[] args)
        {
            DerivedClass obj = new DerivedClass();
            obj.somemethod();
        }


    }

    public class BaseClass
    {
        protected int i = 13;
    }
    public class DerivedClass : BaseClass
    {
        int i = 9;
        int b = 9;

      public  void somemethod()
        {
            DerivedClass p = new DerivedClass();
            Console.WriteLine(i); //9
            Console.WriteLine(base.i); // 13
            Console.ReadLine();
        }

    }



}




you cannot use the base keyword inside a static method, so i have created a new class and done it.
i hope you understood.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900