Results 1 to 4 of 4

Thread: Adding Array Values

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2010
    Posts
    25

    Adding Array Values

    Hello I am trying to add the Sales Revenue for each of the three rows and populate them in their own text box and then get a sum of the three text boxes. Any help would be very much appreciated! For Example:

    1 2 3 4 5 (15)-text box

    1 2 3 4 5 (15)-text box

    2 4 4 3 5 (18)-text box
    -----------------------
    (48)-text box



    Code:
    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
            Dim ItemCost() As Decimal = {12D, 17.95D, 95D, 86.5D, 78D}
    
            Dim SoldItems(,) As Integer = New Integer(,) {{txtS1I1.Text, txtS1I2.Text, txtS1I3.Text, txtS1I4.Text, txtS1I5.Text}, _
                                    {txtS2I1.Text, txtS2I2.Text, txtS2I3.Text, txtS2I4.Text, txtS2I5.Text}, _
                                    {txtS3I1.Text, txtS3I2.Text, txtS3I3.Text, txtS3I4.Text, txtS3I5.Text}}
    
    
            Dim totals(,) As Decimal = Calculate(SoldItems, ItemCost)
            
            
        End Sub
        Private Function Calculate(ByVal SoldItems(,) As Integer, ByVal itemCost() As Decimal) As Decimal(,)
    
    
            Dim SalesRevenue(SoldItems.GetUpperBound(0), SoldItems.GetUpperBound(1)) As Decimal
            Dim strItemsSold As String = ""
            Dim sum As String = ""
    
            For r As Integer = 0 To SoldItems.GetUpperBound(0)
                For c As Integer = 0 To SoldItems.GetUpperBound(1)
                    SalesRevenue(r, c) = CDec(SoldItems(r, c) * itemCost(c))
                    strItemsSold &= FormatCurrency(SalesRevenue(r, c), 2) & vbTab
                Next
                strItemsSold &= vbCrLf & vbCrLf
            Next
    
            MessageBox.Show(strItemsSold, "Daily Sales Revenue")
    
            Return SalesRevenue
        End Function

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    Re: Adding Array Values

    Some general ideas:

    Code:
        Dim r As New Random
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    
            Dim foo As New List(Of someValues) 'maybe a dictionary would be a better choice???
            foo.Add(New someValues(TextBox1))
            foo.Add(New someValues(TextBox2))
    
            For Each sv In foo 'look at all the elements of foo
                'add some random values as test data
                Dim x As Integer = r.Next(2, 6)
                For z As Integer = 0 To x
                    sv.AddValue = CDec(r.NextDouble * 100)
                Next
                sv.ShowTotal()
            Next
    
        End Sub
    
        Private Class someValues
            Private TheTextBox As TextBox
            Private TheValues As New List(Of Decimal)
    
            Public Sub New(ByVal TxtBox As TextBox)
                Me.SetTextBox = TxtBox
            End Sub
    
            WriteOnly Property SetTextBox As TextBox
                Set(ByVal value As TextBox)
                    Me.TheTextBox = value
                End Set
            End Property
    
            WriteOnly Property AddValue As Decimal
                Set(ByVal value As Decimal)
                    Me.TheValues.Add(value)
                End Set
            End Property
    
            Public Sub ShowTotal()
                Me.TheTextBox.Text = Me.TheValues.Sum.ToString
            End Sub
    
        End Class
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    Re: Adding Array Values

    Using Dictionary
    Code:
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim foo As New Dictionary(Of String, someValues)
    
            foo.Add("Sales", New someValues(TextBox1))
            foo.Add("Costs", New someValues(TextBox2))
    
            foo("Sales").AddValue = 12.1D
            foo("Sales").AddValue = 3.22D
            foo("Sales").AddValue = 4.77D
            foo("Sales").AddValue = 9.15D
    
            foo("Costs").AddValue = 7.12D
            foo("Costs").AddValue = 0.03D
            foo("Costs").AddValue = 2.98D
    
            For Each sv In foo 'look at all the elements of foo
                sv.Value.ShowTotal()
            Next
    
        End Sub
    
        Private Class someValues
            Private TheTextBox As TextBox
            Private TheValues As New List(Of Decimal)
    
            Public Sub New(ByVal TxtBox As TextBox)
                Me.SetTextBox = TxtBox
            End Sub
    
            WriteOnly Property SetTextBox As TextBox
                Set(ByVal value As TextBox)
                    Me.TheTextBox = value
                End Set
            End Property
    
            WriteOnly Property AddValue As Decimal
                Set(ByVal value As Decimal)
                    Me.TheValues.Add(value)
                End Set
            End Property
    
            Public Sub ShowTotal()
                Me.TheTextBox.Text = Me.TheValues.Sum.ToString
            End Sub
    
        End Class
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Apr 2010
    Posts
    25

    Re: Adding Array Values

    Thanks, I think I have it resolved...I was wondering if I enter 2 days in a text box and then enter my values, I would like to get the totals and grand total for each day and display them in their appropriate text box.Then take the grand totals for each day and populate them in a message box. Each time I click a button to setup for the next day, everything is cleared but the previous value is stored.When the entered amount of days is reached, I would like it to show in the format below. I believe I would store them in a one dimensional array but not quite sure. What do you think?

    ex.
    Day Sales
    1 $500.45
    2 $975.48

    Code:
    For r As Integer = 0 To SoldItems.GetUpperBound(0)
                    For c As Integer = 0 To SoldItems.GetUpperBound(1)
                        SalesRevenue(r, c) = CDec(SoldItems(r, c) * itemCost(c))
                        strItemsSold &= FormatCurrency(SalesRevenue(r, c), 2) & vbTab
                        totals(r) += SalesRevenue(r, c)
                    Next
                    strItemsSold &= vbCrLf & vbCrLf
                    txtTotalStoreSales1.Text = FormatCurrency(totals(r).ToString, 2)
                    txtTotalStoreSales2.Text = FormatCurrency(totals(r).ToString, 2)
                    txtTotalStoreSales3.Text = FormatCurrency(totals(r).ToString, 2)
                Next
    
     For Each total As Decimal In totals
                    sum += total
                Next
    
                txtDailyTotal.Text = FormatCurrency(sum, 2)
    
            MessageBox.Show("Day" & vbTab & "Sales" & vbCrlf & vbTab &
                            sum, "Future Value Calculations, Daily Sales Totals")

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width