Sharing and updating an Array from multiple forms

jhicks703

New member
Joined
Jan 14, 2008
Messages
1
Programming Experience
1-3
Hello,

I am trying to create a program for use at my job that will track the time each employee spends on particular jobs we process.

I have 2 forms, form1 and form2. form1 takes input from the employee (name, job#, process) stores them in a structure 'Session' and then writes 'Session' to the array called 'Sessions'. When the form1's submit button is clicked it launches form2 which will display the name, job#, process, and start/end time and should eventually write the information to an excel file when the employee pushes stop. However, I am having trouble accessing any of the information stored in Sessions from form2. Am I storing it in the array incorrectly? or do i need to declare the array differently?

Here is the code I have from form1, any help is appreciated, thanks in advance



VB.NET:
Public Class Form1

'declaring a structure named Session
  Structure Session
         Dim sName As String
         Dim sJob As String
         Dim sProcess As String
         Dim sIndex As Integer
         Dim sStart As DateTime
         Dim sEnd As DateTime
  End Structure

  Public Shared Sessions(30) As Session
  Public Shared index As Integer = 0

  Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click

       Sessions(index).sName = txtName.Text
       Sessions(index).sJob = txtJob.Text
       Sessions(index).sProcess = ddProcess.SelectedIndex.ToString
       Sessions(index).sStart = Now
       Sessions(index).sIndex = index
       index = index + 1

       Dim f2 As New Form2()
       f2.Show()
  End Sub
End Class
 
I would suggest making quite a few changes to that. First of all, declare your Session structure in its own file if you haven't already. Also, what is the "s" prefix on each field supposed to signify? If you're using Hungarian notation then surely each type should have a different prefix. I'd suggest getting rid of all the prefixes regardless. Do any of the types in the .NET Framework have prefixes on the names? Are they any harder to use as a result?
VB.NET:
Public Structure Session
    Public Name As String
    Public Job As String
    Public Process As String
    Public Index As Integer
    Public Start As DateTime
    Public [End] As DateTime
End Structure
Secondly, I'd be using a collection rather than an array. That way you never have to worry about keeping track of the last index. The collection will simply grow automatically by one each time you add a new item. Also, the collection should NOT be declared Shared. There is absolutely no reason for that.
VB.NET:
Private sessions As New List(Of Session)
Now, if Form2 needs a list of Session objects then you should be explicitly passing it a list of Session objects. It shouldn't have to retrieve them itself. Add a constructor to your Form2 class that has a List(Of Session) as a parameter.
VB.NET:
Public Class Form2

    Private sessions As List(Of Session)

    Public Sub New(ByVal sessions As List(Of Session))
        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        Me.sessions = sessions
    End Sub

End Class
Now you will not be able to create a Form2 object without passing it a List(Of Session).
VB.NET:
Public Class Form1

    Private sessions As New List(Of Session)

    Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Dim s As Session

        s.Name = txtName.Text
        s.Job = txtJob.Text
        s.Process = ddProcess.SelectedIndex.ToString
        s.Start = Date.Now
        s.Index = Me.sessions.Count

        Me.sessions.Add(s)

        Dim f2 As New Form2(Me.sessions)
        f2.Show()
    End Sub

End Class
I'm not 100% sure but I'm guessing that you probably should be calling ShowDialog there at the end rather than Show too.
VB.NET:
Using f2 As New Form2(Me.sessions)
    f2.ShowDialog()
End Using
 
Back
Top