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

February 27th, 2004, 08:03 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2003
Posts: 142
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
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
|
|

February 27th, 2004, 12:04 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
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.
|
|

March 1st, 2004, 12:20 PM
|
|
Friend of Wrox
|
|
Join Date: Jul 2003
Posts: 142
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
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.
|
|

March 1st, 2004, 12:55 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
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.
|
|
 |