what is wrong in this code? - Programmers Heaven

Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

what is wrong in this code?

Al_Mo3tasmAl_Mo3tasm Posts: 5Member
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

Comments

  • rlcrlc Posts: 448Member
    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:


    [code]
    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
    Else
    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
    [/code]

    and in main
    [code]
    Dim Newcustomer As New Customer

    Newcustomer.AddOrder(Now, 234)
    Newcustomer.AddOrder(Now, 433)
    [/code]
  • Al_Mo3tasmAl_Mo3tasm Posts: 5Member
    Thank you My friend
Sign In or Register to comment.