Best Organization of Arraylist inside another object, or unique object enclosing lots of other objec

B

blisspikle

I figure that someone good at dotnet can look at this and give me a
clue on how to easily organize this code?

If there is a unique identifier like "Publisher" with a bunch of
"Book" that are published under them (I used the arraylist class in
the publisher class). How should the code be organized, and how can
the books properties like "Name" be easily called in the main code, or
searched for in the main code?


Public Class Publisher
Public Name As String
Private BookList As ArrayList
Public Sub New()
Name = "default"
End Sub

Public Sub AddBook(ByVal bBook As Book)
BookList.Add(bBook)
End Sub

Public ReadOnly Property Book(ByVal bBook As String) As Integer
Get
Return 1 'Haven't figured out how to search an
arraylist for the index of my book yet....
End Get
End Property

End Class

Public Class Book
Public Name As String

Public Sub New()
Name = "default" ' tag name
End Sub

Public Sub New(ByVal pName As String)
Name = pName ' book name
End Sub
End Class
 
B

Brian Gideon

I figure that someone good at dotnet can look at this and give me a
clue on how to easily organize this code?

If there is a unique identifier like "Publisher" with a bunch of
"Book" that are published under them (I used the arraylist class in
the publisher class). How should the code be organized, and how can
the books properties like "Name" be easily called in the main code, or
searched for in the main code?

Public Class Publisher
Public Name As String
Private BookList As ArrayList
Public Sub New()
Name = "default"
End Sub

Public Sub AddBook(ByVal bBook As Book)
BookList.Add(bBook)
End Sub

Public ReadOnly Property Book(ByVal bBook As String) As Integer
Get
Return 1 'Haven't figured out how to search an
arraylist for the index of my book yet....
End Get
End Property

End Class

Public Class Book
Public Name As String

Public Sub New()
Name = "default" ' tag name
End Sub

Public Sub New(ByVal pName As String)
Name = pName ' book name
End Sub
End Class

Hi,

I think the general pattern is okay. But, instead of using an
ArrayList to store the Book objects use an IDictionary instead (either
Hashtable or Dictionary<Of T>). Dictionaries give you the ability to
store key-value pairs where the key can be used to quickly locate the
value. In your case the key would be the book name and the value
would be the Book object.

Brian
 
B

blisspikle

Brian,

Would Hashtable or IDictionary still work if I had alot of other
properties in my book object like ISBN,NumOfPages,etc? I am not sure
if I understand what a hashtable is. I looked it up and it seems like
it is only good for one value. If I know the name of something, then
the hashtable returns the place where I can find it? I see that
arraylist has a gethashtable method in it, what would that be used
for?

Thanks,
 
B

Brian Gideon

Brian,

Would Hashtable or IDictionary still work if I had alot of other
properties in my book object like ISBN,NumOfPages,etc? I am not sure
if I understand what a hashtable is. I looked it up and it seems like
it is only good for one value. If I know the name of something, then
the hashtable returns the place where I can find it? I see that
arraylist has a gethashtable method in it, what would that be used
for?

Thanks,

Hi,

Clarification...a Hashtable is an IDictionary. A dictionary is a data
structure that can map a key to a value. There are several different
implementations. In the case of a Hashtable (or the generic
Dictionary<Of T>) the data structure is implemented as a hash table.
A hash table works by hashing the key object to find which internal
bucket to store the value object. A single dictionary would not work
well for more than one book attribute. It would be best if the
Publisher had one dictionary per book attribute (the key). One
problem you're going to run into though is that dictionaries require
the key object to be unique. Obviously, it's possible for two
different books to have the same number of pages. You can handle that
scenario by having your BooksByNumOfPages As IDictionary hold an
ArrayList as the value object which itself will hold the Book objects.

Brian
 
R

RobinS

Brian Gideon said:
Hi,

Clarification...a Hashtable is an IDictionary. A dictionary is a data
structure that can map a key to a value. There are several different
implementations. In the case of a Hashtable (or the generic
Dictionary<Of T>) the data structure is implemented as a hash table.
A hash table works by hashing the key object to find which internal
bucket to store the value object. A single dictionary would not work
well for more than one book attribute. It would be best if the
Publisher had one dictionary per book attribute (the key). One
problem you're going to run into though is that dictionaries require
the key object to be unique. Obviously, it's possible for two
different books to have the same number of pages. You can handle that
scenario by having your BooksByNumOfPages As IDictionary hold an
ArrayList as the value object which itself will hold the Book objects.

Brian

--------------------------------------------------------

You can just use a Generic List to do what I think you're trying to do,
which is keep a list of books for a publisher.

If there is more than one book per title, I'd use ISBN instead of name (or
title).

By the way, you should make those public variables Properties.

Public Class Publisher
Public Name As String 'you should make this a property

Private BookList As List(Of Book)
Private targetName as String

Public Sub New()
Name = "default publisher"
BookList = New List(Of Book)
End Sub

Public Sub AddBook(ByVal bBook As Book)
BookList.Add(bBook)
End Sub

Public Sub RemoveBook(ByVal bBook As Book)
BookList.Remove(bBook)
End Sub

'given a name, return the book object
Public Function GetBookInfo(byVal bookName As String) as Book
targetName = bookName
'this runs the delegate procedure against each item
' in the list; whichever ones returns True gets returned
Dim bk as Book = bookList.Find(AddressOf FindNameDelegate)
debug.Print("Book: {0}, Author: {1}", bk.Name, bk.Author)
Return bk
End Function

Public Function FindBookNameDelegate(bk As Book) As Boolean
Return bk.Name = targetBookName
End Function

End Class

Public Class Book

'THESE SHOULD BE PROPERTIES, *especially* if you're
' making them public. I'm just adding author so I'll
' have more than one; add as many as you like.
Public Name As String
Public Author As String

Public Sub New()
Name = "default book" ' tag name
Author = "default author"
End Sub

Public Sub New(ByVal pName As String)
Name = pName ' book name
Author = "Unknown"
End Sub

Public Sub New(ByVal pName As String, pAuthor As String)
Name = pName
Author = pAuthor
End Sub

End Class

This should work. Let me know!

Robin S.
 
B

blisspikle

--------------------------------------------------------

You can just use a Generic List to do what I think you're trying to do,
which is keep a list of books for a publisher.

If there is more than one book per title, I'd use ISBN instead of name (or
title).

By the way, you should make those public variables Properties.

Public Class Publisher
Public Name As String 'you should make this a property

Private BookList As List(Of Book)
Private targetName as String

Public Sub New()
Name = "default publisher"
BookList = New List(Of Book)
End Sub

Public Sub AddBook(ByVal bBook As Book)
BookList.Add(bBook)
End Sub

Public Sub RemoveBook(ByVal bBook As Book)
BookList.Remove(bBook)
End Sub

'given a name, return the book object
Public Function GetBookInfo(byVal bookName As String) as Book
targetName = bookName
'this runs the delegate procedure against each item
' in the list; whichever ones returns True gets returned
Dim bk as Book = bookList.Find(AddressOf FindNameDelegate)
debug.Print("Book: {0}, Author: {1}", bk.Name, bk.Author)
Return bk
End Function

Public Function FindBookNameDelegate(bk As Book) As Boolean
Return bk.Name = targetBookName
End Function

End Class

Public Class Book

'THESE SHOULD BE PROPERTIES, *especially* if you're
' making them public. I'm just adding author so I'll
' have more than one; add as many as you like.
Public Name As String
Public Author As String

Public Sub New()
Name = "default book" ' tag name
Author = "default author"
End Sub

Public Sub New(ByVal pName As String)
Name = pName ' book name
Author = "Unknown"
End Sub

Public Sub New(ByVal pName As String, pAuthor As String)
Name = pName
Author = pAuthor
End Sub

End Class

This should work. Let me know!

Robin S.- Hide quoted text -

- Show quoted text -

I tried out the List(Of Book) and it works great. I am able to get to
all of the methods or properties similar to
Dim Publisher1 as New Publisher
Publisher1.Booklist.Item(0).Name = "bookname"

This diffenently makes it easier on me. There is also another way
that (I think) would take a lot of processor time to achieve.
If Booklist is defined as an arraylist then you can use
Dim Publisher1 as new Publisher
Ctype(publisher1.booklist(0),book).name = "bookname"

Thank you for your help. I like the List(of T) idea
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top