Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Visual Basic 2005 with .NET 3.0 Programmer's Reference ISBN: 978-0-470-13705-5
This is the forum to discuss the Wrox book Visual Basic 2005 with .NET 3.0 Programmer's Reference by Rod Stephens; ISBN: 9780470137055
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Visual Basic 2005 with .NET 3.0 Programmer's Reference ISBN: 978-0-470-13705-5 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old January 21st, 2008, 10:12 AM
Authorized User
 
Join Date: Dec 2006
Location: , , .
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default Passing collections

I need to replace a collection when it is passed from another class.
In the sample code below, test 1 gives the correct result (ie "Col2") and test2 gives the wrong result ("Col1"). Unfortunately I need to use the indirect method used in test2 (as "oldCol", "setCol" and the tests are actually in a different class from the rest of the code).

Please can you help to sort out test2 so it can still be called without any parameter but returns col2.

Many thanks

Phil


Module Module1

    Sub Main()
        Dim col As New Collection
        col.Add("Col1")
        setCol(col)

        test1(col)
        MsgBox("test1=" & col(1))

        col.Clear()
        col.Add("Col1")
        test2()
        MsgBox("test2=" & col(1))
    End Sub

    Sub sub1(ByRef oldcol As Collection)
        Dim newCol As New Collection
        newCol.Add("Col2")
        oldcol = newCol
    End Sub
'-----------------------------------New class
    Dim oldCol As Collection

    Sub setCol(ByRef col As Collection)
        oldCol = col
    End Sub

    Sub test1(ByRef col As Collection)
        sub1(col)
    End Sub

    Sub test2()
        sub1(oldCol)
    End Sub
End Module


Reply With Quote
  #2 (permalink)  
Old January 21st, 2008, 11:42 AM
Rod Stephens's Avatar
Wrox Author
Points: 2,999, Level: 22
Points: 2,999, Level: 22 Points: 2,999, Level: 22 Points: 2,999, Level: 22
Activity: 22%
Activity: 22% Activity: 22% Activity: 22%
 
Join Date: Jan 2006
Location: , , .
Posts: 601
Thanks: 1
Thanked 84 Times in 83 Posts
Default

I'm not sure you can do it exactly like this. When you call test2, it calls sub1 which updates oldCol. But oldCol is just a reference to the original collection col. The variable col is also a reference, initially to the same collection, but making oldCol point to a new collection doesn't change what col points at.

You need some sort of common variable that both the main program and the class can both see if you want this to work. For example, you could have the main program not keep its own reference to the collection and have the class expose oldCol publicly. Or you could use a global variable (as much as I prefer to avoid them).

Rod

Rod Stephens, Visual Basic MVP
RodStephens@vb-helper.com

*** New Book ***
"Visual Basic 2008 Programmer's Reference"

Sign up for the free VB Helper Newsletters at http://www.vb-helper.com/newsletter.html
Reply With Quote
Reply


Thread Tools
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
collections help bennyt BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 3 July 26th, 2008 01:30 AM
Collections and Recordsets MLC1985 Beginning VB 6 10 August 1st, 2007 09:13 AM
Collections in C#? shenku C# 1 August 17th, 2005 06:16 AM
Collections question GregV VB.NET 2002/2003 Basics 0 August 6th, 2003 12:24 PM



All times are GMT -4. The time now is 05:58 AM.


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