Questions on how to identify an object in the collection

Asked

Viewed 118 times

0

I’m studying C# and I’m having some questions.. The project I’m doing is a fictional gallery system that registers the Artists, Curators and pieces of arts.

I created the abstract class "Persons" that contains the first and last name of the person, and two classes (Artist and Curator) that inherit from "Persons" and that have the variables artistID and curatorID respectively and the proper constructors.

I created the Artpiece class, which stores all the information of the piece of art and after that, I created three classes/collections (Artists, Curators and Pieces) inheriting from Ienumerable and implementing two methods.

Class/Artists Collection:

List<Artist> artists = new List<Artist>();
public void Add(Artist artist) {artists.Add(artist);}

IEnumerator IEnumerable.GetEnumerator() {return artists.GetEnumerator();}

Curators Class/Collection:

List<Curator> curators = new List<Curator>();
public void Add(Curator curator) {curators.Add(curator);}

IEnumerator IEnumerable.GetEnumerator() {return curators.GetEnumerator();}

Class/Collection Pieces:

List<ArtPiece> pieces = new List<ArtPiece>();
public void Add(ArtPiece piece) {pieces.Add(piece);}

IEnumerator IEnumerable.GetEnumerator() {return pieces.GetEnumerator();}

In the Gallery (the main) class, I instated the collections as follows:

Artists artist = new Artists();
Curators curators = new Curators();
Pieces artPieces = new Pieces();

And now, still in the Gallery Class, I need to make the method to sell a piece of art, where will be updated the status of the piece and added commission to the Curator who is associated to it by the curatorID. the only two parameters this method receives is the price at which the part was sold and the ID of the same and returning true case sale successfully, or false if the operation is not performed, thus remaining:

public bool SellPiece(string pieceID, double price) {}

As I can, by the ID of the piece, change its status to sold, take the information from the ID of the curator associated with it and, by the ID of the curator, add the due commission to it?

I tried to use

var piece = artPieces.Where

To get the Artpiece object that has the ID passed as parameter, but I don’t even see the option .Where to use..

I tried to leave the least "rolled up" possible that question..

  • I decided to use Gets and Sets to go through the objects until I find the necessary ones and make the appropriate changes!

  • Uses Lambda expression

1 answer

0

The .Where() does not work in the artPieces because he’s not a IEnumerable, it is an object that contains a list. The best way to catch the object by id would be:

var artPiece = artPieces.pieces.Single(piece => piece.Id == pieceID);

The single ensures that it will only bring one object. If it returns more or less, it will give exceptions.

Remembering that to do this, the attribute Id must be public.

  • Thank you so much for your help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.