Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > VB.NET 2002/2003 Basics
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics 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 Search this Thread Display Modes
  #1 (permalink)  
Old February 27th, 2004, 08:03 AM
Friend of Wrox
 
Join Date: Jul 2003
Location: York, , United Kingdom.
Posts: 142
Thanks: 0
Thanked 2 Times in 2 Posts
Default Populating a collection

I'm trying to populate a collection in a similar way to the way a dataadapter populates a dataset with the fill method;

The business tier object Deliveries has the folling method that will populate its internal collection with delivery objects

  Public Sub GetDeliveryDetails(ByVal JobNumber As String)
    oDbOps.GetDeliveryDetails(JobNumber, Me)
    Me.ToggleInitialisationComplete()
  End Sub

The 'oDbOps' refers to the Operations object of the Database tier, which has the following method...

Public Sub GetDeliveryDetails(ByVal JobNumber As String, _
    ByVal Deliveries As Delivery.Deliveries)

    ' get information from delivery details, delivery notes and pallets,
    ' then populate the oDeliveries object...
    Dim oDelivery As Delivery.Delivery
    Dim cmdDDSelect As SqlCommand = mdbObjects.DeliveryDetails_SelectCommand( _
        Objects.DeliveryDetailsSelectCommandType.ByJobNumb er)
    Dim cmdPallSelect As SqlCommand = mdbObjects.Pallets_SelectCommand( _
    Objects.PalletsSelectCommandType.FirstAndLastPalle t_ByJobNumber)

    ' add values to parameters before running commands...
    cmdDDSelect.Parameters("@pJobNumber").Value = JobNumber
    cmdPallSelect.Parameters("@pJobNumber").Value = JobNumber

    Deliveries = PopulateDeliveryDetails(cmdDDSelect, cmdPallSelect)
End Sub

The internal 'Deliveries' object is merrily populated with delivery objects, but this doesn't get cascaded back to the calling object, no matter whether I pass it in ByRef or ByVal. Does anyone have any notion of what is going wrong?

Thanks in advance

Reply With Quote
  #2 (permalink)  
Old February 27th, 2004, 12:04 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I am not a fan of return arguments. Particularly in this case when you really don't need them, but that is why you are getting this problem...

Public Sub GetDeliveryDetails(ByVal JobNumber As String, _
    ByVal Deliveries As Delivery.Deliveries)
    ...
    Deliveries = PopulateDeliveryDetails(cmdDDSelect, cmdPallSelect)
End Sub

When you pass an argument by value, the caller's instance of that variable will not be updated because the function is essentially creating it's own instance of the argument variable initialized with the value that is passed into the method.

Here's a better way to do this that makes it work...

Public Sub GetDeliveryDetails(ByVal JobNumber As String, _
    ByRef Deliveries As Delivery.Deliveries)
    ...
    Deliveries = PopulateDeliveryDetails(cmdDDSelect, cmdPallSelect)
End Sub

However! Here is how this should be done to avoid this kind of problem all together (note the additions/changes in bold):

Public Function GetDeliveryDetails(ByVal JobNumber As String) [b]As Delivery.Deliveries[b]
    ...
    [b]Return PopulateDeliveryDetails(cmdDDSelect, cmdPallSelect)[b]
End Sub

Peter
------------------------------------------------------
Work smarter, not harder.
Reply With Quote
  #3 (permalink)  
Old March 1st, 2004, 12:20 PM
Friend of Wrox
 
Join Date: Jul 2003
Location: York, , United Kingdom.
Posts: 142
Thanks: 0
Thanked 2 Times in 2 Posts
Default

I mentioned at the end of my last post that I had tried passing in the argument both ByVal and ByRef - I'm aware of the differences, but even passing in ByRef doesn't seem to modify the original variable.

Like yourself I'm not a big fan of passing in parameters for them to be modified, however in this case I could see of no other way of achieving what I wanted. The 'GetDeliveryDetails' method of the Deliveries collection object populates the Deliveries collection object with individual Delivery objects. Therefore, a function will not work as the following assignment is invalid;

me = odbops.GetDeliveryDetails(JobNumber, me)

In a nutshell, what I am trying to do is use a method of a collection object to populate the same collection object.

Reply With Quote
  #4 (permalink)  
Old March 1st, 2004, 12:55 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

My apologies for not seeing that last line. I must have skipped over the mention of both attempts.

What is "me" in this case?

If you want a method of an object to populate something within itself, why do you even need to pass that object instance to the method on it? Can't it just do what it needs to on its internal collection member?

odbops.GetDeliveryDetails(JobNumber)

Otherwise, I can't see why this wouldn't work:

odbops = odbops.GetDeliveryDetails(JobNumber, odbops)

Peter
------------------------------------------------------
Work smarter, not harder.
Reply With Quote
Reply


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
Parameter Collection dcct84 C# 1 September 28th, 2007 10:52 AM
Can anybody help? Collection in C# naveenj .NET Framework 2.0 4 July 15th, 2007 09:34 PM
Collection in C# ? dedex C# 1 January 14th, 2005 04:51 PM
COM Collection ben0027 Visual C++ 0 May 1st, 2004 05:19 AM
Help collection gone MikeB VS.NET 2002/2003 2 June 12th, 2003 02:33 AM



All times are GMT -4. The time now is 10:12 AM.


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