what is wrong in this code?

Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization
Imports System.Xml.Serialization
Imports serializations.Customer
Imports System.IO
Module Module1

Sub Main()

Dim st As FileStream = File.Open("C: est.xml", FileMode.OpenOrCreate)
Dim SerialObj As New XmlSerializer(GetType(Customer))
Dim Newcustomer As New Customer

Newcustomer.Name = "xxxx"
Newcustomer.Address = "yyyy"

Newcustomer.Orders(0).OrderID = "1111" '
Newcustomer.Orders(0).Date = Now 'some thing wrong Here
Newcustomer.Orders(0).Total = 8978 '

End Sub

End Module

Public Class Order
Public OrderID As String
Public [Date] As Date
Public Total As Decimal
End Class
Public Class Customer

Public Name As String
Public Address As String
Public orders(3) As Order

End Class


  • You have no reference I am guessing... You need to initiate instances of your order class for you array... delcaring your structure for orders makes the array, however there are no references for the objects in it.

    Public orders(3) As Order

    orders(0) = nothing
    orders(1) = nothing
    orders(2) = nohting
    orders(3) = nothing

    Add a constructor to initiate the instances in the class...

    I am not sure why you would want to have a static delacration for the order array size in the customer object. Instead I would add a function in Customer class for adding orders and declare the orders array:

    Public orders() As Order

    Public Function AddOrder(ByVal v_Date As Date, ByVal v_Total As Decimal) As Boolean
    Dim l_CurrOrder As New Order

    If Me.orders Is Nothing Then
    ReDim Me.orders(0)
    orders(0) = l_CurrOrder
    ReDim Preserve orders(orders.Length)
    orders(orders.Length - 1) = l_CurrOrder
    End If

    l_CurrOrder.Date = v_Date
    l_CurrOrder.Total = v_Total
    l_CurrOrder.OrderID = 1 'some internally generated number here

    End Function

    and in main
    Dim Newcustomer As New Customer

    Newcustomer.AddOrder(Now, 234)
    Newcustomer.AddOrder(Now, 433)
  • Thank you My friend
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!