Solved

VB.NET Using Instance of Classes across forms

Posted on 2015-01-09
3
106 Views
Last Modified: 2015-01-09
I have an application with a Login form.
Right now, I have:
Public  EmpID as Integer 

Open in new window

setting the value in a module called Settings.vb.

When the user logs in, the Employee ID is grabbed from the database and assigned to EmpID.  I can then use that variable in any form in my application.  The issue is, maybe I need the Employee's name or birthday or security level.  Each of those would need to be set in the module as a variable and would be assigned at login time - or I would take the EmpID and a new call would need to be made to the database on the forms that need the extra data.

It seems to me, an easier way would be to create a class called Employee.  It would have all the properties of an Employee, and maybe methods such as ClockIn and ClockOut.

An instance of the Employee class could be instantiated with the Login form and destroyed upon logout or application exit.

However, I don't know how to do this and keep the instance in scope across all the forms.  I mean, I suppose I could use this on frmLogin.vb:

Public CurrentEmp as New Employee(EmpID)

Open in new window


and then refer to it as frmLogin.CurrentEmp in other forms, but it seems like there should be a better way.
It would be nice just to refer to the instance as CurrentEmp.  Also, if the login form is closed after the login is complete, the use of the instance goes away, doesn't it?

I hope this question makes sense.  

Thanks for the help!!!
0
Comment
Question by:slightlyoff
  • 2
3 Comments
 
LVL 32

Accepted Solution

by:
it_saige earned 500 total points
Comment Utility
These types of variables are call static (in C#) or Shared (in VB.NET).  If your settings module (I assume it is a module) is good enough for the EmpID, there is no reason to believe that it cannot be good enough for an Employee class implementation.  Consider the following:Capture.JPGLogin.vb -
Public Class Login
	Private Sub OnClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
		If Not String.IsNullOrEmpty(tbName.Text) Then
			Settings.Employee = New Employee() With {.Name = tbName.Text}
			Dim frmMain As MainForm = New MainForm()
			frmMain.Show()
		End If
	End Sub
End Class

Open in new window

Login.Designer.vb -
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Login
	Inherits System.Windows.Forms.Form

	'Form overrides dispose to clean up the component list.
	<System.Diagnostics.DebuggerNonUserCode()> _
	Protected Overrides Sub Dispose(ByVal disposing As Boolean)
		Try
			If disposing AndAlso components IsNot Nothing Then
				components.Dispose()
			End If
		Finally
			MyBase.Dispose(disposing)
		End Try
	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.
	<System.Diagnostics.DebuggerStepThrough()> _
	Private Sub InitializeComponent()
		Me.Label1 = New System.Windows.Forms.Label()
		Me.tbName = New System.Windows.Forms.TextBox()
		Me.btnLogin = New System.Windows.Forms.Button()
		Me.SuspendLayout()
		'
		'Label1
		'
		Me.Label1.AutoSize = True
		Me.Label1.Location = New System.Drawing.Point(13, 13)
		Me.Label1.Name = "Label1"
		Me.Label1.Size = New System.Drawing.Size(38, 13)
		Me.Label1.TabIndex = 0
		Me.Label1.Text = "Name:"
		'
		'tbName
		'
		Me.tbName.Location = New System.Drawing.Point(57, 10)
		Me.tbName.Name = "tbName"
		Me.tbName.Size = New System.Drawing.Size(100, 20)
		Me.tbName.TabIndex = 1
		'
		'btnLogin
		'
		Me.btnLogin.Location = New System.Drawing.Point(82, 36)
		Me.btnLogin.Name = "btnLogin"
		Me.btnLogin.Size = New System.Drawing.Size(75, 23)
		Me.btnLogin.TabIndex = 2
		Me.btnLogin.Text = "Login"
		Me.btnLogin.UseVisualStyleBackColor = True
		'
		'Login
		'
		Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
		Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
		Me.ClientSize = New System.Drawing.Size(164, 64)
		Me.Controls.Add(Me.btnLogin)
		Me.Controls.Add(Me.tbName)
		Me.Controls.Add(Me.Label1)
		Me.Name = "Login"
		Me.Text = "Login"
		Me.ResumeLayout(False)
		Me.PerformLayout()

	End Sub
	Friend WithEvents Label1 As System.Windows.Forms.Label
	Friend WithEvents tbName As System.Windows.Forms.TextBox
	Friend WithEvents btnLogin As System.Windows.Forms.Button

End Class

Open in new window

MainForm.vb -
Public Class MainForm
	Private Sub OnClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClockOut.Click, btnClockIn.Click
		If TypeOf sender Is Button Then
			Dim btn As Button = DirectCast(sender, Button)
			If btn.Equals(btnClockIn) Then
				Settings.Employee.ClockIn = DateTime.Now()
				tbClockIn.Text = Settings.Employee.ClockIn.ToShortTimeString
			ElseIf btn.Equals(btnClockOut) Then
				Settings.Employee.ClockOut = DateTime.Now()
				tbClockOut.Text = Settings.Employee.ClockOut.ToShortTimeString
			End If
		End If
	End Sub

	Private Sub OnLoad(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		tbEmployee.Text = Settings.Employee.Name
	End Sub
End Class

Open in new window

MainForm.Designer.vb -
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class MainForm
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    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.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
		Me.Label1 = New System.Windows.Forms.Label()
		Me.tbEmployee = New System.Windows.Forms.TextBox()
		Me.btnClockIn = New System.Windows.Forms.Button()
		Me.btnClockOut = New System.Windows.Forms.Button()
		Me.tbClockIn = New System.Windows.Forms.TextBox()
		Me.Label2 = New System.Windows.Forms.Label()
		Me.tbClockOut = New System.Windows.Forms.TextBox()
		Me.Label3 = New System.Windows.Forms.Label()
		Me.SuspendLayout()
		'
		'Label1
		'
		Me.Label1.AutoSize = True
		Me.Label1.Location = New System.Drawing.Point(13, 13)
		Me.Label1.Name = "Label1"
		Me.Label1.Size = New System.Drawing.Size(56, 13)
		Me.Label1.TabIndex = 0
		Me.Label1.Text = "Employee:"
		'
		'tbEmployee
		'
		Me.tbEmployee.Location = New System.Drawing.Point(75, 10)
		Me.tbEmployee.Name = "tbEmployee"
		Me.tbEmployee.ReadOnly = True
		Me.tbEmployee.Size = New System.Drawing.Size(100, 20)
		Me.tbEmployee.TabIndex = 1
		'
		'btnClockIn
		'
		Me.btnClockIn.Location = New System.Drawing.Point(19, 88)
		Me.btnClockIn.Name = "btnClockIn"
		Me.btnClockIn.Size = New System.Drawing.Size(75, 23)
		Me.btnClockIn.TabIndex = 2
		Me.btnClockIn.Text = "Clock In"
		Me.btnClockIn.UseVisualStyleBackColor = True
		'
		'btnClockOut
		'
		Me.btnClockOut.Location = New System.Drawing.Point(100, 88)
		Me.btnClockOut.Name = "btnClockOut"
		Me.btnClockOut.Size = New System.Drawing.Size(75, 23)
		Me.btnClockOut.TabIndex = 3
		Me.btnClockOut.Text = "Clock Out"
		Me.btnClockOut.UseVisualStyleBackColor = True
		'
		'tbClockIn
		'
		Me.tbClockIn.Location = New System.Drawing.Point(75, 36)
		Me.tbClockIn.Name = "tbClockIn"
		Me.tbClockIn.ReadOnly = True
		Me.tbClockIn.Size = New System.Drawing.Size(100, 20)
		Me.tbClockIn.TabIndex = 5
		'
		'Label2
		'
		Me.Label2.AutoSize = True
		Me.Label2.Location = New System.Drawing.Point(13, 39)
		Me.Label2.Name = "Label2"
		Me.Label2.Size = New System.Drawing.Size(49, 13)
		Me.Label2.TabIndex = 4
		Me.Label2.Text = "Clock In:"
		'
		'tbClockOut
		'
		Me.tbClockOut.Location = New System.Drawing.Point(75, 62)
		Me.tbClockOut.Name = "tbClockOut"
		Me.tbClockOut.ReadOnly = True
		Me.tbClockOut.Size = New System.Drawing.Size(100, 20)
		Me.tbClockOut.TabIndex = 7
		'
		'Label3
		'
		Me.Label3.AutoSize = True
		Me.Label3.Location = New System.Drawing.Point(13, 65)
		Me.Label3.Name = "Label3"
		Me.Label3.Size = New System.Drawing.Size(57, 13)
		Me.Label3.TabIndex = 6
		Me.Label3.Text = "Clock Out:"
		'
		'MainForm
		'
		Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
		Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
		Me.ClientSize = New System.Drawing.Size(183, 115)
		Me.Controls.Add(Me.tbClockOut)
		Me.Controls.Add(Me.Label3)
		Me.Controls.Add(Me.tbClockIn)
		Me.Controls.Add(Me.Label2)
		Me.Controls.Add(Me.btnClockOut)
		Me.Controls.Add(Me.btnClockIn)
		Me.Controls.Add(Me.tbEmployee)
		Me.Controls.Add(Me.Label1)
		Me.Name = "MainForm"
		Me.Text = "MainForm"
		Me.ResumeLayout(False)
		Me.PerformLayout()

	End Sub
	Friend WithEvents Label1 As System.Windows.Forms.Label
	Friend WithEvents tbEmployee As System.Windows.Forms.TextBox
	Friend WithEvents btnClockIn As System.Windows.Forms.Button
	Friend WithEvents btnClockOut As System.Windows.Forms.Button
	Friend WithEvents tbClockIn As System.Windows.Forms.TextBox
	Friend WithEvents Label2 As System.Windows.Forms.Label
	Friend WithEvents tbClockOut As System.Windows.Forms.TextBox
	Friend WithEvents Label3 As System.Windows.Forms.Label
End Class

Open in new window

Settings.vb -
Module Settings
	Private fEmployee As Employee
	Public Property Employee() As Employee
		Get
			If fEmployee Is Nothing Then fEmployee = New Employee()
			Return fEmployee
		End Get
		Set(ByVal value As Employee)
			If Not value Is Nothing AndAlso Not value.Equals(fEmployee) Then
				fEmployee = value
			End If
		End Set
	End Property
End Module

Open in new window

Employee.vb -
Public Class Employee
	Public Property ID() As Integer
	Public Property Name() As String
	Public Property ClockIn() As DateTime
	Public Property ClockOut() As DateTime
End Class

Open in new window

Produces the following output -Initial application load.  Login form comes up.User enters name and clicks login.Login launces MainForm.  MainForm loads the information from the class.-saige-
0
 
LVL 1

Author Comment

by:slightlyoff
Comment Utility
Thank you for the quick reply!

I might have a follow up question - but this hits the nail on the head!  I appreciate your time!
0
 
LVL 32

Expert Comment

by:it_saige
Comment Utility
Not a problem.

-saige-
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 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

19 Experts available now in Live!

Get 1:1 Help Now