Reference a Class Object?

Posted on 2014-08-07
Last Modified: 2014-08-08
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. 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?

Question by:BlakeMcKenna
    LVL 40

    Assisted Solution

    by:Jacques Bourgeois (James Burger)
    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.
    LVL 83

    Assisted Solution

    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.

    Author Comment


    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!


    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!
    LVL 40

    Accepted Solution

    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.

    Author Closing Comment

    Thanks guys!

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now