-3

This is my main class person

public class Person
{
     public void SetAge(int n)
    {
         n = 20;
    }
    static void Main(string[] args)
    {

    }
}

and i want to access the n variabe in this inherited class student

 class student : Person
{

    public void GoToClasses()
    {
        Console.WriteLine("I am going to class");
    }
    public void ShowAge()
    {
        Console.WriteLine("My age is {0}",n);
    }
}

I tried using Person.n or SetAge(20) or SetAge(n) and it won't work !

6 Answers6

2
public class Person
{
    public int Age {get; set;}
}

Above represents a basic object.

Use case:

Person person = new Person();
person.Age = 25;

Other object additions:

public class Person
{
    public Date BirthDate {get; set;}
    public int Age { get { return (DateTime.Now.Year - BirthDate.Year); } } //read only
}
Andrew Grinder
  • 585
  • 5
  • 21
  • I try to keep my object classes as basic as possible. If there is anything that needs logic, I create class to manipulate items. Example. I have a repository class for each object (which objects for me represent a record of data.) So I would have a PersonRepo that does my CRUD operations. – Andrew Grinder Jun 19 '15 at 15:33
2

As Andrew pointed out, you don't have a method of accessing that variable. Not only that; class fields default to private access.

Link:What are the Default Access Modifiers in C#?

Andrew wrote you code that gives you a property, which isn't wholly necessary, but it is the easiest way to get getter and setter accessor methods.

If you only wanted your inherited class to have access while still restricting access outside your class, use protected access modifier.

Edit: I misread the question. Not only can you not access the method parameter n from outside the function it's used in, I'm not even sure why you would want to. Just do what Andrew said, and do some research on variable scope.

Community
  • 1
  • 1
Ron Thompson
  • 1,086
  • 6
  • 12
  • Though accurate the note on class fields defaulting to private seems irrelevant since there are no fields so that most certainly is not any part of the problem here (the fact that there are no fields is the bigger problem!). – Chris Jun 19 '15 at 15:28
  • The `n` is weirdly enough declared as a parameter of the method. The method is very much not what you expect for a SetAge method (you wouldn't expect it to be deciding the age to set and ignoring the input parameter). The original code is just very confused which makes it tricky to work out which part needs focussing on as an answer (just fixing the problem feels like it would probably leave the OP still confused about other concepts demonstrated here. – Chris Jun 19 '15 at 15:35
  • 2
    That's kinda where I ended up after I read it again. Af this point, OP would be better served by an introductory OOP class than anything I can type, and I'm not trying to be mean. – Ron Thompson Jun 19 '15 at 15:37
  • I quite agree! This is why I didn't answer at all. – Chris Jun 19 '15 at 15:39
  • @RonThompson, I agree. However I think that OP may already be in class and was introducted to SO, which in my opinion is a good thing for any developer to learn (easy, or hard way) how to utilize SO to their advantage. – Andrew Grinder Jun 19 '15 at 16:04
  • Thanks for your concern regarding me . I have just started learning c# but it was in the practice exercise that i was trying to do.. http://practiceexercisescsharp.blogspot.in/2013/05/601-classes-student-teacher.html .. Now i understand it a bit better .. Thanks for your effort ! :) – karla racha Jun 19 '15 at 16:59
  • Oh no hard feelings. I just don't know where to start to help you. – Ron Thompson Jun 19 '15 at 17:08
1

n is not an exposed property of Person. You can either expose it or return an int from your SetAge(int n).

Exposing n:

public int n {get; set; }  

Returning an int:

public int SetAge(int n)
{
    // this is not the normal way to set via a method. you don't normally
    // set the input variable to a static value like this.
    n = 20;
    return n; 
}
ethorn10
  • 1,889
  • 1
  • 18
  • 29
0

Here is your base class:

public class Person
{
    public int Age {get; set;}
}

Here is your inherited class:

class student : Person
{
    public void ShowAge()
    {
        Console.WriteLine("My age is {0}",Age);
    }
}
Marc Johnston
  • 1,276
  • 1
  • 7
  • 16
0

You have to declare the "n" variable first in the Person class. Try this:

public class Person
{
    public int n;
    public void SetAge()
    {
        n = 20;
    }

static void Main(string[] args)
   {

   }
} 

You shouldn't set the parameter of the method "SetAge(int n)" when you later change this parameter. It's pointless in this case.

Michal_Drwal
  • 526
  • 1
  • 9
  • 26
0

You should declare your n variable as a class field with either protected or public visibility. Like this:

public class Person
{
    protected int n;

    public void SetAge(int n)
    {
         n = 20;
    }
}

Or you could use a public property as the other answers showed already

public class Person
{
    public int Age {get; set;}
}

This way in the student class you could use it like this

public class Student : Person
{
    public void ShowAge()
    {
        Console.WriteLine("My age is {0}", Age);
    }
}

or like this

public class Student : Person
{
    public void ShowAge()
    {
        Console.WriteLine("My age is {0}", n);
    }
}
Tanuki
  • 717
  • 1
  • 9
  • 24