• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 430
  • Last Modified:

Array - Function with no argument

How do I call a function that takes no argument and returns the average total of the array.

This is what I have come up with:

Public Function Average() as Decimal ={87.5D, 95.25D, 26.50D, 74.45D}
  Dim Average as Decimal
  Dim Number as Integer
  Dim Total as Decimal
If  Number = 0 to 3 then
   Average = Total/Number
Return Convert.ToDecimal (Total/Number)
Else
End If
End Function
0
thimss2
Asked:
thimss2
  • 4
  • 3
1 Solution
 
DabasCommented:
Hi thimss2,
Sorry, does not make sense..
Since you are not passing any arguments, Number will always be zero.
Your function probably will throw a division by zero error.


Dabas
0
 
thimss2Author Commented:
This the function I created. Nnow want to create an array that calls a function that takes no argument and returns the average total of the array.



Public Function Average(ByVal Total as Decimal, ByVal NumberAs Integer) As String

        If Number AndAlso Total > 0 Then
        Return Convert.ToString(Total / Number)
        Else
       Return "Error"
       End If          

      End Function

 ' procedure which passed the argument
Sub DisplayAvg(ByVal Total as Decimal, ByVal Number as Numberas Integer)
MessageBox.Show("Average  " & Average(Total, Number)
End Sub


Or Should I leave it as:
Public Function Average()
Return Convert.ToDecimal (Total/Number)


Thanks
0
 
DabasCommented:
I do not understand what you mean by:

"an array that calls a function"

What you MIGHT be talking about is creating a new class, that consists of an array or a collection and returns the average.

Dabas
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
thimss2Author Commented:
Yes that is what I am trying to do.

0
 
DabasCommented:
thimss2,
OK! Before I get into this, I need to know how much you know about classes if at all

Dabas
0
 
thimss2Author Commented:
My knowledge of classes is very limited. I am a beginner. I know how to create a new class.

Thanks
0
 
DabasCommented:
thimss2,
Lets take it one step at a time.

Create a new class as follows:

Public Class cMyArray
    Dim myArray() As Integer
    Private mNumberofItems As Integer = 0
    Public Sub AddToArray(ByVal NewNumber As Integer)
        ReDim Preserve myArray(mNumberofItems)
        myArray(mNumberofItems) = NewNumber
        mNumberofItems += 1
    End Sub
End Class

This is just the first step, but it has to be clear to you before we continue.
See if you can continue by yourself first, before you ask for the next step, and show me what you have tried.

Dabas
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now