473,473 Members | 2,262 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Using The Remove Method with a Custom Object

a
If the code to insert a new Student is:
Profile.Teachers[tId].Classes[cId].Students.Add(new TCS.Student(id,teacher,
class, name));

what is the code to Remove a student?

I tried the code below, but I don't understand the syntax:
Profile.Teachers[tId].Classes[cId].Students.Remove(new TCS.Student(name));//
This line fails

Any ideas what I need to do to Remove a student based upon something other
than an index number(like their name)?

Thanks,

Paul

Mar 2 '06 #1
7 1680
"a" <a@discussions.microsoft.com> a écrit dans le message de news:
E7**********************************@microsoft.com...

| If the code to insert a new Student is:
| Profile.Teachers[tId].Classes[cId].Students.Add(new
TCS.Student(id,teacher,
| class, name));
|
| what is the code to Remove a student?
|
| I tried the code below, but I don't understand the syntax:
| Profile.Teachers[tId].Classes[cId].Students.Remove(new
TCS.Student(name));//
| This line fails
|
| Any ideas what I need to do to Remove a student based upon something other
| than an index number(like their name)?

Just use the Student instance :

Profile.Teachers[tId].Classes[cId].Students.Add(new TCS.Student(id,teacher,
class, name));

TCS.Student student = Profile.Teachers[tId].Classes[cId].Students[id];

Profile.Teachers[tId].Classes[cId].Students.Remove(student);

Of course, you could redesign your collection classes to allow this
functionality.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Mar 2 '06 #2
HI,

what is the code to Remove a student?

I tried the code below, but I don't understand the syntax:
Profile.Teachers[tId].Classes[cId].Students.Remove(new
TCS.Student(name));//
This line fails


you are creating a new studend and then deleting it from the collection, of
course as this new studend is not yet in the collection (and never will)
nothing happens

this is the correct code

TCS.Student toRemove = null;
foreach( TCS.Student item in Profile.Teachers[tId].Classes[cId].Students
if ( item.Name == name )
{
toRemove = item;
break;
}
if (toRemove!= null )
Profile.Teachers[tId].Classes[cId].Students.Remove( toRemove);

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Mar 2 '06 #3
a
Ignacio:

Thank you for your answer.

Paul
=======================================

"Ignacio Machin ( .NET/ C# MVP )" wrote:
HI,

what is the code to Remove a student?

I tried the code below, but I don't understand the syntax:
Profile.Teachers[tId].Classes[cId].Students.Remove(new
TCS.Student(name));//
This line fails


you are creating a new studend and then deleting it from the collection, of
course as this new studend is not yet in the collection (and never will)
nothing happens

this is the correct code

TCS.Student toRemove = null;
foreach( TCS.Student item in Profile.Teachers[tId].Classes[cId].Students
if ( item.Name == name )
{
toRemove = item;
break;
}
if (toRemove!= null )
Profile.Teachers[tId].Classes[cId].Students.Remove( toRemove);

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Mar 2 '06 #4
a
Joanna:

Thank you for giving me the answer.

This is my first custom object and as such I'm having trouble understanding
how to create it and how to use it.

You wrote: "Of course, you could redesign your collection classes to allow
this
functionality."

Can you recommend a book or other source to help me understand how to do
this, and if you have the time, How would you do this?

Thanks much for your help,
Paul
================================================

"Joanna Carter [TeamB]" wrote:
"a" <a@discussions.microsoft.com> a écrit dans le message de news:
E7**********************************@microsoft.com...

| If the code to insert a new Student is:
| Profile.Teachers[tId].Classes[cId].Students.Add(new
TCS.Student(id,teacher,
| class, name));
|
| what is the code to Remove a student?
|
| I tried the code below, but I don't understand the syntax:
| Profile.Teachers[tId].Classes[cId].Students.Remove(new
TCS.Student(name));//
| This line fails
|
| Any ideas what I need to do to Remove a student based upon something other
| than an index number(like their name)?

Just use the Student instance :

Profile.Teachers[tId].Classes[cId].Students.Add(new TCS.Student(id,teacher,
class, name));

TCS.Student student = Profile.Teachers[tId].Classes[cId].Students[id];

Profile.Teachers[tId].Classes[cId].Students.Remove(student);

Of course, you could redesign your collection classes to allow this
functionality.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer

Mar 2 '06 #5
a
Joanna:

This question is related to my first question.

I have a gridview that contains the student name, if I capture the student
name from the gridview like so:

string s = (GridView_Students.Rows[e.RowIndex].Cells[2].Text); // Student
Name

How can I find the student in my custom object collection by the student
name? Currently it appears that the only way to find the student is by an
index, like so:

TCS.Student student = Profile.Teachers[tId].Classes[cId].Students[id];

Thank you,
Paul

==================================================

"Joanna Carter [TeamB]" wrote:
"a" <a@discussions.microsoft.com> a écrit dans le message de news:
E7**********************************@microsoft.com...

| If the code to insert a new Student is:
| Profile.Teachers[tId].Classes[cId].Students.Add(new
TCS.Student(id,teacher,
| class, name));
|
| what is the code to Remove a student?
|
| I tried the code below, but I don't understand the syntax:
| Profile.Teachers[tId].Classes[cId].Students.Remove(new
TCS.Student(name));//
| This line fails
|
| Any ideas what I need to do to Remove a student based upon something other
| than an index number(like their name)?

Just use the Student instance :

Profile.Teachers[tId].Classes[cId].Students.Add(new TCS.Student(id,teacher,
class, name));

TCS.Student student = Profile.Teachers[tId].Classes[cId].Students[id];

Profile.Teachers[tId].Classes[cId].Students.Remove(student);

Of course, you could redesign your collection classes to allow this
functionality.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer

Mar 2 '06 #6
a
Joanna:

I just implemented the code that Ignacio Machin sent in answer to my first
question and using his code answers my second question to you about finding
the student by name instead of index number. At least it works if I need to
find all instances of the name...(not sure about finding a particular
instance of a name, though.)

Thanks,
Paul
================================

"Joanna Carter [TeamB]" wrote:
"a" <a@discussions.microsoft.com> a écrit dans le message de news:
E7**********************************@microsoft.com...

| If the code to insert a new Student is:
| Profile.Teachers[tId].Classes[cId].Students.Add(new
TCS.Student(id,teacher,
| class, name));
|
| what is the code to Remove a student?
|
| I tried the code below, but I don't understand the syntax:
| Profile.Teachers[tId].Classes[cId].Students.Remove(new
TCS.Student(name));//
| This line fails
|
| Any ideas what I need to do to Remove a student based upon something other
| than an index number(like their name)?

Just use the Student instance :

Profile.Teachers[tId].Classes[cId].Students.Add(new TCS.Student(id,teacher,
class, name));

TCS.Student student = Profile.Teachers[tId].Classes[cId].Students[id];

Profile.Teachers[tId].Classes[cId].Students.Remove(student);

Of course, you could redesign your collection classes to allow this
functionality.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer

Mar 2 '06 #7
Firstly - for the record I have developed solutions for managing
school/student records, and one thing we decided early: don't do *anything*
based on names (alone) [ok, except perhaps searching]. It's just too
unreliable - particularly when there are regional influences (religious,
political, historical, etc) on names. Up to you, though...

I know you've had several solutions; an additional few (in 2.0), depending
on what your Students class is:

if it involves List<Student> you could use the predicate syntax:

Students.RemoveAll(delegate(Student s) {return s.Name == name});

(of course, this won't "break" after the first one like Ignacio's
suggestion; maybe good, maybe bad)

Alternatively, if you can decide on a logical key (unique field) for your
objects, you could use (e.g. for the name) Dictionary<string, Student>,
whose Remove method will then take the string (of the name):

Students.Add(s.Name, s);
Students.Remove("Freddy");

Personally, I'd be looking for something more unique as my first port of
call... a database ID (primary key) is never a bad choice, although if there
is a logical existing figure (e.g. "UPN" = unique pupil number in some UK
circles) then this might be better.

Marc
Mar 3 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: CroDude | last post by:
I've made a custom groupbox control. Inside, as one of it's members is CSimpleGradient object. CSimpleGradient is a wrapper class for gradient usage. Basically it looks like that: public class...
4
by: Kevin Phifer | last post by:
Ok, before anyone freaks out, I have a solution I need to create that gathers content from maybe different places. Each one can return a <form> in the html, so its the classic can't have more than...
5
by: Marcel Gelijk | last post by:
Hi, I am trying to create a User Control that is located in a seperate class library. The User Control contains a textbox and a button. The page generates an exception when it tries to access...
3
by: Don | last post by:
My user control has a combobox with an arraylist attached to it along with custom add and remove methods. The "Add" method is working great. However I don't understand why the "Remove" method...
7
by: Peter Row | last post by:
Hi, I've started work on my own control some parts of which use standard controls, others I need to draw on my controls surface to get the display output I require, however.... I seem to be...
0
by: a | last post by:
I'm trying to delete an item from a collection, by clicking on a delete button in a GridView control. The item in the collection is stored in the Profile object (serialized as xml in the...
6
by: Erick | last post by:
I've created a class called Procs and a collection class called Processes which uses a hastable object to store the Procs. Now i want to enumerate with the "For each" to extract all the Procs in...
10
by: AZRebelCowgirl73 | last post by:
This is what I have so far: My program! import java.util.*; import java.lang.*; import java.io.*; import ch06.lists.*; public class UIandDB {
6
by: GiJeet | last post by:
hello, I'm trying to use a dictionary as a class member. I want to use a property to get/set the key/value of the dictionary but I'm confused as how to use a dictionary as a property. Since there...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.