0

I'm trying out structures and I can't figure out how to use them inside of functions. Later on I'll need to develop a piece of code that'll allow me to add students and reenter some details.

namespace struct_example
{
    struct student
    {
        public int s_id;
        public String s_name, c_name, s_dob;
    }

    class Program
    {
        static void Main(string[] args)
        {
            student[] arr = new student[4];

            for (int i = 0; i < 4; i++)
            {
                fillplz(i);
            }
            for (int i = 0; i < 4; i++)
            {
                showplz(i);
            }
            Console.ReadKey();
        }
        static void fillplz(int id)
        {
            Console.WriteLine("Please enter StudentId, StudentName, CourseName, Date-Of-Birth");
            arr[id].s_id = Int32.Parse(Console.ReadLine());
            arr[id].s_name = Console.ReadLine();
            arr[id].c_name = Console.ReadLine();
            arr[id].s_dob = Console.ReadLine();
        }
        static void showplz(int id)
        {
            Console.WriteLine(arr[id].s_id);
            Console.WriteLine(arr[id].s_name);
            Console.WriteLine(arr[id].c_name);
            Console.WriteLine(arr[id].s_dob);
        }
    }
}
11
  • Can you explain what exactly the problem you are having is? Is this code throwing an error or refusing to compile or something else? Commented Nov 22, 2016 at 10:58
  • 2
    Well first of all by first glance arr is an array in the scope of Main only Commented Nov 22, 2016 at 10:59
  • well it's returning 0's Commented Nov 22, 2016 at 11:00
  • fillplz and showplz cannot access arr[] Commented Nov 22, 2016 at 11:01
  • 2
    why is student a struct instead of a class ? Student should not be a value type, it is an entity and thus a class is more suitable. Commented Nov 22, 2016 at 11:05

1 Answer 1

3

The only problem in your code is that you are trying to access arr when it isn't in scope.

You have declared arr in your Main method which means it is only accessible there. If you declared it as a field on your class then you would be able to access it and everything would work as you expected. Alternatively you could pass the array as a parameter to the methods that use it and access it that way.

In short: the problem is nothing to do with structs, it is to do with scoping your variables and you would have run into similar problems with a class.

An additional note: When I compiled your code (and well done for giving an easily compilable code sample) I got errors such as:

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

And would have included exact line numbers and should have told you exactly how to fix your error.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.