By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,370 Members | 1,574 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,370 IT Pros & Developers. It's quick & easy.

Please Help! Arraylist of structures

P: n/a
I have a form with a 2 textboxes and a listbox, The data is read in by a
file called "memberphones" The listnbox lists names in this way
(lastname,first name) sorted by the last name. the textboxes have the
"lastname,first name" and the phone in the other one. Now I am stumpped
about how to write the instrucution to modify a record of the arraylist.
here is the whole code.

Imports Microsoft.VisualBasic
Imports System.IO
Imports System.Collections

Public Class Form1
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 MainMenu1 As System.Windows.Forms.MainMenu
Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
Friend WithEvents mnuModify As System.Windows.Forms.MenuItem
Friend WithEvents mnuAdd As System.Windows.Forms.MenuItem
Friend WithEvents mnuDelete As System.Windows.Forms.MenuItem
Friend WithEvents mnuExit As System.Windows.Forms.MenuItem
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents lstNames As System.Windows.Forms.ListBox
Friend WithEvents txtName As System.Windows.Forms.TextBox
Friend WithEvents txtPhone As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.lstNames = New System.Windows.Forms.ListBox
Me.txtName = New System.Windows.Forms.TextBox
Me.txtPhone = New System.Windows.Forms.TextBox
Me.MainMenu1 = New System.Windows.Forms.MainMenu
Me.MenuItem1 = New System.Windows.Forms.MenuItem
Me.mnuModify = New System.Windows.Forms.MenuItem
Me.mnuAdd = New System.Windows.Forms.MenuItem
Me.mnuDelete = New System.Windows.Forms.MenuItem
Me.mnuExit = New System.Windows.Forms.MenuItem
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'lstNames
'
Me.lstNames.Location = New System.Drawing.Point(56, 16)
Me.lstNames.Name = "lstNames"
Me.lstNames.Size = New System.Drawing.Size(208, 160)
Me.lstNames.Sorted = True
Me.lstNames.TabIndex = 0
'
'txtName
'
Me.txtName.Location = New System.Drawing.Point(128, 192)
Me.txtName.Name = "txtName"
Me.txtName.Size = New System.Drawing.Size(136, 20)
Me.txtName.TabIndex = 1
Me.txtName.Text = ""
'
'txtPhone
'
Me.txtPhone.Location = New System.Drawing.Point(128, 224)
Me.txtPhone.Name = "txtPhone"
Me.txtPhone.Size = New System.Drawing.Size(136, 20)
Me.txtPhone.TabIndex = 2
Me.txtPhone.Text = ""
'
'MainMenu1
'
Me.MainMenu1.MenuItems.AddRange(New
System.Windows.Forms.MenuItem() {Me.MenuItem1, Me.mnuExit})
'
'MenuItem1
'
Me.MenuItem1.Index = 0
Me.MenuItem1.MenuItems.AddRange(New
System.Windows.Forms.MenuItem() {Me.mnuModify, Me.mnuAdd, Me.mnuDelete})
Me.MenuItem1.Text = "Update"
'
'mnuModify
'
Me.mnuModify.Index = 0
Me.mnuModify.Text = "&Modify"
'
'mnuAdd
'
Me.mnuAdd.Index = 1
Me.mnuAdd.Text = "&Add"
'
'mnuDelete
'
Me.mnuDelete.Index = 2
Me.mnuDelete.Text = "&Delete"
'
'mnuExit
'
Me.mnuExit.Index = 1
Me.mnuExit.Text = "Exit"
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(24, 192)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(96, 16)
Me.Label1.TabIndex = 3
Me.Label1.Text = "Name:"
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(24, 224)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(96, 16)
Me.Label2.TabIndex = 4
Me.Label2.Text = "Phone:"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.txtPhone)
Me.Controls.Add(Me.txtName)
Me.Controls.Add(Me.lstNames)
Me.Menu = Me.MainMenu1
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region


Structure member
Dim name As String
Dim phone As String
End Structure

Dim memberarray As New ArrayList
Dim membership As member
Dim fv As StreamReader = File.OpenText("memberphones.txt")

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
memberarray = read_file(memberarray)
list_members(memberarray)
fv.Close()

End Sub
Private Function read_file(ByVal array As ArrayList) As ArrayList
Dim icounter As Integer 'iterates loop
Do
membership.name = fv.ReadLine
membership.phone = fv.ReadLine
array.Add(membership)
icounter = icounter + 1
Loop Until fv.Peek = -1
Return array
End Function
Private Sub list_members(ByVal memberarray As ArrayList)
Dim member_counter As Integer
lstNames.Items.Clear()
For Each membership In memberarray
lstNames.Items.Add(membership.name)
Next
End Sub

Private Sub lstNames_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
lstNames.SelectedIndexChanged
Dim x As member
For Each membership In memberarray
If membership.name.ToString() = lstNames.Items
(lstNames.SelectedIndex).ToString() Then
txtName.Text = membership.name
txtPhone.Text = membership.phone
End If
Next
End Sub

Private Sub mnuAdd_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuAdd.Click
Dim fr As StreamWriter = File.AppendText("memberphones.txt")
membership.name = txtName.Text
membership.phone = txtPhone.Text
fr.WriteLine(membership.name)
fr.WriteLine(membership.phone)
fr.Close()
list_members(memberarray)

End Sub
Private Sub mnuDelete_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles mnuDelete.Click

memberarray.RemoveAt(lstNames.SelectedIndex)
list_members(memberarray)

End Sub
Private Sub mnuModify_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles mnuModify.Click

index = lstNames.SelectedIndex

membership.name = txtName.Text
membership.phone = txtPhone.Text
************************************************** **********************
CType(memberarray(lstnames.items(lstnames.selected index),
member).phone = txtPhone.Text
I have tried every different derivitive of this and nothing seems to
work, Is there something I am doing wrong? Or is there something else I
shoud try??

JOhn
Nov 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
You're holding value types within a reference type...
http://www.knowdotnet.com/articles/boxedstructures.html
"RBCC" <rb**@vei.net> wrote in message
news:Xn*****************************@207.46.248.16 ...
I have a form with a 2 textboxes and a listbox, The data is read in by a
file called "memberphones" The listnbox lists names in this way
(lastname,first name) sorted by the last name. the textboxes have the
"lastname,first name" and the phone in the other one. Now I am stumpped
about how to write the instrucution to modify a record of the arraylist.
here is the whole code.

Imports Microsoft.VisualBasic
Imports System.IO
Imports System.Collections

Public Class Form1
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 MainMenu1 As System.Windows.Forms.MainMenu
Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
Friend WithEvents mnuModify As System.Windows.Forms.MenuItem
Friend WithEvents mnuAdd As System.Windows.Forms.MenuItem
Friend WithEvents mnuDelete As System.Windows.Forms.MenuItem
Friend WithEvents mnuExit As System.Windows.Forms.MenuItem
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents lstNames As System.Windows.Forms.ListBox
Friend WithEvents txtName As System.Windows.Forms.TextBox
Friend WithEvents txtPhone As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.lstNames = New System.Windows.Forms.ListBox
Me.txtName = New System.Windows.Forms.TextBox
Me.txtPhone = New System.Windows.Forms.TextBox
Me.MainMenu1 = New System.Windows.Forms.MainMenu
Me.MenuItem1 = New System.Windows.Forms.MenuItem
Me.mnuModify = New System.Windows.Forms.MenuItem
Me.mnuAdd = New System.Windows.Forms.MenuItem
Me.mnuDelete = New System.Windows.Forms.MenuItem
Me.mnuExit = New System.Windows.Forms.MenuItem
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'lstNames
'
Me.lstNames.Location = New System.Drawing.Point(56, 16)
Me.lstNames.Name = "lstNames"
Me.lstNames.Size = New System.Drawing.Size(208, 160)
Me.lstNames.Sorted = True
Me.lstNames.TabIndex = 0
'
'txtName
'
Me.txtName.Location = New System.Drawing.Point(128, 192)
Me.txtName.Name = "txtName"
Me.txtName.Size = New System.Drawing.Size(136, 20)
Me.txtName.TabIndex = 1
Me.txtName.Text = ""
'
'txtPhone
'
Me.txtPhone.Location = New System.Drawing.Point(128, 224)
Me.txtPhone.Name = "txtPhone"
Me.txtPhone.Size = New System.Drawing.Size(136, 20)
Me.txtPhone.TabIndex = 2
Me.txtPhone.Text = ""
'
'MainMenu1
'
Me.MainMenu1.MenuItems.AddRange(New
System.Windows.Forms.MenuItem() {Me.MenuItem1, Me.mnuExit})
'
'MenuItem1
'
Me.MenuItem1.Index = 0
Me.MenuItem1.MenuItems.AddRange(New
System.Windows.Forms.MenuItem() {Me.mnuModify, Me.mnuAdd, Me.mnuDelete})
Me.MenuItem1.Text = "Update"
'
'mnuModify
'
Me.mnuModify.Index = 0
Me.mnuModify.Text = "&Modify"
'
'mnuAdd
'
Me.mnuAdd.Index = 1
Me.mnuAdd.Text = "&Add"
'
'mnuDelete
'
Me.mnuDelete.Index = 2
Me.mnuDelete.Text = "&Delete"
'
'mnuExit
'
Me.mnuExit.Index = 1
Me.mnuExit.Text = "Exit"
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(24, 192)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(96, 16)
Me.Label1.TabIndex = 3
Me.Label1.Text = "Name:"
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(24, 224)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(96, 16)
Me.Label2.TabIndex = 4
Me.Label2.Text = "Phone:"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.txtPhone)
Me.Controls.Add(Me.txtName)
Me.Controls.Add(Me.lstNames)
Me.Menu = Me.MainMenu1
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region


Structure member
Dim name As String
Dim phone As String
End Structure

Dim memberarray As New ArrayList
Dim membership As member
Dim fv As StreamReader = File.OpenText("memberphones.txt")

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
memberarray = read_file(memberarray)
list_members(memberarray)
fv.Close()

End Sub
Private Function read_file(ByVal array As ArrayList) As ArrayList
Dim icounter As Integer 'iterates loop
Do
membership.name = fv.ReadLine
membership.phone = fv.ReadLine
array.Add(membership)
icounter = icounter + 1
Loop Until fv.Peek = -1
Return array
End Function
Private Sub list_members(ByVal memberarray As ArrayList)
Dim member_counter As Integer
lstNames.Items.Clear()
For Each membership In memberarray
lstNames.Items.Add(membership.name)
Next
End Sub

Private Sub lstNames_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
lstNames.SelectedIndexChanged
Dim x As member
For Each membership In memberarray
If membership.name.ToString() = lstNames.Items
(lstNames.SelectedIndex).ToString() Then
txtName.Text = membership.name
txtPhone.Text = membership.phone
End If
Next
End Sub

Private Sub mnuAdd_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuAdd.Click
Dim fr As StreamWriter = File.AppendText("memberphones.txt")
membership.name = txtName.Text
membership.phone = txtPhone.Text
fr.WriteLine(membership.name)
fr.WriteLine(membership.phone)
fr.Close()
list_members(memberarray)

End Sub
Private Sub mnuDelete_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles mnuDelete.Click

memberarray.RemoveAt(lstNames.SelectedIndex)
list_members(memberarray)

End Sub
Private Sub mnuModify_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles mnuModify.Click

index = lstNames.SelectedIndex

membership.name = txtName.Text
membership.phone = txtPhone.Text
************************************************** **********************
CType(memberarray(lstnames.items(lstnames.selected index),
member).phone = txtPhone.Text
I have tried every different derivitive of this and nothing seems to
work, Is there something I am doing wrong? Or is there something else I
shoud try??

JOhn

Nov 20 '05 #2

P: n/a
William:
So you are saying is that this can't be done?

Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.