Link to home
Start Free TrialLog in
Avatar of Ron Kidd
Ron KiddFlag for Australia

asked on

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

Avatar of Karrtik Iyer
Karrtik Iyer
Flag of India image

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

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.
ASKER CERTIFIED SOLUTION
Avatar of Karrtik Iyer
Karrtik Iyer
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of it_saige
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 -User generated imageThere are pro's and con's to each method.

-saige-