d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Updating/deleting An Object In An Array > C#
Add Reply New Topic New Poll
Member
Posts: 9,805
Joined: Jul 8 2008
Gold: 9.00
Jan 21 2016 12:23pm
Just curious as to what might be the most efficient way to accomplish this. I am new to using C# and im trying to redo some of my old school projects. I have a student object (student ID, first name, last name, gpa, credit hours, and 2 enums: academic standing and classification(freshman, sophomore etc)). In my main i built a switch and id like to allow for the option to make changes to or delete a record. My program is set up so that everything is indexed by student ID, when the program is first ran it displays the ID of every student (currently 5) and to view a record you enter an ID from the list.

Code
bool done = false;
while (!done)
{
Console.WriteLine("\nPlease choose one of the following options: ");
Console.WriteLine("1: View a student Record");
Console.WriteLine("2: Update a record");
Console.WriteLine("3: Delete a record");
Console.WriteLine("4: Terminate");
Console.WriteLine("5: Bonus Round!");
string choice = Console.ReadLine();
switch (choice)
{
case "1":
Console.Write("Enter Student ID: ");
stuCodeID = long.Parse(Console.ReadLine());
Console.WriteLine("====================================");
Console.WriteLine("Student Record");
Console.WriteLine("------------------------------------");
Console.WriteLine(listStudents[stuCodeID]);
Console.WriteLine("====================================");
break;

case "2":
Console.WriteLine("\nUnavailable at this time.");
break;

case "3":
Console.WriteLine("\nUnavailable at this time.");
break;

case "4":
Console.WriteLine("\nTerminating");
done = true;
break;

case "5":
System.Diagnostics.Process.Start("http://xkcd.com/292/");
break;
}
}


Code
public Student()
{
Random rnd = new Random();
stuID = rnd.Next(100000, 999999);
stuFirstName = "";
stuLastName = "";
stuGPA = 0.00M;
stuCreditHours = 0;
stuStanding = AcademicStanding.Unknown;
stuClassification = StudentClassification.Unknown;
}
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jan 21 2016 03:27pm
you asked a question about arrays and posted code that doesn't include arrays. if you're asking specifically about arrays, then it's the same way in every language. search it one by one then update it.

on the other hand, if you just want to use it easily and dont need an array, just use Dictionary.

either way, have some sort of Data layer to handle it separate from the UI.

This post was edited by carteblanche on Jan 21 2016 03:28pm
Member
Posts: 12,786
Joined: May 17 2013
Gold: 4,010.00
Jan 26 2016 07:41am
You just store the object in a variable and update the array position after changing the information on said variable.

Also, just as a tip, when using .Parse you need to handle possible exception (if it's not a number).
You could use TryParse instead, as it returns a bool whether the parse was successful or not.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll