VS 2017 [RESOLVED] Receiving Numerical Data from a list box to get an avg val-VBForums
Results 1 to 6 of 6

Thread: [RESOLVED] Receiving Numerical Data from a list box to get an avg val

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2017
    Posts
    4

    Resolved [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

  2. #2
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,004

    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.
    Nothing I post is production-ready. It is provided as-is, use it at your own risk.

  3. #3
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    3,397

    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.

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2017
    Posts
    4

    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

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,171

    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.

  6. #6
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    5,425

    Re: [RESOLVED] Receiving Numerical Data from a list box to get an avg val

    Quote Originally Posted by dday9 View Post
    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:
    1. '
    2. Dim avg As Double = lstInput.Items.Cast(Of Object).Where(Function(n) Double.TryParse(n, Nothing)).Select(Function(n) CDbl(n)).Average
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.