|
-
Aug 24th, 2011, 07:36 AM
#1
Thread Starter
Frenzied Member
[RESOLVED] WHere to Declare a class to share b/w two forms
There are two forms, first form list's the employees and the second form is used to add,edit the employee record. So to pass the information between the two forms i want to use the Employee Objects.
To do so, where should i declare the Employee Class
Thanks
-
Aug 24th, 2011, 07:43 AM
#2
Re: WHere to Declare a class to share b/w two forms
Depends on your Form's functionallity. If the Add/Edit form is a Dialog type form to the list form, then I'd declare it in the list form and pass it to the Add/Edit form as needed. That way, when the list form is closed, the object is cleaned up.
If both forms are standard forms and either one can be closed at any time, I'd declare it in a Module since it's visable to both forms. It doesn't get cleaned up when both forms are closed unless you write some extra code, but that's generally not a bad thing either.
-
Aug 24th, 2011, 08:03 AM
#3
Thread Starter
Frenzied Member
Re: WHere to Declare a class to share b/w two forms
My Add/Edit form is an Dialog to the List form, so i have declared the Employee Class in the List form, but i am not able to access that class in the Add/Edit form
-
Aug 24th, 2011, 08:16 AM
#4
Re: WHere to Declare a class to share b/w two forms
First, let's distinguish between declaring the Employee class, declaring a variable of the Employee type and creating an instance of the Employee type, because they are three different things.
Where you should declare the Employee class is in its own code file. In general, all classes should have there own code file. Right-click the project in the Solution Explorer, select Add -> Class, enter the name and click OK.
Now, once the Employee class is declared, you can declare variables of that type. You said that your first form lists all the employees so it must already have a variable of type List(Of Employee) or the like. When you want to edit an employee you get the appropriate Employee object from that list and pass it to the second form, either as a constructor argument or by setting a property. That's it, that's all. The dialogue updates the properties of that Employee object.
When adding a new employee, you'll need to create a new Employee object. You might do that in the first form or the dialogue. It might be convenient to do it in the first form, which will allow the dialogue to behave exactly the same way whether adding or editing. Once the dialogue closes, the first form adds the new item to the list.
-
Aug 24th, 2011, 08:52 AM
#5
Re: WHere to Declare a class to share b/w two forms
Right. Assuming your Employee class is in it's own file and created, you'd instance a new Employee object in the List form:
Code:
Dim emp As New Employee
Then, in your Add/Edit form, make an employee variable and property to access it:
Code:
Private emp As Employee
Public Property Employee As Employee
Get
Return emp
End Get
Set(ByVal value As Employee)
emp = value
End Set
End Property
Finally, in your List form, when you want to pop-up the add/edit dialog:
Code:
Dim f As New frmAddEditEmployee
f.Employee = emp
-
Aug 24th, 2011, 07:24 PM
#6
Re: WHere to Declare a class to share b/w two forms
 Originally Posted by Jenner
you'd instance a new Employee object
"Instance" is a noun, not a verb. The verb is "instantiate". You instantiate a class, which is equivalent to creating an object. The object is the instance of the class. This may be seen as just quibbling over semantics but using incorrect terminology is great way to confuse the less experienced. If you understand the terminology then understanding the process is much easier.
-
Aug 25th, 2011, 08:00 AM
#7
Re: [RESOLVED] WHere to Declare a class to share b/w two forms
Thanks for the correction. You are absolutely right. I've heard the phrase "instance an object" often enough on here that I've become a bit corrupted by it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|