"Armin Zingler" <az.nospam@freenet.de> wrote in message
news:eJ5dKljHGHA.1332@TK2MSFTNGP10.phx.gbl...[color=blue]
> "me" <cmp@notvalid.com> schrieb[color=green]
>> Hi All,
>>
>> I am new to Classes and learniing the ropes with VB.NET express
>>
>> Here's my question - say I have a want to manage a list of books.
>> Each book has an Author, Title and ISBN
>>
>> Now, I am used to using Arrays so I would normally do something like
>> this:
>>
>> Set an array up during the init routine (called from form_load) say
>> of Books(100) of type Book (which is setup with Author as string,
>> Title as string etc.)
>>
>> Allow the user to add/delete/edit a new book (with a simple VB form)
>>
>> When it comes to saving the file, just cycle through the array and
>> dump each line to a CSV text file.
>>
>> Opening - again cycle through the CSV file and fill the array.
>>
>> Now - here's my question:
>>
>> How can I do this using classes? I have set up a class (called Book)
>> with properties such as Title, Author, ISBN and can get/set them
>>
>> I create a new instance, i.e. myBook = New Book.Book
>>
>> However, I now dont understand.... This creates one instance, which
>> i can fill with information. However, if i want another book, I have
>> to create
>>
>> myBook2 = New Book.Book[/color]
>
>
> If your array's name is 'Books', you can store the reference to a new book
> directly in this array:
>
> Books(Index) = New Book.Book
>
> Now the item at position Index points to the book that has been created in
> the same line.
>
> 'Index' is a variable that you will have to manage. It should point to the
> first empty position within the array. At the beginning, the value of
> Index
> is 0. After you stored a reference to a new book at the position that
> Index
> points to, Index has to be incremented by 1, so next time you will store a
> new book at the next position in the array. There are alternatives better
> than arrays that I'll mention below.
>
> After this, you can set the book's properties:
>
> Books(Index).Title = ...
> Books(index).author = ...
>
> Or, if you have many properties:
>
> With Books(Index)
> .Title = ...
> .author = ...
> End With
>
>[color=green]
>> So how does one then cycle through these items when saving? How do
>> we create all the instances when first loading the CSV file?
>>
>> I tried myBook(1000) = New Book.Book but got an error.
>>
>> I tried using
>>
>> myBookGroup as Collections
>>
>> but still... I dont understand - how can I refer to each individual
>> book in a loop for example when reading/writing the file?
>>
>> Am I misunderstanding the point? Should I just stick with arrays?[/color]
>
>
> Using arrays and classes is not a contradiction. BTW, an array is class,
> too.
>
> To loop through all books in the array:
>
> For i as integer = 0 to index - 1
> Debug.writeline(books(i).title)
> Debug.writeline(books(i).author)
> next i
>
> You see how to access the books in the array and how to access the
> properties. You can access them the same way if you write them to a file.
> I
> don't know how you do it, thus I use debug.writline here.
>
> There are alternatives:
>
> For i as integer = 0 to index - 1
> dim LoopBook as book
> LoopBook = books(i)
> Debug.writeline(LoopBook.title)
> Debug.writeline(LoopBook.author)
> next i
>
> or
>
> For i as integer = 0 to index - 1
> with books(i)
> Debug.writeline(.title)
> Debug.writeline(.author)
> end with
> next i
>
>
> Now we come to how to read the books from the file. I don't know how
> exactly
> you intend to read the file, thus I focus on creating the objects and
> storing them in the array:
>
>
> index = 0
>
> do until end-of-file
> dim Author, Title as string
> 'read one line and store the values in 'Author' and 'Title'
> '...
>
> books(index) = new book
> books(index).title = title
> books(index).author = author
> index += 1
> loop
>
>
> As you see, using an array can be much work:
>
> - You can not change the size of an array. If you need more/less items,
> you
> have to create a new array and copy the content of the old array into the
> new one (ReDim Preserve).
>
> - If you define your array being able to hold 100 items maximum, you need
> an
> additional variable, called 'Index' in this example, that tracks the
> number
> of items that are used within the array.
>
> - You can not delete items from the array. You would have to copy items to
> avoid having an unused item in the array. An unused item is 'Nothing',
> i.e.
> it does not point to a book.
>
> - If, within the array, unused items are allowed, you would have to loop
> through all the items if you want to find an unused item where you can
> store
> the reference to a new book.
>
>
> Therefore, you'd better use another sort of collection. An arraylist is
> often used. See the namespace System.Collections for more information.
> With
> an arraylist, you can add and remove items without caring about the space
> for new/removed items.
>
> dim books as new arraylist
>
> To add a new book - also when reading the file:
>
> dim b as book
>
> b = new book
> b.title = ...
> b.author = ...
>
> books.add(b) 'add the book to the arraylist
>
> Use books.RemoveAt (or books.remove) to remove a book from the arraylist.
> Use the arraylist's count property to get the number of items in the list.
> As an arraylist can contain references to any type of object, the type of
> the arraylist's 'Item' property is 'Object'. For this reason, you have to
> use type casting:
>
> dim b as book
>
> b = directcast(books(17), book)
> debug.writline(b.title)
> debug.writline(b.author)
>
>
>
> See also:
>
http://msdn.microsoft.com/library/en...sOfObjects.asp
>
>
http://msdn.microsoft.com/library/en...ollections.asp
>
>
http://msdn.microsoft.com/library/en...Components.asp
> (especially: "Walkthrough: Creating Your Own Collection Class")
>
>
> (As I see now, the links contain already all you need. :-)
>
>
> Armin[/color]
Thanks for the feedback Armin,
So is an arraylist like a collection? Only, not for classes, just for
variables with types?