Link to home
Start Free TrialLog in
Avatar of slightlyoff
slightlyoff

asked on

VB.NET Using Instance of Classes across forms

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!!!
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of slightlyoff
slightlyoff

ASKER

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!
Not a problem.

-saige-