Refresh Data Collection Class VB.Net 2015

I Have the Following classes.
The first one is an Order Class and the Second one is a Collection of Orders.

I declare an Instance of this Collection Class.
Dim orderCollection = New ProductionOrderCollection

That works Fine - But I need to Refresh the Collection to get Updated Data from the Database.

Is there a Way to Refresh the Current Instance of the Collection Class or do I have to Remove the Instance and Create a New one?

orderCollection = nothing
orderCollection = New ProductionOrderCollection

Does Removing the Instance and Creating a New Instance waste Memory?

Order Class
Public Class ProductionOrder
    Public Property SalesOrderID As Integer
    Public Property CustomerName As String
    Public Property DespatchMethod As String
End Class

Open in new window


Order Collection Class
Public Class ProductionOrderCollection

    Inherits List(Of ProductionOrder)

    Public Sub New()
        'Declare the Handled Exception Collection
        Dim handledException = CustomErrorCollection.GetCustomErrorCollection

        'Declare Data Reader
        Dim drd As DbDataReader

		'Get Data from Data DLL
		drd = ProductionData.Orders

		While drd.Read
			Dim order As New ProductionOrder
			order.SalesOrderID = drd.GetInt32(0)
			order.CustomerName = drd.GetString(1)
			order.DespatchMethod = drd.GetString(2)
			Me.Add(order)
		End While

    End Sub
End Class

Open in new window

p-platerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Karrtik IyerSoftware ArchitectCommented:
How is the below code getting the date from DB? I am assuming it is ADO.NET? Is that correct?
drd = ProductionData.Orders

Open in new window

Karrtik IyerSoftware ArchitectCommented:
You can do orderCollection.Clear() and then add new items to it on refresh.  The way you are filling the data in a for loop as shown in the question above, I can't think of any other way to do it.
Karrtik IyerSoftware ArchitectCommented:
BTW, it is not a waste of memory since you are removing them from the list so you are marking them for garbage collection (assuming there are no more references to the object that are being removed from the list) and then adding new objects to it.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
it_saigeDeveloperCommented:
There are basically two ways to handle this.  You can clear the collection and add new items to it; e.g. -
Module Module1
	Private data As New Orders(From i As Integer In Enumerable.Range(0, 50) Select New Order() With {.ID = i, .CustomerName = String.Format("Customer{0}", i), .DispatchMethod = If(i Mod 3 = 0, "Plane", If(i Mod 2 = 0, "Train", "Automobile"))})

	Sub Main()
		Dim customerOrders As New Orders(data)
		Console.WriteLine("Before update")
		Console.WriteLine(customerOrders(25))

		For Each [order] In data
			If [order].DispatchMethod = "Plane" Then
				[order].DispatchMethod = "Boeing 747"
			ElseIf [order].DispatchMethod = "Train" Then
				[order].DispatchMethod = "B&O"
			Else
				[order].DispatchMethod = "Mom Drove You"
			End If
		Next
		customerOrders.Clear()
		customerOrders.AddRange(data)

		Console.WriteLine("After update")
		Console.WriteLine(customerOrders(25))
		Console.ReadLine()
	End Sub
End Module

Class Order
	Public Property ID() As Integer
	Public Property CustomerName() As String
	Public Property DispatchMethod() As String

	Public Overrides Function ToString() As String
		Return String.Format("{{ ID: {0}; Customer Name: {1}; Dispatch Method: {2} }}", ID, CustomerName, DispatchMethod)
	End Function
End Class

Class Orders
	Inherits List(Of Order)

	Public Sub New()
		MyBase.New()
	End Sub

	Public Sub New(capacity As Integer)
		MyBase.New(capacity)
	End Sub

	Public Sub New(collection As IEnumerable(Of Order))
		MyBase.New(collection)
	End Sub
End Class

Open in new window

Or you can check if the item exists and update it's individual field(s); e.g. -
Module Module1
	Private data As New Orders(From i As Integer In Enumerable.Range(0, 50) Select New Order() With {.ID = i, .CustomerName = String.Format("Customer{0}", i), .DispatchMethod = If(i Mod 3 = 0, "Plane", If(i Mod 2 = 0, "Train", "Automobile"))})

	Sub Main()
		Dim customerOrders As New Orders(data)
		Console.WriteLine("Before update")
		Console.WriteLine(customerOrders(25))

		For Each [order] In data
			If [order].DispatchMethod = "Plane" Then
				[order].DispatchMethod = "Boeing 747"
			ElseIf [order].DispatchMethod = "Train" Then
				[order].DispatchMethod = "B&O"
			Else
				[order].DispatchMethod = "Mom Drove You"
			End If
		Next

		For Each [order] In data
			Dim current = customerOrders.FirstOrDefault(Function(x) x.ID = [order].ID)
			If current IsNot Nothing Then
				current.CustomerName = [order].CustomerName
				current.DispatchMethod = [order].DispatchMethod
			Else
				customerOrders.Add([order])
			End If
		Next

		Console.WriteLine("After update")
		Console.WriteLine(customerOrders(25))
		Console.ReadLine()
	End Sub
End Module

Class Order
	Public Property ID() As Integer
	Public Property CustomerName() As String
	Public Property DispatchMethod() As String

	Public Overrides Function ToString() As String
		Return String.Format("{{ ID: {0}; Customer Name: {1}; Dispatch Method: {2} }}", ID, CustomerName, DispatchMethod)
	End Function
End Class

Class Orders
	Inherits List(Of Order)

	Public Sub New()
		MyBase.New()
	End Sub

	Public Sub New(capacity As Integer)
		MyBase.New(capacity)
	End Sub

	Public Sub New(collection As IEnumerable(Of Order))
		MyBase.New(collection)
	End Sub
End Class

Open in new window

Each of the above produce the following output -Capture.JPGThere are pro's and con's to each method.

-saige-
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.