Since all you have done in the last day is dump your complete homework question on us, to add to your code dump, I'll ignore the question completely, and comment instead on the code. Do note that this will not complete the task for you, all I am going to do is suggest things you could have done better, which may have lead to a more obvious way to actually complete your homework. That part is, of course, up to you - you are the one who gets the grades as a result of this, not me!
Firstly, separate your Student out of the Form1 class. Change it from a
struct
to a full blown public class. Don't worry about why, you will understand later. When you do that, create a constructor which takes a string, and override the ToString method. You will use these to handle the data storage. Make the internal fields public properties.
In the constructor, break the string up as you have been doing, and store it away in the relevant Student fields. You might want to use a different separator if you have control over the text file format: space is not common. Try comma, or vertical bar. Comma is good: it is the C in CSV. Why? What happens if you get two John Smith students? You have to distinguish them by middle name, or nickname. But, if you use space as a field separator, how do you do that?
In the overridden ToString method, return a string which is compatible with the constructor. I.e. you can construct a new Student from the output of an existing Student.ToString method.
This means that the Student becomes responsible for what is in it's record, not your main form.
Your main form now becomes simpler: Read in all the lines of the text file, and construct a student from each of them, storing the students in some structure.
Now storage: Don't use an array. Very inflexible, rigid things. As you have seen, you have to start out knowing how many Students you are going to have. Nasty.
Use a List instead. And only generate it once, in the Form.Load event. So, make the List a class level variable.
There are also much better ways to read files, particularly text files.
My version of your entire create-the-array-of-students code is a simple method:
I've done it in C#, because 1) my preferred C#-to-VB converter site is down, and 2) I don't want to give it to you on a plate!
List<Student> myStudents = new List<Student>();
...
private void GetStudents(string path)
{
myStudents.Clear();
string[] lines = File.ReadAllLines(path);
foreach(string line in lines)
{
myStudents.Add(new Student(line));
}
}
When you have done this, you may be interested to know that a List has a Sort method... Have a look:
MSDN[
^]