Solved

# Function

Posted on 2011-09-24
Medium Priority
264 Views
Public Class SigmaValues

Public sigmaAway1 As Double
Public sigmaAway2 As Double
Public sigmaAway3 As Double
Public sigmaAway4 As Double
Public sigmaAway5 As Double
Public sigmaAway6 As Double
Public sigmaAway7 As Double
Public sigmaAway8 As Double

End class

Public Function GetSigmaSum(sigmaList as arraylist, sigmaID as integer) as double

Dim sigmaSum as double

return sigmaSum

End Function

Hi Experts,

Please look at the above snippet. I would like to pass an array of sigmavalues to a function and it pass back the sum of all values in a single sigma group. How can I do this. Useage would be.

GetSigmaSum(sigmalist, 2)

This would pass back the sum of all sigmaAway2 values.

0
Question by:DColin
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 83

Accepted Solution

CodeCruiser earned 1000 total points
ID: 36593854
Is the number of sigmaaway variables in class limited? Or is it extendable? How about using an array within the class as well? Then you can try something like below

Public Function GetSigma(sigmaList as arraylist, sigmaID as integer) as double
Dim Sum as Double
For Each sl in SigmaList
Sum += sl.Values(sigmaID - 1) 'Values is the array in the class
Next
Return Sum
End Function

If not using array then something like below

Public Function GetSigma(sigmaList as arraylist, sigmaID as integer) as double
Dim Sum as Double
For Each sl in SigmaList
If sigmaID = 1 Then
Sum += sl.SigmaAway1
ElseIf SigmaID = 2 then
Sum += sl.SigmaAway2
...
End If
Next
Return Sum
End Function
0

LVL 86

Assisted Solution

Mike Tomlinson earned 1000 total points
ID: 36594496
You can do this via Reflection or the legacy CallByName()  function.

Here's a quick example without any error checking:
``````Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim al As New ArrayList

Dim sv As New SigmaValues
sv.sigmaAway2 = 1

sv = New SigmaValues
sv.sigmaAway2 = 1

sv = New SigmaValues
sv.sigmaAway2 = 1

Dim sum As Double = GetSigmaSum(al, 2)
MessageBox.Show(sum)
End Sub

Public Function GetSigmaSum(ByVal sigmaList As ArrayList, ByVal sigmaID As Integer) As Double
Dim sigmaSum As Double
For Each sl As SigmaValues In sigmaList
sigmaSum = sigmaSum + CallByName(sl, "sigmaAway" & sigmaID, CallType.Get)
Next
Return sigmaSum
End Function

End Class

Public Class SigmaValues
Public sigmaAway1 As Double
Public sigmaAway2 As Double
Public sigmaAway3 As Double
Public sigmaAway4 As Double
Public sigmaAway5 As Double
Public sigmaAway6 As Double
Public sigmaAway7 As Double
Public sigmaAway8 As Double
End Class
``````
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
###### Suggested Courses
Course of the Month8 days, 20 hours left to enroll