ZanderKraig
ZanderKraig

Reputation: 3

Unable to access object properties from the same class but different method in C#

I am unable to access the properties of the object student1 that has been created in the Main() method of the Program class in the method ClassOperations() of the same class. I keep getting

error CS0103: The name 'student1' does not exist in the current context

using System;

namespace Trials
{
    public class Students
    {
      public int maths;
      public int science;
      public int english;
      public int secondLang;
      public int socialScience;
      public string name;

      public Students(int[] inputMarks)
      {
        maths = inputMarks[0];
        science = inputMarks[1];
        english = inputMarks[2];
        secondLang = inputMarks[3];
        socialScience = inputMarks[4];
      }
    }

    public class Program
    {
      static void Main(string[] args)
        {
            Console.WriteLine("Enter your name: ");
            string inputName = Console.ReadLine();
            int[] marks = new int[5];

            Console.WriteLine("Enter the maths marks: ");
            marks[0] = int.Parse(Console.ReadLine());

            Console.WriteLine("Enter the Science marks: ");
            marks[1] = int.Parse(Console.ReadLine());

            Console.WriteLine("Enter the English marks: ");
            marks[2] = int.Parse(Console.ReadLine());

            Console.WriteLine("Enter the Second language marks: ");
            marks[3] = int.Parse(Console.ReadLine());

            Console.WriteLine("Enter the Social Science marks: ");
            marks[4] = int.Parse(Console.ReadLine());

            Students student1 = new Students(marks);

            student1.name = inputName;
            ClassOperations();
        }

      static void ClassOperations()
      {
          Console.WriteLine("Enter input :");
          int input = int.Parse(Console.ReadLine());

          switch (input)
          {
            case 1:
              Console.WriteLine(student1.name);
              break;
            case 2:
              Console.WriteLine(student1.maths);
              break;
            case 3:
              Console.WriteLine(student1.science);
              break;
            case 4:
              Console.WriteLine(student1.english);
              break;
            case 5:
              Console.WriteLine(student1.secondLang);
              break;
            case 6:
              Console.WriteLine(student1.socialScience);
              break;
        }
      }
    }
}

Upvotes: 0

Views: 28

Answers (1)

pinkfloydx33
pinkfloydx33

Reputation: 12789

The error is telling you the problem: local variables (student1) declared in a method (Main) are not accessible within other methods (ClassOperations, the current context). You have several options:

  • (static) property
  • (static) field
  • method parameter

In this example, the "best" option is to pass your student1 variable as a parameter to the ClassOperations method:

static void Main(string[] args) {
    // setup elided
    Students student1 = new Students(marks);
    student1.name = inputName;  
    // pass as parameter    
    ClassOperations(student1);
} 

static void ClassOperations(Students student) {
    // access value via new name "student" 
    Console.WriteLine(student.name);
}

Based on your comments, here is what the alternative with a field could look like:

static Students student; //static field 
static void Main(string[] args) {
    // setup elided
    // initialize static field 
    student = new Students(marks);
    student.name = inputName;  
    ClassOperations();
} 

static void ClassOperations() {
    // access value via static field "student" 
    Console.WriteLine(student.name);
}

Upvotes: 0

Related Questions