Search code examples
c#arraysstructure

How to create an array to hold structures?


I was asked to create some structures: student, teacher, course, program and then make an array to hold 5 students structures, and assign values to the fields of students in the array, I'm stuck in creating the array to hold the structures, here is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Module4Assignment
{
    class Program
    {
        //Student structure: 
        public struct Student
        {
            public Student (string name , string address , string country , string birthday , int telephone)
            {
                this.Name = name;
                this.Address = address;
                this.Country = country;
                this.Birthday = birthday;
                this.Telephone =telephone;
            }

            public string Name;
            public string Address;
            public string Country;
            public string Birthday;
            public int Telephone;
        }

        //Teacher structure:
        public struct Teacher
        {
            public Teacher(string tname, string taddress, string tcountry, string tbirthday, int ttelephone)
            {
                this.TName = tname;
                this.TAddress = taddress;
                this.TCountry = tcountry;
                this.TBirthday = tbirthday;
                this.TTelephone = ttelephone;
            }

            public string TName;
            public string TAddress;
            public string TCountry;
            public string TBirthday;
            public int TTelephone;
        }

        //Program structure
        public struct Program
        {
            public Program(string pname , string department , int pcredits)
            {
                this.PName = pname;
                this.Department = department;
                this.PCredits = pcredits;

            }
            public string PName;
            public string Department;
            public int PCredits;
        }

        //Course structure
        public struct Course
        {
            public Course(string cname, string day, int ccredits)
            {
                this.CName = cname;
                this.Day = day;
                this.CCredits = ccredits;

            }
            public string CName;
            public string Day;
            public int CCredits;
        }


        static void Main(string[] args)
        {
            //Instantiating 5 students structures:
            Student student1 = new Student();
            Student student2 = new Student();
            Student student3 = new Student();
            Student student4 = new Student();
            Student student5 = new Student();


            //creating the array:
            string[] studentArray = new string[5];
            studentArray[0]=student1;
            studentArray[1]=student2;
            studentArray[2]=student3;
            studentArray[3]=student4;
            studentArray[4]=student5;
        }
    }
}

Solution

  • I have a lot of problems with what you're doing here, but the simple answer is that you cant put Student objects into an array of strings:

    static void Main(string[] args)
        {
            //Instantiating 5 students structures :
            Student student1 = new Student();
            Student student2 = new Student();
            Student student3 = new Student();
            Student student4 = new Student();
            Student student5 = new Student();
    
    
            //creating the array :
            Student [] studentArray = new Student[5]; // <---- array of Student!
            studentArray[0]=student1;
            studentArray[1]=student2;
            studentArray[2]=student3;
            studentArray[3]=student4;
            studentArray[4]=student5;
        }