How to Initialize an object with an instance of the custom object

Hobbyist_programmer 621 Reputation points
2020-12-21T20:51:40.25+00:00

Hallo,

I have a class with Customer and orders and how do i place a dummy order when i initialize customer so that the customer already has 1 order?

Public Class Customer

Public Sub New()
 ordersValue = New Orders()
End Sub


Private WithEvents ordersValue As Orders

Public Property Orders() As Orders
        Get
            Return ordersValue
        End Get
        Set(ByVal value As Orders)
            ordersValue = value
        End Set
End Property

End Class

Public Class Order

    Public Sub New()
    End Sub

    Private orderIDValue As String
    Public ReadOnly Property OrderID() As String
        Get
            Return orderIDValue
        End Get
    End Property

End class

Thanks

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,737 questions
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,436 Reputation points
    2020-12-21T21:08:03.703+00:00

    Hello @Hobbyist_programmer

    See if this is helpful, there are some additional properties that may or may not be in your classes. Note Customeridentifier and OrderId would both be -1 until the primary key is returned from inserting a customer, same with order. If this was Entity Framework both keys would be handled by entity framework 6 or entity framework core.

    Public Class Customer  
        Public Property Id() As Integer  
        Public Sub New()  
            Orders = New List(Of Orders)  
      
            Orders.Add(New Orders() With {  
                .OrderId = -1,  
                .CustomerIdentifier = -1,  
                .OrderDate = Now})  
      
        End Sub  
        Public Property CompanyName() As String  
        Public Property ContactId() As Integer  
        Public Property CountryIdentifier() As Integer  
        Public Property Orders() As List(Of Orders)  
    End Class  
      
    Public Class Orders  
        Public Property OrderId() As Integer  
        Public Property OrderDate() As Date  
        Public Property CustomerIdentifier() As Integer  
        Public Property ShipAddress() As String  
        Public Property ShipCity() As String  
        Public Property ShipPostalCode() As String  
        Public Property ShipCountryIdentifier() As Integer  
    End Class  
    Public Class Country  
        Public Property CountryIdentifier() As Integer  
        Public Property Name() As String  
    End Class  
      
      
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Hobbyist_programmer 621 Reputation points
    2020-12-23T07:48:01.973+00:00

    Thanks Karen it helped.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.