3 Replies - 685 Views - Last Post: 02 February 2012 - 10:28 PM Rate Topic: -----

Topic Sponsor:

#1 Jas0791  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 22-September 11

How to Pass structure variable array to function

Posted 02 February 2012 - 02:45 PM

I have a structure to hold a student name, an array to hold 5 test scores, and an average score. I have created an a array of structures for 6 students.
User input is setup through text boxes. I want to calculate the average for each student. I have made a function to do this.
What I am having trouble with is how to pass the array of test scores to the function.

Here is what I have so far:
CODE********
Public Const intMAX_STUDENTS As Integer = 6
    Public Const intMAX_NUMTESTS As Integer = 5
    Public student(intMAX_STUDENTS - 1) As StudentData

    Public Structure StudentData
        Dim strStudentName As String
        Dim dblTestscores() As Double
        Dim dblAverage As Double
    End Structure

    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        For intIndex = 0 To (student.Length - 1)
            ReDim student(intIndex).dblTestscores(intMAX_NUMTESTS - 1)
        Next

    End Sub

Private Sub btnCalcAverage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalcAverage.Click

       student(0).dblAverage = CalcAverage(student(0).dblTestscores)[/color]
        lblStudent1Average.Text = student(0).dblAverage.ToString("n1")

    End Sub

    Function CalcAverage(ByVal student As StudentData) As Double
        Dim dblTotal As Single = 0
        Dim dblAverageScore As Double
        Dim intCount As Integer

        With student
            For intCount = 0 To (student.dblTestscores.Length - 1)
                dblTotal += student.dblTestscores(intCount)
            Next

            dblAverageScore = dblTotal / student.dblTestscores.Length

            Return dblAverageScore

        End With
    End Function


Any input would be great

Thanks

This post has been edited by AdamSpeight2008: 02 February 2012 - 02:54 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How to Pass structure variable array to function

#2 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 756
  • View blog
  • Posts: 2,822
  • Joined: 02-July 08

Re: How to Pass structure variable array to function

Posted 02 February 2012 - 02:52 PM

You know we have a button in the editor for putting code in a code block - it looks like [CODE]. Use a List(Of Double) instead of an array. Even though you ReDim your array it is still empty. You have to find a way to fill it. The list(Of T) has an Add method instead of ReDim plus after you fill it all you do is call it's extension function Average and you now do not need to reinvent the wheel with your function.
Was This Post Helpful? 0
  • +
  • -

#3 trevster344  Icon User is online

  • Slick.
  • member icon

Reputation: 124
  • View blog
  • Posts: 873
  • Joined: 16-March 11

Re: How to Pass structure variable array to function

Posted 02 February 2012 - 03:49 PM

Redim doesn't actually resize the array either, it just copies all of its elements and creates a new array with them in it.
Was This Post Helpful? 0
  • +
  • -

#4 demausdauth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 162
  • View blog
  • Posts: 563
  • Joined: 03-February 10

Re: How to Pass structure variable array to function

Posted 02 February 2012 - 10:28 PM

You don't pass the array of test scores. You already have it setup to recieve a StudentData. Just go from there.

Private Sub btnCalcAverage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalcAverage.Click
     'Here you want to loop through each of the items in the student array and call the CalcAverage.
     ' For index As Integer = 0 To student.Length - 1
     ' then pass in student(index) to the function    
     student(0).dblAverage = CalcAverage(student(0))
     lblStudent1Average.Text = student(0).dblAverage.Totring("n1")

    End Sub


Honestly, since you are passing the StudentData object to the CalcAverage method you could just as well set the average inside the method.
    Function CalcAverage(ByVal student As StudentData) As Double
        Dim dblTotal As Single = 0
        Dim dblAverageScore As Double
        Dim intCount As Integer

        With student
            For intCount = 0 To (student.dblTestscores.Length - 1)
                dblTotal += student.dblTestscores(intCount)
            Next

            dblAverageScore = dblTotal / student.dblTestscores.Length

            Return dblAverageScore

        End With
    End Function



If you are going to use the With keyword then you don't need to restate the object that you are "With-ing". :D

The CalcAverage method could be cleaned up a bit to ... I already mentioned "With" And it could easily be converted to a Sub Procedure rather than a Function.

Not too bad otherwise.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1