When I register a loan I want the chosen Book-object to be saved to a List-control in the Customer-object.
The idea is to show all the books a customer have lended in a richTextBox.
I did this in an old Java-course and I have been trying to translate it to C#.
In the Customer-Class I have declared:
private List<Bok> loan;
Have a property that returns a booklist:
public list
{
get
{
return loan = new List
}
} I also instanciates the List in a constructor.
Then I do this when try to register the loan:
bookToLend.BiblioteksKund = lender; //bookToLend is a Book-object.
lender.Loan.Add(bookToLend); //lender is a Customer-object, loan is a List of Books.
I get no error but the selected book doesn't seem to be added to the list in the customer-object. Why?
VulpesPosted Apr 13, 2012, 12:57 PM
You just need to return the existing object:
Sam HobbsPosted Apr 15, 2012, 5:36 PM
It is not easy to decide when to say borrow or borrowed instead of lend or lent. The important thing is to know about the word borrow and to understanding its relationship with lending.
There is a member in this community that I will not name but I know he is definitely a native to the USA and English is his primary and probably only language but his grammer is quite different from most people whose native language is English. So yes grammer is difficult to get right. I hope it helps to have help with some of the confusion.
Lars PerssonPosted Apr 15, 2012, 2:18 PM
Hm, not really sure what you mean here. I save a book-object in a List (perhaps control isn't what you call it ) that I have declared in my customer-class. This way I know what books the customer have borrowed
"Finaly, note that the question about "can´t lend a book that is already been lent" seems to be a new question; I think it is independent of the original question."
Yes, but it was about the same issue, saving books in a customer-object, so I decided to keep it in the same thread. This is about judgment I suppose and maybe I made a wrong one.
About lending and borrowing it is confusing. Didn't think of the the borrow actually but in my case I don't think it matters. Thanks for clearing that up though!
VulpesPosted Apr 15, 2012, 2:01 PM
Sam HobbsPosted Apr 15, 2012, 1:37 PM
You say yo are nto sure of the proper grammer. It is interesting that Vulpes is using grammer consistent with your use. Vulpes of course is familiar with the term "borrow". A library lends books that are borrowed by people. So a lender is a library. When a book is lent by a libray it is borrowed by the customer or member. So this is one area where English can be confusing.
Finaly, note that the question about "can´t lend a book that is already been lent" seems to be a new question; I think it is independent of the original question.
Lars PerssonPosted Apr 14, 2012, 10:18 AM
I would get an exception.
That however wasn´t my problem. When I tested I always had a customer-object and if I didn´t the code would break.
The problem was that i didn´t clear the richTextBox where I present the books belonging to the customer.
So when I updated the customer, the same book would be added in the richTextBox.
As it turned out, the code worked fine.
Thanks though for the error you pointed out.
VulpesPosted Apr 14, 2012, 8:40 AM
If you don't do that then, if the book exists and is not lent, the libraryCustomer property would be reset to null and you'd then get an exception when you try to add the book to the customer's Loan collection.
Similarly, if the book is not found, you also don't want to execute the rest of the code to avoid an exception when bookToLend is null.
//Search for chosen customer
foreach (Kund K in CustomerList) //Kund is customer in swedish...
{
//if customer found
if (txtPersonNr.Text.Equals(K.PersonNr.ToString()))
lender = K; //Kunden som hittats läggs i ny variabel.
}
//Om customer is not found
if (lender == null)
{
textBox7.Text = "Bok eller Kund saknas!";
return; // return from method here
}
//Search for chosen book
foreach (Bok B in BookList)
{
//Om chosen book is found
if (textBox6.Text.Equals(B.ISBN.ToString()))
bookToLend = B;
}
//Om chosen book is not found
if (bookToLend == null)
{
textBox7.Text = "Bok eller Kund saknas!";
return; // return from method here
}
if (bookToLend.libraryCustomer == null) //Om bok inte redan är utlånad
{
bookToLend.libraryCustomer = lender; //tilldelar vald kund till bok.
lender.Loan.Add(bookToLend); //Lägger lånad bok i kundens bokLista.
}
Lars PerssonPosted Apr 14, 2012, 8:14 AM
When a Customer lends a book, an object of type Customer-class is set in the book-class.
Now we know that the book has been lent and to lent it (the Customer object).
I set the variable pointing at null, so if it is still null, no one has lended the book.
So I check to see if the variable in the book-object is null or not.
Kund lender = null;
Bok bookToLend = null;
//Search for chosen customer
foreach (Kund K in CustomerList) //Kund is customer in swedish...
{
//if customer found
if (txtPersonNr.Text.Equals(K.PersonNr.ToString()))
lender = K; //Kunden som hittats läggs i ny variabel.
}
//Om customer is not found
if (lender == null)
textBox7.Text = "Bok eller Kund saknas!";
//Search for chosen book
foreach (Bok B in BookList)
{
//Om chosen book is found
if (textBox6.Text.Equals(B.ISBN.ToString()))
bookToLend = B;
}
//Om chosen book is not found
if (bookToLend == null)
textBox7.Text = "Bok eller Kund saknas!";
if (bookToLend.libraryCustomer == null) //Om bok inte redan är utlånad
{
bookToLend.libraryCustomer = lender; //tilldelar vald kund till bok.
lender.Loan.Add(bookToLend); //Lägger lånad bok i kundens bokLista.
}
The idea is this: If the variable of type customer-class is still null (libraryCustomer) no one has lended the book. If so, the chosen customer-object is set in the book object. Now no one else can lend the book and you can't loan it twice.
The problem is that you can loan it twice. What am I missing here?
Lars PerssonPosted Apr 13, 2012, 6:40 PM
I think did this at one point but probably made another error.
When you think about it, it is quite logic.
Now it works!