none
Can I omit paramarray keyword vb.net?

    Question

  • Are those 2 functions equivalent?

        Private Function PAMean(ParamArray PA As Integer()) As Integer

            Dim res As Integer

            For i = 0 To UBound(PA)

                res += PA(i)

            Next

            Return res / PA.Length

        End Function


        Private Function AMean(ByVal PA As Integer()) As Integer

            Dim res As Integer

            For i = 0 To UBound(PA)

                res += PA(i)

            Next

            Return res / PA.Length

        End Function

    Friday, April 21, 2017 5:58 PM

Answers

  • If you omit ParamArray, you will not be able to specify the values directly:  ‘m = PAMean(1, 2, 3)’. You will need to define an array containing 1, 2, 3, or to make a literal: ‘m = PAMean({1, 2, 3})’.

    • Marked as answer by JP Clem Monday, May 01, 2017 1:49 PM
    Friday, April 21, 2017 7:17 PM

All replies

  • JP,

    They almost are identical, but as a suggestion, at the top of your code put the following:

    Option Strict On
    Option Explicit On
    Option Infer Off

    You'll see some compile errors so fix those and run them.

    *****

    Also, in your function(s), you might want to test for validity.


    "A problem well stated is a problem half solved.” - Charles F. Kettering


    Friday, April 21, 2017 6:07 PM
  • As an idea:

    Option Strict On
    Option Explicit On
    Option Infer Off

    Public Class Form1
        Private Sub Form1_Load(sender As System.Object, _
                               e As System.EventArgs) _
                               Handles MyBase.Load

            Dim test1 As Double = _
                GetMean(New Integer() {1, 2, 3, 4, 5})

            Dim test2 As Double = _
                GetMean(New List(Of Integer) From {1, 2, 3, 4, 5})

            Dim test3 As Double = GetMean(1, 2, 3, 4, 5)

            Stop

        End Sub

        Private Function GetMean(ByVal numbers As IEnumerable(Of Integer)) As Double

            If numbers IsNot Nothing AndAlso numbers.Count > 0 Then
                Return numbers.Average
            Else
                Return 0
            End If

        End Function

        Private Function GetMean(ByVal ParamArray numbers() As Integer) As Double

            If numbers IsNot Nothing AndAlso numbers.Count > 0 Then
                Return numbers.Average
            Else
                Return 0
            End If

        End Function
    End Class



    "A problem well stated is a problem half solved.” - Charles F. Kettering


    Friday, April 21, 2017 6:16 PM
  • If you omit ParamArray, you will not be able to specify the values directly:  ‘m = PAMean(1, 2, 3)’. You will need to define an array containing 1, 2, 3, or to make a literal: ‘m = PAMean({1, 2, 3})’.

    • Marked as answer by JP Clem Monday, May 01, 2017 1:49 PM
    Friday, April 21, 2017 7:17 PM
  • Frank L. Smith,

    Thank you a lot for your fast answers, they  were useful.

    • Edited by JP Clem Saturday, April 22, 2017 1:25 PM
    Saturday, April 22, 2017 1:22 PM
  • Frank L. Smith,

    Thank you a lot for your fast answers, they  were useful.


    I'm glad that it helped. :)

    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Saturday, April 22, 2017 1:54 PM
  • I see only one function for the paramarray and that is when a program is called from the commandline.

    Here nice described how to use it.

    https://msdn.microsoft.com/en-us/library/538f81ec.aspx


    Success
    Cor

    Sunday, April 23, 2017 1:35 PM
  • Hi JP,

    It seems that Frank's post solve your issue, please remember to close your thread by marking Frank's post as answer, it is very beneficial to the other communities who face the same issue.

    Thanks for your understanding.

    Best Regards,

    Cherry Bu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 25, 2017 5:14 AM
    Moderator