2 Replies - 195 Views - Last Post: 07 February 2012 - 11:10 AM 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

Print structure array variable contents - error

Posted 06 February 2012 - 06:46 PM

I have a structure named StudentData, it consists of a studentname, an array of testscores, and the student average. There is also an array of structure objects for a total of 6 students. I have the header, column heads, the student names and the averages printing when I click the print page event. I have tried multiple ways of setting this up to get the test scores to print, these are the structure array variables. When I put in the code to process the arrays testscores, I receive a run time error that I don't understand.
ERROR:
An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

Additional information: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

I have tried to put the testscores array in a for next loop inside the student structure array, both between the student names and averages, after both of them, used a for each loop, even tried to use a 'with' student statement.

'This gets the the header, column heads, the student names and the averages printing when I click
Private Sub pdPrint_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pdPrint.PrintPage

        Dim intVertPos As Integer
        'Print the Report header.
        e.Graphics.DrawString("Student Test Scores Report", New Font("Courier New", 12, FontStyle.Bold), Brushes.Black, 140, 10)
        'Print the current date and time.
        e.Graphics.DrawString("Date and Time: " & Now.ToString(), New Font("Courier New", 12, FontStyle.Bold), Brushes.Black, 15, 38)
        'Print the column headers.
        e.Graphics.DrawString(String.Format("{0, 15} {1, 5} {2, 5} {3, 5} {4, 5} {5, 5} {6, 10} ",
                                            "Student Name", "Test 1", "Test 2", "Test 3", "Test 4", "Test 5", "Average"),
                                            New Font("Courier New", 12, FontStyle.Bold), Brushes.Black, 10, 66)
        'Print the body of the report.
        intVertPos = 90
        For intCount = 0 To (student.Length - 1)
            e.Graphics.DrawString(String.Format("{0, 15} {1, 45}",
                                            student(intCount).strStudentName, student(intCount).dblAverage.ToString("n1")),
                                            New Font("Courier New", 12, FontStyle.Regular), Brushes.Black, 10, intVertPos)
           

            intVertPos += 14
        Next

    End Sub

'These are the different things I have tried for the test arrays, this is what gets the error at the click event.

'Print the body of the report.
        intVertPos = 90
        For intCount = 0 To (student.Length - 1)
            e.Graphics.DrawString(String.Format("{0, 15} ",
                                            student(intCount).strStudentName),
                                            New Font("Courier New", 12, FontStyle.Regular), Brushes.Black, 10, intVertPos)
            For intTest = 0 To (intMAX_NUMTESTS - 1)
                e.Graphics.DrawString(String.Format("{0, 20} {1, 5} {2, 5} {3, 5} {4, 10} ",
                                            student(intCount).dblTestscores(intTest).ToString),
                                            New Font("Courier New", 12, FontStyle.Regular), Brushes.Black, 10, intVertPos)
            Next
            e.Graphics.DrawString(String.Format("{0, 45} ",
                                            student(intCount).dblAverage.ToString),
                                            New Font("Courier New", 12, FontStyle.Regular), Brushes.Black, 10, intVertPos)
            intVertPos += 16
        Next

'print testscores try 2
	For Each intTest As Double student.dblTestscores
                e.Graphics.DrawString(String.Format("{0, 20} {1, 5} {2, 5} {3, 5} {4, 5} ",
                student(intCount).dblTestscores(intTest).ToString()),
                New Font("Courier New", 12, FontStyle.Regular), Brushes.Black, 10, intVertPos)
            Next

'printscoresTry3
	 With student

            For intCount = 0 To (student.Length - 1)
                e.Graphics.DrawString(String.Format("{0, 15} {1, 45}",
                                                student(intCount).strStudentName, student(intCount).dblAverage.ToString("n1")),
                                                New Font("Courier New", 12, FontStyle.Regular), Brushes.Black, 10, intVertPos)

                For Each intTest As Double In dblTestscores
                    e.Graphics.DrawString(String.Format("{0, 20} {1, 5} {2, 5} {3, 5} {4, 5} ",
                    student(intCount).dblTestscores(intTest).ToString()),
                    New Font("Courier New", 12, FontStyle.Regular), Brushes.Black, 10, intVertPos)
                Next
                intVertPos += 14
            Next
        End With



THANKS

Is This A Good Question/Topic? 0
  • +

Replies To: Print structure array variable contents - error

#2 AdamSpeight2008  Icon User is offline

  • Coder-ian
  • member icon

Reputation: 1401
  • View blog
  • Posts: 7,358
  • Joined: 29-May 08

Re: Print structure array variable contents - error

Posted 07 February 2012 - 08:22 AM

The key is to understand the error message, as it directs you to where the problem lays.

Quote

An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll
[/code]
So must be something with your usage of String.Format.
[code]
Additional information: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

So you must be indexing outside the range, or you're missing some.

Do the number of arguments you state in the string, match the number of arguments you supply?
Was This Post Helpful? 0
  • +
  • -

#3 Ionut  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 327
  • View blog
  • Posts: 914
  • Joined: 17-July 10

Re: Print structure array variable contents - error

Posted 07 February 2012 - 11:10 AM

try2 and try3 are incorrect because in intTest you have a value from array
example:
Testscores = (3, 10, 9, 9, 6) -> length = 5
for each intTest in students(i).Testscores

next intTest

'in step one, intTest = 3 => Testscores(intTest) = 9 (second 9)
'in step two, intTest = 10 => Testscores(intTest) => index out of bounds(you have only 5 elements)



The problem might be in the first for: instead of intMAX_NUMTESTS use Testscores.Length. If the array has less items than intMAX_NUMTESTS, then the error can occur
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1