9 Replies - 1305 Views - Last Post: 09 April 2013 - 09:31 PM Rate Topic: -----

#1 misseshoney  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 07-April 13

Problem printing array of structures vb.net

Posted 08 April 2013 - 10:20 AM

I am working on my Final Project for Visual Basic 2010 class and I am struggling with the code to print an array of structures to a report. The code I have so far is as follows:

--------------------------------------------------------------------------------------------------------------------
Public Class MainForm
    'declare a structure data type named SingleRecord
    Public Structure SingleRecord
        Public strCustNbr As String                          'customer number
        Public strLName As String                              'last name
        Public strFName As String                              'first name
        Public strAdd As String                                'address info
        Public strCity As String                               'city info
        Public strState As String                              'state info
        Public strZip As Integer                                 'zip code
        Public strPhone As String                            'telephone number
        Public decBal As Decimal                               'account balance amount
        Public datLPmt As Date                                'last pmt date
    End Structure
    Public Array() As SingleRecord


Public Sub mnuFilePrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFilePrint.Click
        'add words to array of structures Array()

        Dim intCount As Integer = 0
        Dim inputFile As StreamReader
        inputFile = File.OpenText("customeraccounts.txt")

        Do While inputFile.Peek = -1
            Array(intCount).strCustNbr = inputFile.ReadLine()
            Array(intCount).strLName = inputFile.ReadLine()
            Array(intCount).strFName = inputFile.ReadLine()
            Array(intCount).strAdd = inputFile.ReadLine()
            Array(intCount).strCity = inputFile.ReadLine()
            Array(intCount).strState = inputFile.ReadLine()
            Array(intCount).strZip = inputFile.ReadLine()
            Array(intCount).strPhone = inputFile.ReadLine()
            Array(intCount).decBal = inputFile.ReadLine()
            Array(intCount).datLPmt = inputFile.ReadLine()
            intCount += 1
        Loop

        'close the file
        inputFile.Close()



        'print the current document
        pdPrint.Print()
    End Sub

    Private Sub pdPrint_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pdPrint.PrintPage

      

        Dim intCounter As Integer = 0
        Dim intVertical As Integer

        'print the report header
        e.Graphics.DrawString("Customer Accounts", New Font("courier new", 12, FontStyle.Bold), Brushes.Black, 150, 10)
       
 e.Graphics.DrawString("Date and Time: " & Now.ToString(), New Font("courier new", 12, FontStyle.Bold), Brushes.Black, 10, 38)

        'print the column headings
        e.Graphics.DrawString(String.Format("{0, 10}{1,15}{2,15}{3,15}{4,10}{5,10}{6,10}{7,12}{8,10}{9,12} ", "Cust Nbr", "Last Name", "First Name", "Address", "City", "State", "Zip", "Phone", "Acct Bal", "Last Pymt"), New Font("courier new", 12, FontStyle.Bold), Brushes.Black, 10, 66)

        'print the body of the report

        For intCounter = 0 To Array.Length

            e.Graphics.DrawString(String.Format("{0, 10}{1,15}{2,15}{3,15}{4,10}{5,10}{6,10}{7,12}{8,10}{9,12} ", Array(intCounter).strCustNbr, Array(intCounter).strLName, Array(intCounter).strFName, Array(intCounter).strAdd, Array(intCounter).strCity, Array(intCounter).strState, Array(intCounter).strZip, Array(intCounter).strPhone, Array(intCounter).decBal.ToString("c"), Array(intCounter).datLPmt.ToString("d")), New Font("Courier New", 12, FontStyle.Regular), Brushes.Black, 10, intVertical)
            intVertical += 1
        Next
    End Sub

---------------------------------------------------------------------------------------------------------------

>>>> I have tried to chant the For intCounter = 0 to (Array.length-1)
but I continually get the same error message Object reference not set to an instance of an object.

Please help. Thank you

This post has been edited by modi123_1: 08 April 2013 - 10:25 AM
Reason for edit:: highlight the text and just click the 'code' button in the text editor


Is This A Good Question/Topic? 0
  • +

Replies To: Problem printing array of structures vb.net

#2 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1030
  • View blog
  • Posts: 4,008
  • Joined: 02-July 08

Re: Problem printing array of structures vb.net

Posted 08 April 2013 - 10:35 AM

Why not kame it a class instead of a structure? Don't name collections the same as other objects that already exist(Array()) - this can cause undesired effects if the compiler picks the System.Array instead of your Array() variable. I prefer a List(Of T) over arrays, it is a type specific collection where you can use a structure or class object as the datatype. I don't see where you have loaded any data into your array.
Was This Post Helpful? 0
  • +
  • -

#3 misseshoney  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 07-April 13

Re: Problem printing array of structures vb.net

Posted 08 April 2013 - 12:21 PM

I have been struggling and struggling to figure out what I am not doing correctly with this code. I am finishing the final project for my Intro to Visual Basic class. Part of the requirement is to print an array of structures into a report. I am having trouble with the readline command adding values to the array. Please take a look and thank you
Imports System.IO

Public Class MainForm
    'declare a structure data type named SingleRecord
    Public Structure SingleRecord
        Public strCustNbr As String                          'customer number
        Public strLName As String                              'last name
        Public strFName As String                              'first name
        Public strAdd As String                                'address info
        Public strCity As String                               'city info
        Public strState As String                              'state info
        Public strZip As Integer                                 'zip code
        Public strPhone As String                            'telephone number
        Public decBal As Decimal                               'account balance amount
        Public datLPmt As Date                                'last pmt date
    End Structure

    Dim accounts() As SingleRecord
    Public Sub mnuFilePrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFilePrint.Click
        'add words to array of structures Array()

        Dim intIndex As Integer = 0
        Dim inputFile As StreamReader
        inputFile = File.OpenText("customeraccounts.txt")

        Do While inputFile.Peek = -1
            accounts(intIndex).strCustNbr = inputFile.ReadLine()
            accounts(intIndex).strLName = inputFile.ReadLine()
            accounts(intIndex).strFName = inputFile.ReadLine()
            accounts(intIndex).strAdd = inputFile.ReadLine()
            accounts(intIndex).strCity = inputFile.ReadLine()
            accounts(intIndex).strState = inputFile.ReadLine()
            accounts(intIndex).strZip = inputFile.ReadLine()
            accounts(intIndex).strPhone = inputFile.ReadLine()
            accounts(intIndex).decBal = inputFile.ReadLine()
            accounts(intIndex).datLPmt = inputFile.ReadLine()
            intIndex += 1
        Loop

        'close the file
        inputFile.Close()


        'print the current document
        pdPrint.Print()
    End Sub

    Private Sub pdPrint_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pdPrint.PrintPage

        Dim intCount As Integer = 0
        Dim intVertical As Integer

        'print the report header
        e.Graphics.DrawString("Customer Accounts", New Font("courier new", 12, FontStyle.Bold), Brushes.Black, 150, 10)
        e.Graphics.DrawString("Date and Time: " & Now.ToString(), New Font("courier new", 12, FontStyle.Bold), Brushes.Black, 10, 38)
        'print the column headings
        e.Graphics.DrawString(String.Format("{0, 10}{1,15}{2,15}{3,15}{4,10}{5,10}{6,10}{7,12}{8,10}{9,12} ", "Cust Nbr", "Last Name", "First Name", "Address", "City", "State", "Zip", "Phone", "Acct Bal", "Last Pymt"), New Font("courier new", 12, FontStyle.Bold), Brushes.Black, 10, 66)

        'print the body of the report

        For intCount = 0 To (Accounts.Length - 1)

            e.Graphics.DrawString(String.Format("{0, 10}{1,15}{2,15}{3,15}{4,10}{5,10}{6,10}{7,12}{8,10}{9,12} ", Accounts(intCount).strCustNbr, Accounts(intCount).strLName, Accounts(intCount).strFName, Accounts(intCount).strAdd, Accounts(intCount).strCity, Accounts(intCount).strState, Accounts(intCount).strZip, Accounts(intCount).strPhone, Accounts(intCount).decBal.ToString("c"), Accounts(intCount).datLPmt.ToString("d")), New Font("Courier New", 12, FontStyle.Regular), Brushes.Black, 10, intVertical)
            intVertical += 1
        Next
    End Sub
End Class

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8371
  • View blog
  • Posts: 31,099
  • Joined: 12-June 08

Re: Problem printing array of structures vb.net

Posted 08 April 2013 - 12:28 PM

Do not create duplicate topics. Merging.
Was This Post Helpful? 0
  • +
  • -

#5 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1533
  • View blog
  • Posts: 4,919
  • Joined: 25-September 09

Re: Problem printing array of structures vb.net

Posted 08 April 2013 - 12:36 PM

One thing I see off the bat is that your Do while loop is wrong
Do While inputFile.Peek = -1
Should be
Do While inputFile.Peek <> -1

You are basically saying while it is the end of the stream instead of while it's not the end of the stream.

That will probably fix a lot.

Also, to save you some paper while you're setting up the document layout (which appears that it will go off page)

Where you have
pdPrint.Print() Remark this line until you have the form looking like you want it
and create a print preview dialog
dim pd as new PrintPreviewDialog
pd.document = pdprint
pd.showdialog

Now instead of printing directly to the printer, it will open in a preview dialog so that you can look to see if the layout is what you want.

Once done, if you no longer want this, remark the block and un-remark your print line.


Edit:
Just caught the fact that you are not sizing your array as needed.

Look at the ReDim Statement
AdamSpeight2008's tutorial on Arrays would be a good read for you too. He mentions this problem about 10 lines into the tutorial.

This post has been edited by CharlieMay: 08 April 2013 - 01:00 PM

Was This Post Helpful? 0
  • +
  • -

#6 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Problem printing array of structures vb.net

Posted 08 April 2013 - 01:23 PM

Someone suggested you use a List(Of T), and I strongly agree with that. With a List(Of T), you don't have to mess around with dimensioning it, redimensioning, etc., and you can access it with an index just like an array.

Here's a modified version of the first part of your code. It works. I usually don't like to just hand you code, but I couldn't see a better way to show you a few things.

1. The error you were getting was because you never made an instance of the Structure. Simply defining the Structure does not make a sructure. You have to make one with a Dim statement, such as Dim CustRecord as New SingleRecord.

2. I have included an Option Strict On statement, which pointed out some data type problems. See the link in my signature to learn why this is A Good Thing.

Option Strict On
Imports System.IO

Public Class MainForm
    'declare a structure data type named SingleRecord
    Public Structure SingleRecord
        Public strCustNbr As String                          'customer number
        Public strLName As String                              'last name
        Public strFName As String                              'first name
        Public strAdd As String                                'address info
        Public strCity As String                               'city info
        Public strState As String                              'state info
        Public strZip As Integer                                 'zip code
        Public strPhone As String                            'telephone number
        Public decBal As Decimal                               'account balance amount
        Public datLPmt As Date                                'last pmt date
    End Structure

    Public CustRecord As New List(Of SingleRecord)

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        'add words to array of structures CustRecord()
        Dim recrd As New SingleRecord
        Dim intCount As Integer = 0
        Dim inputFile As StreamReader
        inputFile = File.OpenText("customeraccounts.txt")

        Do While inputFile.Peek <> -1
            recrd.strCustNbr = inputFile.ReadLine()
            recrd.strLName = inputFile.ReadLine()
            recrd.strFName = inputFile.ReadLine()
            recrd.strAdd = inputFile.ReadLine()
            recrd.strCity = inputFile.ReadLine()
            recrd.strState = inputFile.ReadLine()
            recrd.strZip = CInt(inputFile.ReadLine())
            recrd.strPhone = inputFile.ReadLine()
            recrd.decBal = CDec(inputFile.ReadLine())
            recrd.datLPmt = CDate(inputFile.ReadLine())
            CustRecord.Add(recrd)
            intCount += 1
        Loop

        'close the file
        inputFile.Close()

        'print the current document
        pdPrint.Print()
    End Sub


I can't help you much on the printing problem, as I am not very familiar with the Print Document Class, but one thing I can say about the printing is that you can use a For loop...

'either
        For intCounter = 0 to CustRecord.Count - 1
        '...
        Next
'or
        For Each Rec as SingleRecord in CustRecord
        '...
        Next


This post has been edited by lar3ry: 08 April 2013 - 01:25 PM

Was This Post Helpful? 1
  • +
  • -

#7 misseshoney  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 07-April 13

Re: Problem printing array of structures vb.net

Posted 08 April 2013 - 01:52 PM

Thank you so very much to both of you for your help. I am going to implement these things now. Thanks again.
Was This Post Helpful? 0
  • +
  • -

#8 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: Problem printing array of structures vb.net

Posted 08 April 2013 - 01:56 PM

Not fan of While stream.Peek <> -1

Much prefer using, the more easily readable.
While Not stream.EndOfStream

End While


Was This Post Helpful? 0
  • +
  • -

#9 misseshoney  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 07-April 13

Re: Problem printing array of structures vb.net

Posted 09 April 2013 - 09:09 PM

View Postmisseshoney, on 08 April 2013 - 12:21 PM, said:

I have been struggling and struggling to figure out what I am not doing correctly with this code. I am finishing the final project for my Intro to Visual Basic class. Part of the requirement is to print an array of structures into a report. I am having trouble with the readline command adding values to the array. Please take a look and thank you
Imports System.IO

Public Class MainForm
    'declare a structure data type named SingleRecord
    Public Structure SingleRecord
        Public strCustNbr As String                          'customer number
        Public strLName As String                              'last name
        Public strFName As String                              'first name
        Public strAdd As String                                'address info
        Public strCity As String                               'city info
        Public strState As String                              'state info
        Public strZip As Integer                                 'zip code
        Public strPhone As String                            'telephone number
        Public decBal As Decimal                               'account balance amount
        Public datLPmt As Date                                'last pmt date
    End Structure

    Dim accounts() As SingleRecord
    Public Sub mnuFilePrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFilePrint.Click
        'add words to array of structures Array()

        Dim intIndex As Integer = 0
        Dim inputFile As StreamReader
        inputFile = File.OpenText("customeraccounts.txt")

        Do While inputFile.Peek = -1
            accounts(intIndex).strCustNbr = inputFile.ReadLine()
            accounts(intIndex).strLName = inputFile.ReadLine()
            accounts(intIndex).strFName = inputFile.ReadLine()
            accounts(intIndex).strAdd = inputFile.ReadLine()
            accounts(intIndex).strCity = inputFile.ReadLine()
            accounts(intIndex).strState = inputFile.ReadLine()
            accounts(intIndex).strZip = inputFile.ReadLine()
            accounts(intIndex).strPhone = inputFile.ReadLine()
            accounts(intIndex).decBal = inputFile.ReadLine()
            accounts(intIndex).datLPmt = inputFile.ReadLine()
            intIndex += 1
        Loop

        'close the file
        inputFile.Close()


        'print the current document
        pdPrint.Print()
    End Sub

    Private Sub pdPrint_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pdPrint.PrintPage

        Dim intCount As Integer = 0
        Dim intVertical As Integer

        'print the report header
        e.Graphics.DrawString("Customer Accounts", New Font("courier new", 12, FontStyle.Bold), Brushes.Black, 150, 10)
        e.Graphics.DrawString("Date and Time: " & Now.ToString(), New Font("courier new", 12, FontStyle.Bold), Brushes.Black, 10, 38)
        'print the column headings
        e.Graphics.DrawString(String.Format("{0, 10}{1,15}{2,15}{3,15}{4,10}{5,10}{6,10}{7,12}{8,10}{9,12} ", "Cust Nbr", "Last Name", "First Name", "Address", "City", "State", "Zip", "Phone", "Acct Bal", "Last Pymt"), New Font("courier new", 12, FontStyle.Bold), Brushes.Black, 10, 66)

        'print the body of the report

        For intCount = 0 To (Accounts.Length - 1)

            e.Graphics.DrawString(String.Format("{0, 10}{1,15}{2,15}{3,15}{4,10}{5,10}{6,10}{7,12}{8,10}{9,12} ", Accounts(intCount).strCustNbr, Accounts(intCount).strLName, Accounts(intCount).strFName, Accounts(intCount).strAdd, Accounts(intCount).strCity, Accounts(intCount).strState, Accounts(intCount).strZip, Accounts(intCount).strPhone, Accounts(intCount).decBal.ToString("c"), Accounts(intCount).datLPmt.ToString("d")), New Font("Courier New", 12, FontStyle.Regular), Brushes.Black, 10, intVertical)
            intVertical += 1
        Next
    End Sub
End Class

Was This Post Helpful? 0
  • +
  • -

#10 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Problem printing array of structures vb.net

Posted 09 April 2013 - 09:31 PM

I see you've made some changes, and I think it's still not working, but there are no questions or comments from you.

?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1