Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27196

VS 2010 How do I update a structure

$
0
0
Code:

Hello everyone, I am new to vb.net. I am working on a project where I have to generate the details for a bill (i.e. qty, price, amount etc) first. Once I am done with that I generate the bill number. So i have 2 structures. Structure One holds the item wise details of the bill (bill number, item #, qty, price, item amount) while structure Two holds the bill summary (i.e. bill number, item count, bill amount). The nature of the requirement forces me to generate values for the items first (structure one except the bill number) and subsequently the summary information. As part of generating the bill summary (Structure Two), I generate the bill number and the count of items allocated to the bill. Then for that many items in the bill details (structure one), I update the Bill number.

For example, If there are 12 items in my first structure (item details), and while generating details for the second structure (summary), the first bill number is to be allocated to 5 items in the bill details, I update the first five rows in the first structure with the bill number. Then say, the next pass allocates 4 items to the next bill number, the in the bill details I update the second bill number to the next 4 items and so on ...

The problem is that the update of bill number in the bill detail structure is not happening. To demonstrate the problem, I have created a small project. There is a form with one button which in turn calls a class. Any help will be much appreciated.

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim cpy As New clsMain

        Call cpy.p_GenerateBillDetails(5)
        Call cpy.p_GenerateBillSummary(5)

        For Each item As clsMain.stru_BillDetails In clsMain.op_BillDetails
            MessageBox.Show("Bill No " & item.spBillNo) ' why is bill number zero here ??
        Next

    End Sub
End Class


Public Class clsMain
    Public Shared op_BillSummary As New List(Of stru_BillSummary)
    Public Shared op_BillDetails As New List(Of stru_BillDetails)

    Public Structure stru_BillSummary
        Public ipRowNum As Integer
        Public spBillNo As String
        Public ipItemCount As Integer
        Public dpNetBillAmount As Double
    End Structure

    Public Structure stru_BillDetails
        Public spBillNo As String
        Public ipItemNo As Integer
        Public ipQuantity As Single
        Public ipPrice As Double
        Public dpItemAmount As Double
    End Structure
    Protected Friend Sub p_GenerateBillDetails(ByVal Iterations As Integer)
        Dim i As Integer = 1
        Dim il_Quantity As Integer = 0
        Dim il_Price As Integer = 0

        For i = 1 To Iterations
            il_Quantity = f_intGetRandomValue(1, 10)
            il_Price = f_intGetRandomValue(1, 10)
            op_BillDetails.Add(New stru_BillDetails With {.spBillNo = CStr(0), .ipItemNo = i, .ipPrice = il_Price, .ipQuantity = il_Quantity, .dpItemAmount = il_Quantity * il_Price})
            i = i + 1
        Next

    End Sub

    Protected Friend Function f_intGetRandomValue(Optional ByVal Lowerbound As Integer = 1, Optional ByVal UpperBound As Integer = 10) As Integer
        Dim objR As New Random()

        Return objR.Next(Lowerbound, UpperBound + 1)

    End Function


    Protected Friend Sub p_GenerateBillSummary(ByVal Iterations As Integer)

        Dim j As Integer = 1

        For Each item As stru_BillDetails In op_BillDetails
            item.spBillNo = "Bill" & CStr(j)
            ' immediately after update
            MessageBox.Show("Immediately after update " & item.spBillNo) ' Bill number is updated here but then lost subsequently some where.
            op_BillSummary.Add(New stru_BillSummary With {.ipRowNum = j, .spBillNo = "Bill" & CStr(j), .ipItemCount = 5, .dpNetBillAmount = 1000 * j})
            j = j + 1
        Next

        ' for debugging
        For Each item As stru_BillDetails In op_BillDetails
            ' Bill # is not getting displayed
            MessageBox.Show("later " & item.spBillNo) ' why is bill number zero here ??
        Next

    End Sub
End Class


Viewing all articles
Browse latest Browse all 27196

Trending Articles