Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

VB.NET Using Instance of Classes across forms

Posted on 2015-01-09
3
Medium Priority
?
120 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 34

Accepted Solution

by:
it_saige earned 2000 total points
ID: 40541017
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
ID: 40541056
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 34

Expert Comment

by:it_saige
ID: 40541059
Not a problem.

-saige-
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

648 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