Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Reference a Class Object?

Posted on 2014-08-07
5
Medium Priority
?
200 Views
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. 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!
0
Comment
Question by:BlakeMcKenna
  • 2
  • 2
5 Comments
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 1336 total points
ID: 40247983
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
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 664 total points
ID: 40248387
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
 

Author Comment

by:BlakeMcKenna
ID: 40248591
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
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 1336 total points
ID: 40248994
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
 

Author Closing Comment

by:BlakeMcKenna
ID: 40249001
Thanks guys!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?

580 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