Solved

Function

Posted on 2011-09-24
2
246 Views
Last Modified: 2012-06-27
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
Comment
Question by:DColin
2 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 250 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 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 250 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
        al.Add(sv)

        sv = New SigmaValues
        sv.sigmaAway2 = 1
        al.Add(sv)

        sv = New SigmaValues
        sv.sigmaAway2 = 1
        al.Add(sv)

        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

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Write to file when application closes (by logoff) - VB.NET 1 37
vb.net help 4 33
Sql server insert 13 36
VB.Net - String Manipulation Scenario 2 23
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question