Array of structures in C #

I try to get information from the user using an array of structures, and then print it out:

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CA4 { class Program { static void Main(string[] args) { StudentDetails[,] student = new StudentDetails[5, 1]; Console.WriteLine("Please enter the unit code:"); student[0, 0].unitCode = Console.ReadLine(); Console.WriteLine("Please enter the unit number:"); student[1, 0].unitNumber = Console.ReadLine(); Console.WriteLine("Please enter first name:"); student[2, 0].firstName = Console.ReadLine(); Console.WriteLine("Please enter last name:"); student[3, 0].lastName = Console.ReadLine(); Console.WriteLine("Please enter student mark:"); student[4, 0].studentMark = int.Parse(Console.ReadLine()); for (int row = 0; row < 5; row++) { Console.WriteLine(); for (int column = 0; column < 1; column++) Console.WriteLine("{0} ", student[row, column]); } Console.ReadLine(); } public struct StudentDetails { public string unitCode; //eg CSC10208 public string unitNumber; //unique identifier public string firstName; //first name public string lastName;// last or family name public int studentMark; //student mark } } } 

Unfortunately, after entering all the data, I get:

 CA4.Program+StudentDetails CA4.Program+StudentDetails CA4.Program+StudentDetails CA4.Program+StudentDetails CA4.Program+StudentDetails 

This is not a failure, only instead of the data that I entered, you will get the following 5 lines.

I know that the reason it does not work is because I do not use structures correctly because there are no problems without them.

Can someone please help me and tell me how to use the structures in the example above. Thanks

Greetings

n1te

+6
source share
4 answers

Your call to Console.WriteLine("{0} ", student[row, column]); implicitly calls the ToString () method of the StudentDetails structure, which by default simply writes the name of the structure type. Override the ToString () method:

 public struct StudentDetails { public string unitCode; //eg CSC10208 public string unitNumber; //unique identifier public string firstName; //first name public string lastName;// last or family name public int studentMark; //student mark public override string ToString() { return string.Format("{0},{1},{2},{3},{4}", unitCode, unitNumber,firstName,lastName,studentMark); } } 

However, the big problem is that you set the properties of 5 different StudentDetails structures ... by declaring an array of StudentDetails[,] student = new StudentDetails[5, 1]; and asking the user to enter details about the structures at different points in the array, i.e. student[0, 0] then student[1,0] , you do not create one StudentDetails object and do not set properties on it, you created 5 different StudentDetails objects.

Why are you using an array? If you want the user to populate one StudentDetails object, just do

 StudentDetails student = new StudentDetails(); Console.WriteLine("Please enter the unit code:"); student.unitCode = Console.ReadLine(); Console.WriteLine("Please enter the unit number:"); student.unitNumber = Console.ReadLine(); ... 

Then, to write this:

 Console.WriteLine("{0}", student); 

This will use the ToString () method that you specified in your StudentDetails structure. (ToString () is called whenever an object needs to be converted to a string, you just don't have to write it)

Hope this helps.

+3
source

This is because ToString() returns the type name by default, you must override it yourself for the StudentDetails struct:

 public struct StudentDetails { public override void ToString() { return String.Format( CultureInfo.CurrentCulture, "FirstName: {0}; LastName: {1} ... ", this.FirstName, this.LastName); } } 

By the way, why are you using stuct and inherited arrays? Instead of <array>, use class instead of struct and Generic IList<StudentDetails> or IDictionary<int, StudentDetails> . Because inherited arrays along with the structure (basically the type value) introduce a box (struct β†’ object) every time you add an element to the array and unboxing (object β†’ struct) when reading it.

+5
source

Others have considered overriding ToString() , so I will not rephrase this information. Moreover, at your request:

Can someone please help me and tell me how to use the structures in the example above.

See MSDN: Structural Design

First, your structure is volatile. Inevitable structures are not always the best solution, but they should be considered ... see the article I use for in the <class> structure in the Dictionary class . However, I would define a basic requirement: does the structure require variability? In other words, will there be a student name, unit number or label change after creating the instance?

Secondly, if the requirement is to have a StudentDetails collection, the array is in order:

 // declare students collection StudentDetail[] students = new StudentDetail[5]; // declare an array indexer int indexer = 0; static void Main(string[] args) { Console.WriteLine("Please enter the unit code:"); string unitCode = Console.ReadLine(); Console.WriteLine("Please enter the unit number:"); string unitNumber = Console.ReadLine(); /* get the rest of your inputs */ AddStudentDetails(unitCode, unitNumber, firstName, lastName, studentMark); } // demonstrate auto-magically instantiated, mutable struct void AddStudentDetails(string unitCode, string unitNumber, string firstName, string lastName, int studentMark) { students[indexer].unitCode = unitCode; students[indexer].unitNumber = unitNumber; students[indexer].firstName = firstName; students[indexer].lastName = lastName; students[indexer].studentMark = studentMark; // increment your indexer indexer++; } 

Note: exception handling is not considered in this example; for example, incrementing outside the array.

In the previous example, you could change any of the properties of the StudentDetails structure after creating the instance. Structures become safer with immutability:

 public struct StudentDetails { public readonly string unitCode; //eg CSC10208 public readonly string unitNumber; //unique identifier public readonly string firstName; //first name public readonly string lastName;// last or family name public readonly int studentMark; //student mark // use a public constructor to assign the values: required by 'readonly' field modifier public StudentDetails(string UnitCode, string UnitNumber, string FirstName, string LastName, int StudentMark) { this.unitCode = UnitCode; this.unitNumber = UnitNumber; this.firstName = FirstName; this.lastName = LastName; this.studentMark = StudentMark; } } 

This requires changing how you add the details object to the students array:

 void AddStudentDetails(string unitCode, string unitNumber, string firstName, string lastName, int studentMark) { students[indexer] = new StudentDetails(unitCode, unitNumber, firstName, lastName, studentMark); // increment your indexer indexer++; } 

Consider the requirements for structure and design.

+2
source

You need to print the structure elements accordingly. I would suggest a method that does printing for you, such as struct

 public void PrintStruct(StudentDetails stDetails) { Console.WriteLine(stDetails.firstName); Console.WriteLine(stDetails.lastName); .... etc } 

Alternatively create a class (this is C #) and override the ToString () method to return a string with all the member information, and you don’t need to change the main code.

Structures have the right to use in C #, but in cases where you need to present data, and not just transfer data, than you should use a class.

+1
source

Source: https://habr.com/ru/post/894977/


All Articles