Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Search | Today's Posts | Mark Forums Read
BOOK: Beginning Visual Basic 2005 ISBN: 978-0-7645-7401-6
This is the forum to discuss the Wrox book Beginning Visual Basic 2005 by Thearon Willis, Bryan Newsome; ISBN: 9780764574016
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual Basic 2005 ISBN: 978-0-7645-7401-6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 3rd, 2006, 06:29 AM
Registered User
 
Join Date: Nov 2006
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default P154

I don't see how the code can work

The list box is populated using:

Me.lstCustomers.Items.Add(objNewCustomer.ToString)

This calls the override function which returns:

Return Name & " (" & Email & ")"

How can the 'Public ReadOnly Property SelectedCustomer() As Customer' return a 'Customer' from:

Return lstCustomers.Items(lstCustomers.SelectedIndex)

???

  #2 (permalink)  
Old November 6th, 2006, 06:52 AM
Thearon's Avatar
Wrox Author
 
Join Date: Dec 2003
Location: Fuquay Varina, NC, USA.
Posts: 396
Thanks: 0
Thanked 8 Times in 8 Posts
Default

I think the How It Works section on page 155 explains this pretty clearly. If you have already read this then please ask a specific question and I'll be happy to try to clear things up for you.

Thearon
  #3 (permalink)  
Old November 6th, 2006, 01:38 PM
Registered User
 
Join Date: Nov 2006
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The following line of code is expected to return a customer:

Return lstCustomers.Items(lstCustomers.SelectedIndex)

How can this be so? The items in the listbox are in the form (for example) "Bill Scott (bscott@somecompany.com)" - How does this translate into a customer structure which the property must return?

  #4 (permalink)  
Old November 8th, 2006, 06:16 PM
Thearon's Avatar
Wrox Author
 
Join Date: Dec 2003
Location: Fuquay Varina, NC, USA.
Posts: 396
Thanks: 0
Thanked 8 Times in 8 Posts
Default

The CustomerCollection class, defined in the objCustomers object, inherits the .Net Framework Collections.CollectionBase class. Every item in the collection is indexed based. Thus the first item is item 0, the second item is item 1. Using the selected index value of the lstCustomers listbox, we can determine which items is selected (e.g. 0, 1, 2) and then use that number to access the item in the customer in the CustomerCollection class, defined in the objCustomers object.

Does this help?

Thearon
  #5 (permalink)  
Old November 9th, 2006, 12:04 PM
Registered User
 
Join Date: Nov 2006
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes thank you, it all makes sense now. There is nothing wrong with the code in the book. I am doing the example in a web form and I thought it wouldn't matter but I'd forgetten about the module level variable so it keeps losing it's value and I get an error. I have solved the problem. For any one else doing it the same way you have to put <Serializable()> before the 'Public Structure Customer' declaration and include the following vb code on the form:

    Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If (Me.ViewState("arrayListInViewState") IsNot Nothing) Then
            objCustomers = CType(Me.ViewState("arrayListInViewState"), ArrayList)
        End If

    End Sub

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender

        'Save PageArrayList before the page is rendered.
        Me.ViewState.Add("arrayListInViewState", objCustomers)

    End Sub


 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using A Custom Assembly, chap 5 p154 - 155 jmurdock BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 2 June 20th, 2007 05:01 PM



All times are GMT -4. The time now is 07:03 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.