Welcome to Dream.In.Code
Become a VB Expert!

Join 149,478 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,504 people online right now. Registration is fast and FREE... Join Now!




Please help with array

 
Reply to this topicStart new topic

Please help with array

km22
2 May, 2007 - 08:55 PM
Post #1

New D.I.C Head
*

Joined: 11 Mar, 2007
Posts: 32


My Contributions
Array help

I need to allow the user to enter the current bonus rate, display each salespersons number, total sales amount, total bonus amount, and total bonus paid to all salespeople.

I cannot figure out how to access the array to add the total sales amount (I realize my code is not correct in the For Each statement…this is where I am stuck). Thanks for any help.

CODE

Option Explicit On
Option Strict On


Public Class MainForm

    Structure Item
        Public salesPerson As String
        Public January As Integer
        Public February As Integer
        Public March As Integer

    End Structure



    Private Sub calcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles calcButton.Click

        Dim searchFor As String

        Dim bonus As Integer
        Dim isconverted As Boolean
        Dim sum1 As Integer

        Dim sales(9) As Item

        sales(0).salesPerson = "1"
        sales(0).January = 2400
        sales(0).February = 3500
        sales(0).March = 2000

        sales(1).salesPerson = "2"
        sales(1).January = 1500
        sales(1).February = 7000
        sales(1).March = 1000

        sales(2).salesPerson = "3"
        sales(2).January = 600
        sales(2).February = 450
        sales(2).March = 2100


        sales(3).salesPerson = "4"
        sales(3).January = 790
        sales(3).February = 240
        sales(3).March = 500

        sales(4).salesPerson = "5"
        sales(4).January = 1000
        sales(4).February = 1000
        sales(4).March = 1000


        sales(5).salesPerson = "6"
        sales(5).January = 6300
        sales(5).February = 7000
        sales(5).March = 8000

        sales(6).salesPerson = "7"
        sales(6).January = 1300
        sales(6).February = 450
        sales(6).March = 700

        sales(7).salesPerson = "8"
        sales(7).January = 2700
        sales(7).February = 5500
        sales(7).March = 6000

        sales(8).salesPerson = "9"
        sales(8).January = 4700
        sales(8).February = 4800
        sales(8).March = 4900

        sales(9).salesPerson = "10"
        sales(9).January = 1200
        sales(9).February = 1300
        sales(9).March = 1400

        RateTextBox.Text = bonus.ToString

        isconverted = Integer.TryParse(RateTextBox.Text, bonus)


        For subscript As Integer = 0 To 9
            salesPersonListBox.Items.Add(sales(subscript).salesPerson)
        Next subscript

        For Each totalSalesAmount As Integer In sales(January, February, March)
            sum1 = sum1 + totalSalesAmount
        Next totalSalesAmount
        totalSalesLabel.Text = sum1.ToString
    End Sub

End Class

User is offlineProfile CardPM
+Quote Post

Jayman
RE: Please Help With Array
2 May, 2007 - 10:19 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,302



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
The problem is that you are creating an array of your structure Item. Because this is an object data type, you cannot use primitive data types to access it in your For/Each loop.

You must use the structure as the data type. Replace Integer with Item.
CODE

For Each totalSalesAmount As Item In sales


In addition, because this is a structure you need to follow the rules for accessing the members of the structure. Meaning you need to access them as structureName.MemberName.
Since you are merely adding the totals together, you will need to include all the Members in your formula.
CODE

sum1 = sum1 + totalSalesAmount.January + totalSalesAmount.February + totalSalesAmount.March

User is offlineProfile CardPM
+Quote Post

km22
RE: Please Help With Array
3 May, 2007 - 03:05 PM
Post #3

New D.I.C Head
*

Joined: 11 Mar, 2007
Posts: 32


My Contributions
Thank you – I have just started to comprehend simple arrays - 1 dimensional, and thought I would try to combine a structure with an array for this project. I am not sure if this was the best choice. Is it possible to add January, February and March, for each individual Salesperson? (These would be put in a large label)
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Please Help With Array
3 May, 2007 - 04:30 PM
Post #4

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,302



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Absolutely. You just need to concatenate the individual results to the label inside the loop. You will need to use two variables inside the For/Each loop. One that will contain the current calculation for the current salesperson and another to keep a running total of all the values calculated.

Example:
CODE

        Dim total As Integer

        For Each totalSalesAmount As Item In sales
            total = totalSalesAmount.January + totalSalesAmount.February + totalSalesAmount.March
            sum1 = sum1 + total
            Me.Label2.Text = Me.Label2.Text & total & ControlChars.NewLine
        Next totalSalesAmount

        Me.Label1.Text = sum1.ToString

User is offlineProfile CardPM
+Quote Post

km22
RE: Please Help With Array
3 May, 2007 - 08:21 PM
Post #5

New D.I.C Head
*

Joined: 11 Mar, 2007
Posts: 32


My Contributions
Thx a ton!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 04:00PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month