-
Nov 10th, 2017, 03:55 PM
#1
Thread Starter
New Member
[RESOLVED] Receiving Numerical Data from a list box to get an avg val
Hiya,
So currently im building an application that will get me the avg of numbers that is input by the user via a text box using Doubles and arrays i got most of it working but having a little trouble figuring out
as to why the return average code keeps giving errors
Code:
Private Sub btnCalculation_Click(sender As Object, e As EventArgs) _
Handles btnCalculation.Click
Dim i As Integer = 0
Dim intTopIndex As Integer = lstInput.Items.Count - 1
Dim dblListArray(intTopIndex) As Double
Dim dblAvg as Double
getValues(dblListArray)
Code:
Sub getValues(ByRef dblListArray() As Double)
Dim i As Integer = 0
Dim dblNumbers(i) As Double
For i = 0 To lstNumbers.Items.Count - 1
dblNumbers(i) = CDbl(lstNumbers.Items(i))
Next i
Code:
Private Function getAverage(ByVal dblArray As Array) As Double
Dim i As Integer = 0
Dim iSum As Integer = 0
For i = 0 To lstNumbers.Items.Count - 1
iTotal = iTotal + getValues(dblListArray:=) 'the error i have is here, the error is saying its expecting an Expression
Next i
Return iTotal / lstNumbers.Items.Count - 1
End Function
Thank you
-Shappy
-
Nov 10th, 2017, 04:21 PM
#2
Re: Receiving Numerical Data from a list box to get an avg val
See the weird bit ':=' at the end here?
Code:
iTotal = iTotal + getValues(dblListArray:=)
That's a very special operator VB only uses for a feature called "named parameters" that's fun, but you probably don't need to be using.
Visual Studio likes to "helpfully" add it when you're trying to type a variable name that looks like the parameter, but isn't defined in the local context. It assumes you must mean "I want to use a named parameter" and auto-completes the name while adding the broccoli operator (:=) to it.
But you aren't trying to make a named parameter. You're confused because you have a variable named dblListArray somewhere else, but you also made a parameter named dblListArray for the GetValues() function.
In this case, you probably meant to type 'dblArray', but it's smart to name things consistently. If it's 'dblListArray' in some places, it should be 'dblListArray' in all places.
Arguably, I think you should call your parameters "input", or in the case of the ByRef one "output". That way it's easier to keep track of them.
Some people like the type warts in front, but I figure by the time we have the variable "dblListArrayInput" we may as well call it "thisIsAContiguousBlockOfMemoryWhereEverySixtyFourBytesIsConsideredToBeADoubleAndCanBeIndexedByAnInt eger". Or "input" for short.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
-
Nov 10th, 2017, 04:25 PM
#3
Re: Receiving Numerical Data from a list box to get an avg val
This "(dblListArray:=)" isn't the correct syntax. (dblListArray)
But I really don't see why your calling getValues.
If all you want is the average of the items in the Listbox,
Code:
Public Class Form7
Private Sub Form7_Load(sender As Object, e As EventArgs) Handles Me.Load
For i As Integer = 0 To 9
Me.ListBox1.Items.Add(i.ToString)
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim listTotal As Decimal = 0
Dim listAvg As Decimal = 0
For i As Integer = 0 To Me.ListBox1.Items.Count - 1
listTotal = listTotal + CInt(Me.ListBox1.Items(i).ToString)
Next
listAvg = listTotal / Me.ListBox1.Items.Count
MessageBox.Show(listAvg.ToString)
End Sub
End Class
I just want to add, this is a simple example and depends on there only being numeric amounts in the ListBox. In a real life scenario you would need to add code to verify the listbox data.
Last edited by wes4dbt; Nov 10th, 2017 at 04:30 PM.
-
Nov 10th, 2017, 05:08 PM
#4
Thread Starter
New Member
Re: Receiving Numerical Data from a list box to get an avg val
thank you both very much, And i will definitely work on being more consistent with naming variables, the error did disappear and thank you wes, for showing a better way to get the average without having to call upon getnumbers().
Thank you
-Shappy
-
Nov 10th, 2017, 06:04 PM
#5
Re: [RESOLVED] Receiving Numerical Data from a list box to get an avg val
I wanted to take a minute to reply to this even though it is resolved because there are some things that I think could be optimized and help you become a better programmer.
It looks like you're already converting values using CDbl, so if you have Option Strict on, then great! If not, then you should certainly turn it on. Since you are not using a numeric control (like a NumericUpDown) to get user input then you should use Double.TryParse to convert the String values from the TextBox to a Double:
Code:
'Adding values to the ListBox
Dim input As Double
If Double.TryParse(TextBoxInput.Text, input) Then
lstInput.Items.Add(input)
Else
MessageBox.Show("Please enter a valid Double.", "Invalid Input", MessageBoxButtons.OK)
End If
Also, while technically you're using ByRef correctly in your getValues method, you should consider changing the method to a Function and passing the ListBox item collection as a ByVal parameter. This is much more fluid and how something like this would traditionally be written:
Code:
Private Function getValues(ByVal input() As ListBox.ObjectCollection) As Double()
Dim values(input.Count - 1) As Double
For counter As Integer = 0 To input.Count - 1
values(counter) = CDbl(input.Item(counter))
Next
Return values
End Function
Finally, I don't know if this is for an assignment or not but if you're able to, use the Average enumerable method directly on the array:
Code:
Dim average As Double = Me.getValues(lstInput.Items).Average()
Again, I'm not trying to nitpick your code, just trying to provide some practical solutions to help you improve.
-
Nov 11th, 2017, 10:03 AM
#6
Re: [RESOLVED] Receiving Numerical Data from a list box to get an avg val
Originally Posted by dday9
Finally, I don't know if this is for an assignment or not but if you're able to, use the Average enumerable method directly on the array:
Code:
Dim average As Double = Me.getValues(lstInput.Items).Average()
Again, I'm not trying to nitpick your code, just trying to provide some practical solutions to help you improve.
Hell you could do it directly on the ListBox:-
vbnet Code:
' Dim avg As Double = lstInput.Items.Cast(Of Object).Where(Function(n) Double.TryParse(n, Nothing)).Select(Function(n) CDbl(n)).Average
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|