0

Please help me on this.

I am new to C# and structure

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

namespace dhiru
{
class Program
{
    struct studentInformation
    {
        public string[,] student;

        public studentInformation(int fValue, int lValue)
        {
            student = new string[5, 7];
            for (int i = 0; i < 6; i++ )
            {
                for (int j = 0; j < 5; j++ )
                {
                    switch (i)
                    {
                        case 0:
                            Console.WriteLine("Enter the student first name : ");
                            student[i, j] = Console.ReadLine();
                            break;
                        case 1:
                            Console.WriteLine("Enter the student last name : ");
                            student[i, j] = Console.ReadLine();
                            break;
                        case 2:
                            Console.WriteLine("Enter the student Address : ");
                            student[i, j] = Console.ReadLine();
                            break;
                        case 3:
                            Console.WriteLine("Enter the student city : ");
                            student[i, j] = Console.ReadLine();
                            break;
                        case 4:
                            Console.WriteLine("Enter the student state : ");
                            student[i, j] = Console.ReadLine();
                            break;
                        case 5:
                            Console.WriteLine("Enter the student country : ");
                            student[i, j] = Console.ReadLine();
                            break;
                    }
                }
            }
        }
    }
    static void Main(string[] args)
    {
        string sFirstName = "";
        string sLastName = "";
        string sAddress = "";
        string sCity = "";
        string sState = "";
        string sCountry = "";

        studentInformation sInfo = new studentInformation();
        for (int i = 0; i < 6; i++)
        {
            for (int j = 0; j < 5; j++)
            {
                switch(i)
                {
                    case 0:
                        sFirstName = sInfo.student[i, j];
                        break;
                    case 1:
                        sLastName = sInfo.student[i, j];
                        break;
                    case 2:
                        sAddress = sInfo.student[i, j];
                        break;
                    case 3:
                        sCity = sInfo.student[i, j];
                        break;
                    case 4:
                        sState = sInfo.student[i, j];
                        break;
                    case 5:
                        sCountry = sInfo.student[i, j];
                        break;
                }
            }
            Console.WriteLine("{0} {1} is a student comes from city : {2}, state: {3}, country: {4} and his address is {5}", sFirstName, sLastName, sCity, sState, sCountry, sAddress); //print the output
        }
    }
 }
}

This code is throwing null reference exception which I know why but I don't know how to correct this other than using the for loop of struct in main method. I want to take user input in struct.

d007
  • 1
  • 1

1 Answers1

0

A better way would be to declare StudentInfo as a class with property for each information instead of using array.

However, if the main purpose is to learn arrays, this is because you call the default constructor of the struct instead of yours

 studentInformation sInfo = new studentInformation();

should be

 studentInformation sInfo = new studentInformation(1,1);

But I don't understand this two values in your constructor...

cdie
  • 4,014
  • 4
  • 34
  • 57
  • This is because your loop is on `j`, and not on `i`, so `i` value never change in each `j` loop turn. Consider changing your `switch(i)` on `switch(j)` in your struct ctor – cdie Feb 17 '16 at 11:58
  • Thanks for reply. I have changed the code as below this time I don't get any exception but console window keep asking for student first name only it is not executing as per for loop. Please suggest what to do next and why. Why is important as I am learning about structure. I can easily write using class or methods but I want to know how to write using structure. – d007 Feb 17 '16 at 12:01
  • Thanks for saving :) – d007 Feb 17 '16 at 12:06