Two arrays. Can someone help. I'm stuck

red

New member
Joined
Jun 22, 2004
Messages
3
Programming Experience
Beginner
Hello. I am a newbie. I am taking VB.net for the first time. I have to admit, it is a little scary. I am working on an assignment that deals with feeding a string into two arrays. I am going to post the synopsis of this thing and my code i have thus far.... WARNING -- it's a little crappy and that is because i am not very comfortable with arrays just yet. Any ideas here would be greatly appreciated.
********************************************
Synopsis:
Write a function that accepts two arrays and a String. The first array, dtePurchaseDates, should be an array of dates. The second array, decSaleAmount, should be an array of Decimals. Each array should contain 100 items. The String strMonth should represent the month whose total sales will be returned from the function. The function should loop through the arrays and total the sales of the month indicated and return the total.


_______________________________________________
my code thus far....

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Declare arrays and variables
Dim dtePurchaseDates(99) As Date
Dim decSaleAmount(99) As Decimal
Dim strMonth As String, strSearch As String
Dim intStep As Integer, intTotal As Integer

'Initialize variables that need to be intialized
intTotal = 0
intStep = 0


For intStep = 0 To 99
If (strSearch = dtePurchaseDates(intStep)) Then
intTotal += (strMonth)
End If


Next intStep


************************************************

I know i have some errors here.



--Red
 
First of all, you'll get in trouble with the string that represents a month i suppose. How is the month in the string represented? (e.g. January or so).

But before I can help you, you'd better give some more information about this function (what should it look like, what do the arrays look like etc.).

Greetz
 
~Greetings~

Greetings!

We pretty much have 'leeway' here. That synopsis i posted (see again below) is pretty much the only instructions our instructor gave us..... so we put whatever values we want into the arrays.... hope this helps.... Thanks for replying so soon...


Regards
Red

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Synopsis:
Write a function that accepts two arrays and a String. The first array, dtePurchaseDates, should be an array of dates. The second array, decSaleAmount, should be an array of Decimals. Each array should contain 100 items. The String strMonth should represent the month whose total sales will be returned from the function. The function should loop through the arrays and total the sales of the month indicated and return the total.

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 
I wrote a function, but you'll need a second one too. I think the code is quite clear:

VB.NET:
  Public Function TotalSales(ByVal dtePurchaseDates() As Date, ByVal decSaleAmount() As Decimal, _
 	ByVal strMonth As String) As Decimal
 		Dim i As Integer
 		Dim MonthNumber As Byte = GetMonthNumber(strMonth)
 		Dim tempTotalSales As Decimal
 
 		For i = 0 To 99
 			If dtePurchaseDates(i).Month = MonthNumber Then
 				tempTotalSales += decSaleAmount(i)
 			End If
 		Next
 
 		Return tempTotalSales
 	End Function
 
 	Private Function GetMonthNumber(ByVal sMonth As String) As Byte
 		Select Case sMonth
 			Case "January"
 				Return 1
 			Case "February"
 				Return 2
 			Case "March"
 				Return 3
 			Case "April"
 				Return 4
 			Case "May"
 				Return 5
 			Case "June"
 				Return 6
 			Case "July"
 				Return 7
 			Case "August"
 				Return 8
 			Case "September"
 				Return 9
 			Case "October"
 				Return 10
 			Case "November"
 				Return 11
 			Case "December"
 				Return 12
 		End Select
 
 	End Function


I didn't test it, but i suggest it works...


Greetz
 
my code and yours combined...

Here is the whole shabang....
*********************************

Public
Class Form1
Inherits System.Windows.Forms.Form

'declare two arrays

Const intNumOfEntries = 99

Dim dtePurchaseDates(intNumOfEntries) As Date

Dim decSaleAmount(intNumOfEntries) As Decimal

#Region " Windows Form Designer generated code "

'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

Friend WithEvents txtMonth As System.Windows.Forms.TextBox

Friend WithEvents Label1 As System.Windows.Forms.Label

Friend WithEvents Label2 As System.Windows.Forms.Label

Friend WithEvents btnClickMeFirst As System.Windows.Forms.Button

Friend WithEvents btnCalculateSales As System.Windows.Forms.Button

Friend WithEvents TextBox1 As System.Windows.Forms.TextBox

Friend WithEvents txtData As System.Windows.Forms.TextBox

Friend WithEvents txtTotal As System.Windows.Forms.TextBox

Friend WithEvents Label3 As System.Windows.Forms.Label

<System.Diagnostics.DebuggerStepThrough()>
Private Sub InitializeComponent()

Me.txtMonth = New System.Windows.Forms.TextBox

Me.Label1 = New System.Windows.Forms.Label

Me.Label2 = New System.Windows.Forms.Label

Me.btnClickMeFirst = New System.Windows.Forms.Button

Me.btnCalculateSales = New System.Windows.Forms.Button

Me.TextBox1 = New System.Windows.Forms.TextBox

Me.txtData = New System.Windows.Forms.TextBox

Me.txtTotal = New System.Windows.Forms.TextBox

Me.Label3 = New System.Windows.Forms.Label

Me.SuspendLayout()

'

'txtMonth

'

Me.txtMonth.Location = New System.Drawing.Point(184, 144)

Me.txtMonth.Name = "txtMonth"

Me.txtMonth.Size = New System.Drawing.Size(104, 20)

Me.txtMonth.TabIndex = 1

Me.txtMonth.Text = ""

'

'Label1

'

Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

Me.Label1.Location = New System.Drawing.Point(192, 112)

Me.Label1.Name = "Label1"

Me.Label1.Size = New System.Drawing.Size(96, 24)

Me.Label1.TabIndex = 2

Me.Label1.Text = "Enter a month:"

'

'Label2

'

Me.Label2.Font = New System.Drawing.Font("Arial", 11.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

Me.Label2.Location = New System.Drawing.Point(24, 8)

Me.Label2.Name = "Label2"

Me.Label2.Size = New System.Drawing.Size(248, 24)

Me.Label2.TabIndex = 3

Me.Label2.Text = "Monthly Sales Calculator"

Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter

'

'btnClickMeFirst

'

Me.btnClickMeFirst.Location = New System.Drawing.Point(224, 56)

Me.btnClickMeFirst.Name = "btnClickMeFirst"

Me.btnClickMeFirst.Size = New System.Drawing.Size(64, 32)

Me.btnClickMeFirst.TabIndex = 4

Me.btnClickMeFirst.Text = "Click Me First"

'

'btnCalculateSales

'

Me.btnCalculateSales.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

Me.btnCalculateSales.Location = New System.Drawing.Point(16, 224)

Me.btnCalculateSales.Name = "btnCalculateSales"

Me.btnCalculateSales.Size = New System.Drawing.Size(168, 40)

Me.btnCalculateSales.TabIndex = 5

Me.btnCalculateSales.Text = "Click me to calculate total sales for this month!"

'

'TextBox1

'

Me.TextBox1.Location = New System.Drawing.Point(144, 40)

Me.TextBox1.Multiline = True

Me.TextBox1.Name = "TextBox1"

Me.TextBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical

Me.TextBox1.Size = New System.Drawing.Size(24, 168)

Me.TextBox1.TabIndex = 7

Me.TextBox1.Text = ""

'

'txtData

'

Me.txtData.Location = New System.Drawing.Point(16, 40)

Me.txtData.Multiline = True

Me.txtData.Name = "txtData"

Me.txtData.Size = New System.Drawing.Size(120, 168)

Me.txtData.TabIndex = 8

Me.txtData.Text = ""

'

'txtTotal

'

Me.txtTotal.Location = New System.Drawing.Point(200, 240)

Me.txtTotal.Name = "txtTotal"

Me.txtTotal.Size = New System.Drawing.Size(88, 20)

Me.txtTotal.TabIndex = 9

Me.txtTotal.Text = ""

'

'Label3

'

Me.Label3.Location = New System.Drawing.Point(216, 208)

Me.Label3.Name = "Label3"

Me.Label3.Size = New System.Drawing.Size(64, 24)

Me.Label3.TabIndex = 10

Me.Label3.Text = "Total $:"

'

'Form1

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(292, 273)

Me.Controls.Add(Me.Label3)

Me.Controls.Add(Me.txtTotal)

Me.Controls.Add(Me.txtData)

Me.Controls.Add(Me.TextBox1)

Me.Controls.Add(Me.btnCalculateSales)

Me.Controls.Add(Me.btnClickMeFirst)

Me.Controls.Add(Me.Label2)

Me.Controls.Add(Me.Label1)

Me.Controls.Add(Me.txtMonth)

Me.Name = "Form1"

Me.Text = "Form1"

Me.ResumeLayout(False)

End Sub

#End Region

Public Function TotalSales(ByVal dtePurchaseDates() As Date, ByVal decSaleAmount() As Decimal, ByVal strMonth As String) As Decimal

'declare variables

Dim i As Integer

Dim MonthNumber As Byte = GetMonthNumber(strMonth)

Dim tempTotalSales As Decimal

For i = 0 To UBound(dtePurchaseDates)

If dtePurchaseDates(i).Month = MonthNumber Then

tempTotalSales += decSaleAmount(i)

End If

Next

Return tempTotalSales

End Function

Private Function GetMonthNumber(ByVal sMonth As String) As Byte

Select Case sMonth

Case "January"

Return 1

Case "February"

Return 2

Case "March"

Return 3

Case "April"

Return 4

Case "May"

Return 5

Case "June"

Return 6

Case "July"

Return 7

Case "August"

Return 8

Case "September"

Return 9

Case "October"

Return 10

Case "November"

Return 11

Case "December"

Return 12

End Select

End Function

Private Sub btnClickMeFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClickMeFirst.Click

'declare variables

Dim i As Integer

Dim intDay As Integer

Dim intMonth As Integer

Dim decSales As Decimal

For i = 0 To 99

intDay = Rnd() * 27 + i

intMonth = Rnd() * 12 + i

dtePurchaseDates(i) =
New Date(2003, intMonth, intDay)



decSales = Rnd() * 3000 + 2000

decSaleAmount(i) = decSales

txtData.Text = txtData.Text & i.ToString & "-" & dtePurchaseDates(i).ToShortDateString & " - " & decSaleAmount(i).ToString & vbNewLine

Next i

End Sub

Private Sub btnCalculateSales_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculateSales.Click

MsgBox(TotalSales(dtePurchaseDates, decSaleAmount, txtMonth.Text))

Dim strMonth As String = txtMonth.Text

GetMonthNumber(Convert.ToInt32(strMonth))

'TotalSales()

'txtMonth.Text = intTotal



End Sub

End
Class


***************************

I know there are still some things to take care of .... i am still a workin on it.
let me know what you see wrong here..


~Red~
 
Back
Top