0

I am struggling (really struggling) as I am a complete novice with C# and VS 2012. I have been asked to create a simple library system where the user types in a book title and an isbn an then there are 2 buttons, one to add the book and one to remove the book. I am struggling to find a way to add the book... Is there a specific way to do this? or can i use lists, dictionaries etc...

Any help would be much appreciated as i am sat looking at my screen and cant find help any where else :(

user3008643
  • 89
  • 2
  • 8
  • 1
    yes you can (use list, dictionaries, whatever you want). Now, what did you try? what do you mean you sat looking at the screen? how does this help? do you have any idea on coding at all? – Noctis Nov 19 '13 at 11:57
  • Web, WPF or Windows Forms application? – Panagiotis Kanavos Nov 19 '13 at 11:58
  • At lest draw the interface as a startup maybe – Bolu Nov 19 '13 at 11:59
  • Do you have a `Book` class? (I am strugling not to make a `read` joke here ;-)), But I you have `var bookList = new List();` and then `bookList.Add(new Book());` – Stefan Nov 19 '13 at 12:00
  • after you add the book to a list (for example) , do you want to show the current added book ? or you want to save it to database (a file for example)? – T-D Nov 19 '13 at 12:08

2 Answers2

1

You can use Dictionary or List<T> it's indifferent:

1)Method with Dictionary:

  • declare a global dictionary

Dictionary <string, string> books = new Dictionary<string, string>();

  • In btnOk simply add the value that you receive from input

    dictionary.Add("book", "isbn");

  • In btnRemove add this code

    d.Remove("Book");//Something like removeat
    

2)If you want use List(I prefer this method)

  • Declare a class with two string properties book and isbn
  • Declare a List of your class
  • In bntOk use myList.Add(new myClass{book=Inputbook, isbn=InputIsbn});
  • in btnRemove before search the item that you want delete and then delete the obj from the list var itemToRemove = myList.Single(r => r.Book == "BookToRemove); myList.Remove(itemToRemove);
Tinwor
  • 7,765
  • 6
  • 35
  • 56
0

I haven't used c# for a while but i guess it depends on the way you have set your program and GUI up to work. You could simply define an ArrayList:

ArrayList bookTitles = new ArrayList();

Then simple retrieve the input from a text box:

bookTitles.Add(txtTitle.Text);

When The Add button is clicked. To do this in VS you can add a button click event listner by double clicking the button on your 'design' page i believe.

Depending on weather or not you have a 'book' class, you could simple use another ArrayList for your isbn numbers, or just have an array of 'book' objects.

WireInTheGhost
  • 373
  • 5
  • 11
  • you haven't used it long enough to not know ArrayList is frowned upon these days :) ... – Noctis Nov 19 '13 at 12:23
  • Oh really? Is this due to performance/ safety factors resulting from storing everything as 'objects'? – WireInTheGhost Nov 19 '13 at 12:33
  • Don't trust me, look at what Mark Gravell has to say [in this answer](http://stackoverflow.com/a/725464/1698987) (he has 200 times my reputation ... I hope he knows what he's talking about ... ) – Noctis Nov 19 '13 at 12:54
  • Cheers guys... I managed to do it after a bit more hard thinking :) Just one last question as I am really struggling to finish my small project. I just want to search for a book through a textbox (which are already added to the dictionary) and for example if i type harry potter in it will find the matching harry potter book with the correct isbn (which are already added) Any help on this please? – user3008643 Dec 08 '13 at 14:03