Reference a Class Object?

I have often not used classes because I was never sure how to reference them correctly. My scenario is that I have a bunch of forms in my project. In the same project is a "GeneralClass" file that contains many different classes (primarily used to represent Database Tables).

Lets say I instantiate a particular class in form1. I load the various properties (which again represent columns of Data Tables) with data. Then...in form2, I need to be able to take the data from that class and move that data into different controls on form2. What is the best way to do this?

I actually created a new project that contained that "GeneralClass" file. I got to thinking that if I'm not going to use that project in any other solution, I might as well just create that "GeneralClass" file within this project. How far off am I?

Thanks!
BlakeMcKennaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jacques Bourgeois (James Burger)PresidentCommented:
Well, it depends on the flow of your application. Whether Form1 opens before or after Form2. Whether Form2 is itself instantiated from Form1 or not. And as many different flows as there are applications.

The ideal solution is that if Form1 contains the code that instantiate Form2. Then you simply need to create a constructor that accepts a parameter in Form2, something  like Public Sub New(yourObject As yourClass). This will enable you to pass yourClass object as a parameter from Form1 to Form2 when you call New on Form2. If you need to pass many classes, add more parameters. If you need to pass different parameters depending on conditions, create many New subs in Form2 with different types of parameters.

Anyway, you will also want to add a default constructor. Sub New() without parameters. This constructor is called by the Form Designer and is required in order for you to add or remove controls to the form. It is usually created automatically for you at compile time, but it is not if you define a constructor with parameters. You must thus add it manually.
0
CodeCruiserCommented:
Regarding having the class within the same project, you can keep it within the same project but generally, only UI related code is kept in the UI project and as much code as possible is moved into libraries to enable reuse. You may not see any reuse required today but you may have to reuse these classes elsewhere at some point.

It also makes maintenance easier as you can update components individually rather than rebuilding and redistributing the whole application.
0
BlakeMcKennaAuthor Commented:
CodeCruiser,

I know what your saying about resuability. I do try to do that but often catch myself just incorporating "Modules" within the project. It's a lazy thing!

James,

I've never quite understood constructors and was unaware that I needed to pass a class object thru a constructor. I'll play around with this. Know of any good references that clearly explains how to use Constructors?

Thanks guys!
0
Jacques Bourgeois (James Burger)PresidentCommented:
The constructor is what you call when you ask for New on a variable. You have probably seen that many .NET classes enables you to pass a parameter to some of their constructors. Since a Form is a class, this is exactly the same mechanism that comes into play.

You can get information about the concept by going F1 and look for Constructors in the index.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BlakeMcKennaAuthor Commented:
Thanks guys!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.