The relation between objects

  • Thread starter Thread starter Arjen
  • Start date Start date
A

Arjen

Hello,

Let's say that we have an array "persons" with person objects.
We also have an array with "books" with book objects.
We have from a single book only one copy.
This means that it is possible that a persons loans one or more books.

All right, let's say that we have a person ("Name: James; Age: 25") that
loans 2 books ("Title: My first book; ISBN: 23213 & Title: My last book;
ISBN: 89899").

We save our objects... (inside arrays not inside a database)
How can I see which persons have loans which books?
I understand the relations between the objects... but don't know how to get
them back.

Can you give me some sample code?

Many thanks!
 
Arjen,

Sounds like a Homework question to me. Why don't you post some code,
explain were you are stuck and the group will try to help.

Sorry no free rides :)
 
Arjen said:
Hello,

Let's say that we have an array "persons" with person
objects.

Ok, an array of person objects:

Person [] people = new Person[MAX_PEOPLE];
We also have an array with "books" with book objects.

Ok, an array of book objects:

Book [] books = new Book[MAX_BOOKS];
We have from a single book only one copy.

This means that it is possible that a persons loans one or
more books.

If you have designed the Person class to have such a relationship with
objects of the Book class. This is the crucial thing you need to learn: how
to design such classes, and how to establish such relationships between such
classes.

The only way to 'link' these two collections:

Person [] people = new Person[MAX_PEOPLE];
Book [] books = new Book[MAX_BOOKS];

is via some explicit connection between the contained objects. So, for
example, if the classes were something like the following:

class Person
{
...
bool OwnsBook(String bookName) { ... }
...
}

class Book
{
...
// Could use property - this keeps example simple
String BookName() { ... }
...
}

You might then be able to do something like:

...
if (people[idx].OwnsBook(books[idx].BookName())
...

Remember, this is merely one of *many* possibilities !!!
All right, let's say that we have a person ("Name: James;
Age: 25") that loans 2 books ("Title: My first book; ISBN:
23213 & Title: My last book; ISBN: 89899").

We save our objects... (inside arrays not inside a database)

How can I see which persons have loans which books?

It depends entirely how you have designed your classes, and established any
relationships between them. There is no single, general way that this can be
accomplished.
I understand the relations between the objects... but don't
know how to get them back.

Can you give me some sample code?

The response, by Ivan Krivyakov, to your earlier 'Re: Object and objects'
query provided you with an excellent coding example. I suggest that you
spend more time studying it, and try to understand why things were done the
way they were. You can then apply the principles learned from this code to
your Person / Books problem.

I hope this helps.

Anthony Borla

P.S.

This *is* a genuine attempt to help you. You will, very likely, waste a lot
of time, and find yourself quite frustrated, if you you don't try to
understand the basic principles of object design. Once these ideas are clear
to you, any coding then becomes fairly trivial.
 
I agree, this guy has been posting for weeks and not once
showing us an example of what he's doing.
He wanted the syntax for Xml Serialization, i gave it to
him, not a word of thanks (to anyone, not just me) and
then a new question a few hours later asking the same
question.

Some advice for Arjan, go to msdn.com, search on google,
if you have Visual Studio use the help files contained,
with all of the information available you will be able to
solve all of your problems.
Failing that buy a book on the topic of C# (i recommend
the Wrox press ones) and above all say thankyou to people
when they're trying to help you.

jax
 
Thanks!



Jax said:
I agree, this guy has been posting for weeks and not once
showing us an example of what he's doing.
He wanted the syntax for Xml Serialization, i gave it to
him, not a word of thanks (to anyone, not just me) and
then a new question a few hours later asking the same
question.

Some advice for Arjan, go to msdn.com, search on google,
if you have Visual Studio use the help files contained,
with all of the information available you will be able to
solve all of your problems.
Failing that buy a book on the topic of C# (i recommend
the Wrox press ones) and above all say thankyou to people
when they're trying to help you.

jax
 
Remember, this is merely one of *many* possibilities !!!

I have get some other answers from other persons too...
And of cource this helps!

Thanks for your answer.



Anthony Borla said:
Arjen said:
Hello,

Let's say that we have an array "persons" with person
objects.

Ok, an array of person objects:

Person [] people = new Person[MAX_PEOPLE];
We also have an array with "books" with book objects.

Ok, an array of book objects:

Book [] books = new Book[MAX_BOOKS];
We have from a single book only one copy.

This means that it is possible that a persons loans one or
more books.

If you have designed the Person class to have such a relationship with
objects of the Book class. This is the crucial thing you need to learn: how
to design such classes, and how to establish such relationships between such
classes.

The only way to 'link' these two collections:

Person [] people = new Person[MAX_PEOPLE];
Book [] books = new Book[MAX_BOOKS];

is via some explicit connection between the contained objects. So, for
example, if the classes were something like the following:

class Person
{
...
bool OwnsBook(String bookName) { ... }
...
}

class Book
{
...
// Could use property - this keeps example simple
String BookName() { ... }
...
}

You might then be able to do something like:

...
if (people[idx].OwnsBook(books[idx].BookName())
...

Remember, this is merely one of *many* possibilities !!!
All right, let's say that we have a person ("Name: James;
Age: 25") that loans 2 books ("Title: My first book; ISBN:
23213 & Title: My last book; ISBN: 89899").

We save our objects... (inside arrays not inside a database)

How can I see which persons have loans which books?

It depends entirely how you have designed your classes, and established any
relationships between them. There is no single, general way that this can be
accomplished.


I understand the relations between the objects... but don't
know how to get them back.

Can you give me some sample code?

The response, by Ivan Krivyakov, to your earlier 'Re: Object and objects'
query provided you with an excellent coding example. I suggest that you
spend more time studying it, and try to understand why things were done the
way they were. You can then apply the principles learned from this code to
your Person / Books problem.

I hope this helps.

Anthony Borla

P.S.

This *is* a genuine attempt to help you. You will, very likely, waste a lot
of time, and find yourself quite frustrated, if you you don't try to
understand the basic principles of object design. Once these ideas are clear
to you, any coding then becomes fairly trivial.
 
Arjen said:

please note - the thanks is not required, and is often implied (and those
that demand or expect it are often associated with the lower tiers of the
food chain).

some consider the gratuitous response to be nothing more than noise. do it
if you want, but otherwise feel no obligation.

rlf
 
Back
Top