First time posting here. Well. I’m trying to make a program that can makes you delete a student and add and so forth. But when i delete and i add it again and then i delete it again I have this error message. ‘Unhandled exception. System.IndexOutOfRangeException: Index is out of range.’
Here’s the code.
Students.CS
`using System;
using System.Collections.Generic;
namespace StudentsSelector
{
public static class StudentsList
{
public static string[] AllStudents { get; private set; }
private static int studentCount;
// New property for assigned roles
public static Dictionary<string, string> AssignedRoles { get; set; }
// Static constructor to initialize arrays
static StudentsList()
{
AssignedRoles = new Dictionary<string, string>();
AllStudents = new string[]
{
// List of initial students
"Alex Jose Rodriguez Taveras",
"Amaury Daniel Romero Gonzalez",
"Ashlee Ramirez Rosario",
"Cyd Marie Jorge Chapman",
"Edison Yadir Rossis",
"Edwin Oscar Perez Rodriguez",
"Eli Samuel Suero Rodriguez",
"Erickson David Encarnacion Encarnacion",
"Eudy Yunior Lorenzo Ramirez",
"Jamil Guzman Feliz",
"Joan Manuel Arroyo Valerio",
"Jose Miguel Canela Santos",
"Maria Del Carmen Diaz Campanas",
"Maria Marlene Abreu Saiz",
"Marlon Miguel Vargas Mendez",
"Michael Dmeshell Sanchez Heredia",
"Odana Margarita Calderon Pache",
"Oscar Daniel Tuletta Mercedes",
"Rafael Antonio Urbaez Hernandez",
"Smith Morillo Encarnacion",
"Teudy Joshua Encarnacion Fulgencio",
"Xander Ruddy Cruz De La Rosa",
"Yadianna Vargas Pimentel",
"Yafreisy Emelin Alvarez Capellan",
"Yoelmi Alexander Alcala Valdez"
};
studentCount = AllStudents.Length;
}
// Method to reset all students (for testing or initialization)
public static void ResetAllStudents()
{
AllStudents = new string[]
{
// Reset the list of students
"Alex Jose Rodriguez Taveras",
"Amaury Daniel Romero Gonzalez",
"Ashlee Ramirez Rosario",
"Cyd Marie Jorge Chapman",
"Edison Yadir Rossis",
"Edwin Oscar Perez Rodriguez",
"Eli Samuel Suero Rodriguez",
"Erickson David Encarnacion Encarnacion",
"Eudy Yunior Lorenzo Ramirez",
"Jamil Guzman Feliz",
"Joan Manuel Arroyo Valerio",
"Jose Miguel Canela Santos",
"Maria Del Carmen Diaz Campanas",
"Maria Marlene Abreu Saiz",
"Marlon Miguel Vargas Mendez",
"Michael Dmeshell Sanchez Heredia",
"Odana Margarita Calderon Pache",
"Oscar Daniel Tuletta Mercedes",
"Rafael Antonio Urbaez Hernandez",
"Smith Morillo Encarnacion",
"Teudy Joshua Encarnacion Fulgencio",
"Xander Ruddy Cruz De La Rosa",
"Yadianna Vargas Pimentel",
"Yafreisy Emelin Alvarez Capellan",
"Yoelmi Alexander Alcala Valdez"
};
studentCount = AllStudents.Length;
}
public static void ResizeArray()
{
// Create a new array with the increased size
string[] newArray = new string[AllStudents.Length + 1];
Array.Copy(AllStudents, newArray, AllStudents.Length);
AllStudents = newArray;
}
// Method to remove a student at a specific index
public static void RemoveStudentAt(int index)
{
if (index < 0 || index >= studentCount)
{
throw new IndexOutOfRangeException("Index is out of range.");
}
for (int i = index; i < studentCount - 1; i++)
{
AllStudents[i] = AllStudents[i + 1];
}
AllStudents[studentCount - 1] = string.Empty;
studentCount--;
}
// Method to get the current student count
public static int GetStudentCount()
{
return studentCount;
}
}
}
`
DeleteStudent.CS
`using System;
namespace StudentsSelector
{
class GetRidStudents
{
public static void DeleteStudents()
{
string[] Students = StudentsList.AllStudents;
Console.Write("Ingrese el nombre del estudiante a eliminar: ");
string? UserSelection = Console.ReadLine();
if (UserSelection != null)
{
bool studentDeleted = false;
for (int i = 0; i < Students.Length; i++)
{
if (!string.IsNullOrEmpty(Students[i]) && Students[i].IndexOf(UserSelection, StringComparison.OrdinalIgnoreCase) >= 0)
{
StudentsList.RemoveStudentAt(i);
studentDeleted = true;
i--;
}
}
if (studentDeleted)
{
Console.WriteLine("Lista de estudiantes actualizada:");
foreach (string student in StudentsList.AllStudents)
{
if (!string.IsNullOrEmpty(student)) // Mostrar solo los nombres válidos
{
Console.WriteLine(student);
}
}
}
else
{
Console.WriteLine("No se encontraron estudiantes que coincidan con la entrada.");
}
}
else
{
Console.WriteLine("La entrada fue nula. Por favor, ingrese una parte válida del nombre del estudiante.");
}
}
}
}
`
if someone can help me please say so! I’d be paying much attetion here, thanks and have a blessed day.
I tried rewriting the code, adding more methods to check the index and so on, didn’t work. I also tried GPT, nothing worked
Joshua Encarnacion is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.