Results 1 to 7 of 7

Thread: [RESOLVED] WHere to Declare a class to share b/w two forms

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,158

    Resolved [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

  2. #2
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    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.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,158

    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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    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
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: WHere to Declare a class to share b/w two forms

    Quote Originally Posted by Jenner View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    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.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width