PC Review


Reply
Thread Tools Rate Thread

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

 
 
blisspikle
Guest
Posts: n/a
 
      5th Feb 2007
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

 
Reply With Quote
 
 
 
 
Brian Gideon
Guest
Posts: n/a
 
      5th Feb 2007
On Feb 5, 8:29 am, "blisspikle" <eklas...@metforming.com> wrote:
> 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

 
Reply With Quote
 
blisspikle
Guest
Posts: n/a
 
      5th Feb 2007
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,

 
Reply With Quote
 
Brian Gideon
Guest
Posts: n/a
 
      5th Feb 2007
On Feb 5, 12:52 pm, "blisspikle" <eklas...@metforming.com> wrote:
> 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

 
Reply With Quote
 
RobinS
Guest
Posts: n/a
 
      6th Feb 2007

"Brian Gideon" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Feb 5, 12:52 pm, "blisspikle" <eklas...@metforming.com> wrote:
>> 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
>


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

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.


 
Reply With Quote
 
blisspikle
Guest
Posts: n/a
 
      21st Feb 2007
On Feb 5, 7:02 pm, "RobinS" <Rob...@NoSpam.yah.none> wrote:
> "Brian Gideon" <briangid...@yahoo.com> wrote in message
>
> news:(E-Mail Removed)...
>
>
>
>
>
> > On Feb 5, 12:52 pm, "blisspikle" <eklas...@metforming.com> wrote:
> >> 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

>
> --------------------------------------------------------
>
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem using an object inside another object.. Michel S. Microsoft Excel Programming 2 1st Mar 2007 09:59 PM
Find object inside arraylists by object attribute Arjen Microsoft C# .NET 4 27th Jul 2005 05:59 PM
Embedding an *object* INSIDE another object Hari Prasadh Microsoft Powerpoint 2 11th Feb 2005 11:44 AM
Memory question when returning an object from inside another object Steven Blair Microsoft C# .NET 10 11th Jan 2005 07:15 PM
Active Directory Objects - Object reference not set to an instance of an object. Jason Dunbar Microsoft C# .NET 1 25th Jun 2004 07:55 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:36 AM.