Solved

Streamwriter array

Posted on 2003-11-15
1
883 Views
Last Modified: 2006-11-17
This is what my book says, I am still learning and am not good with arrays.  Could someone help me please?


Given an array of structures named sCustomers having string members of Name, Address, City and State, write the statements to create an instance of the StreamWriter class, and write the contents of the array to the StreamWriter.  Use the GetUpperBound method to determine the array size
0
Comment
Question by:brassmon
1 Comment
 
LVL 9

Accepted Solution

by:
malharone earned 500 total points
ID: 9755068
hope the below helps... just a recommendation, avoid using structures ... and start taking advantage of the classes.







Public Class Form4
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button3 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.Button3 = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(8, 8)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(112, 32)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Load Fake Data"
        '
        'Button3
        '
        Me.Button3.Location = New System.Drawing.Point(128, 8)
        Me.Button3.Name = "Button3"
        Me.Button3.Size = New System.Drawing.Size(112, 32)
        Me.Button3.TabIndex = 0
        Me.Button3.Text = "Output Data"
        '
        'Form4
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(248, 266)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1, Me.Button3})
        Me.Name = "Form4"
        Me.Text = "Form4"
        Me.ResumeLayout(False)

    End Sub

#End Region

    'Given an array of structures named sCustomers having string members of Name, Address, City and State,
    'write the statements to create an instance of the StreamWriter class, and write the contents of the
    'array to the StreamWriter.  Use the GetUpperBound method to determine the array size

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

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim writer As New IO.StreamWriter("c:\outputFile.txt")
        Dim upperBound As Integer = sCustomers.GetUpperBound(0) - 1
        Dim lowerBound As Integer = sCustomers.GetLowerBound(0)
        Dim currCustomer As Customer
        Dim x As Integer
        For x = lowerBound To upperBound
            currCustomer = sCustomers(x)
            writer.WriteLine(currCustomer.Name)
            writer.WriteLine(currCustomer.Address)
            writer.WriteLine(currCustomer.City)
            writer.WriteLine(currCustomer.State)
            writer.WriteLine()
        Next
        writer.Close()
        Process.Start("c:\outputfile.txt")
        Application.Exit()
    End Sub

    Public sCustomers(3) As Customer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        With sCustomers(0)
            .Name = "Tom"
            .Address = "111 Main Street"
            .City = "Las Vegas"
            .State = "NM"
        End With
        With sCustomers(1)
            .Name = "Michael"
            .Address = "222 Lincoln Avenue"
            .City = "New Mexico City"
            .State = "NV"
        End With
        With sCustomers(2)
            .Name = "Joe"
            .Address = "333 Berkley Blvd"
            .City = "New York"
            .State = "TX"
        End With
        With sCustomers(3)
            .Name = "Ben"
            .Address = "444 Harvard Ct"
            .City = "Houston"
            .State = "NY"
        End With
        MessageBox.Show("Fake data loaded", "info", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Structure Customer
        Public Name As String
        Public Address As String
        Public City As String
        Public State As String
    End Structure
End Class
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now