Trouble Getting Elements Into My Array Correctly
Hi All, I'm back with more novice questions.
In this case I'm trying to create a number analysis app. I'm required to declare an array and get the data for the array using ten text boxes. Why, because my instructor said so.
My problem is my event is returning zero. Therefore I believe my code is somehow flawed. Can somebody examine my code and let me know where I'm going wrong.
My approach is to create functions that calculate the required metrics. I have only tried to code one function to date. If I can get help making one function work, I will be able to do the others . Can someone pleas take a look at my code and help me identify where I'm off.
Thanks
Public Class Form1
Dim decScore0, decScore1, decScore2, decScore3, decScore4 As Decimal
Dim decScore5, decScore6, decScore7, decScore8, decScore9 As Decimal
Dim decObservationArray() As Decimal = {decScore0, decScore1, decScore2, decScore3, _
decScore4, decScore5, decScore6, decScore7, decScore8, decScore9}
Private Sub btnCalculateStatistics_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculateStatistics.Click
lblMin.Text = Minimum().ToString
lblMax.Text = Maximum().ToString
lblSum.Text = Sum().ToString
lblAverage.Text = Average().ToString
End Sub
Public Function Minimum()
decScore0 = CDec(txtScore0.Text)
decScore1 = CDec(txtScore1.Text)
decScore2 = CDec(txtScore2.Text)
decScore3 = CDec(txtScore3.Text)
decScore4 = CDec(txtScore4.Text)
decScore5 = CDec(txtScore5.Text)
decScore6 = CDec(txtScore6.Text)
decScore7 = CDec(txtScore7.Text)
decScore8 = CDec(txtScore8.Text)
decScore9 = CDec(txtScore9.Text)
Dim intCount As Integer = 1
Dim decMinimum As Decimal = decObservationArray(0)
For intCount = 1 To decObservationArray.Length - 1
If decObservationArray(intCount) < decMinimum Then
decMinimum = decObservationArray(intCount)
End If
Next intCount
Return decMinimum
End Function
Public Function Maximum() As Decimal
End Function
Public Function Sum() As Decimal
End Function
Public Function Average() As Decimal
End Function
Public Function ClearBoxes() As Decimal
txtScore0.Clear()
txtScore1.Clear()
txtScore2.Clear()
txtScore3.Clear()
txtScore4.Clear()
txtScore5.Clear()
txtScore6.Clear()
txtScore7.Clear()
txtScore8.Clear()
txtScore9.Clear()
End Function
Private Sub btnClearAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearAll.Click
ClearBoxes()
End Sub
End Class
Re: Trouble Getting Elements Into My Array Correctly
Please use the buttons provided to wrap your code snippets in CODE or VBCode tags.
Re: Trouble Getting Elements Into My Array Correctly
Ok first off, in your Minimum function you never update the values in your Array.
This part of the code is useless
Code:
Dim decScore0, decScore1, decScore2, decScore3, decScore4 As Decimal
Dim decScore5, decScore6, decScore7, decScore8, decScore9 As Decimal
Dim decObservationArray() As Decimal = {decScore0, decScore1, decScore2, decScore3, _
decScore4, decScore5, decScore6, decScore7, decScore8, decScore9}
You just declared the decScore variables so they will all be 0. When you put all those variables in the array, it's not permanently making it so that when you update a Score variable it also updates the Array, no, you have to update the Array every time.
I suggest an UpdateArray Procedure
Code:
Private Sub UpdateArray()
decObservationArray(0) = CDec(txtScore0.Text)
decObservationArray(1) = CDec(txtScore1.Text)
'etc. for each textbox
End Sub
Then, before you use a math function, such as minimum, call your UpdateArray function and the numbers will update.
As for you minimum function, if you have .NET 3.5 I believe there is a .Min value that will return the minimum value, so only one line of code is needed to find the .Min.
If you don't have a .Min value on your decObservationArray then you can still use Array.Sort() to sort it and then decObservationArray(0) will be the lowest value.
Re: Trouble Getting Elements Into My Array Correctly
What you are doing by declaring all the variables, then adding them to the array would actually work if Decimal were a reference type, but since it is a value type, it won't work, as the value of the variable is copied into the array rather than the reference to the variable.
CDec is a risky way to convert the text into a number, too, since it will throw an exception if the textbox is either empty, or contains something that cannot be converted completely into a decimal value. Decimal.TryParse is safe, as it will never throw an exception in this case, but you code would change a bit:
Code:
If not Decimal.TryParse(textbox1.Text,decObservationArray(0)) Then
'It wasn't a number
End If
Though you don't really need the If statement if you don't actually want to do anything if the value was not a valid decimal.