Solved

VB.NET SerialPort used for multiple forms

Posted on 2014-09-29
6
818 Views
Last Modified: 2014-10-05
I have a program that needs to use a serial port in VB.NET 2013.
When the program starts I have a splash screen that loads and when the user clicks the "Proceed" button the splash screen is hidden (me.hide) and the user is presented with an "Options" form. The two options are "Run Test" and "System Check". Each of these launches a different form and both forms call the same module that communicate with hardware on the serial port.
In this module I need to refer to the serial port. What I need to know is, can I put the serial port (named "serialport1") on the splash screen and then in the module, when I need to call the serial port, simply say: frmSplash.serialport1 . . .
instead of putting separate serial ports on each form and then modifying the module to call a different serial port depending on which form is being used?

Thanks,
Charlie
0
Comment
Question by:charlieb01
6 Comments
 
LVL 32

Expert Comment

by:ste5an
ID: 40352344
Abstraction is your friend (OOP).

The two options are "Run Test" and "System Check". Each of these launches a different form and both forms call the same module that communicate with hardware on the serial port.

Just inject the serial port into your module. Thus each of your forms has a separate serial port, put the concrete used one is injected, e.g. by using the constructor, into your module.

The rest depends on your concrete code.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40352365
>>putting separate serial ports on each form

Are you talking about putting the serial port as a control onto the form (like you do for example a button) ?
0
 

Author Comment

by:charlieb01
ID: 40352596
Andy, Yes, I am talking about putting the serial port on the form. This is the way we always did it in VB6 days and the serial port is a windows form control.

Is there another way to do this?
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40352799
>>can I put the serial port (named "serialport1") on the splash screen and then in the module, when I need to call the serial port, simply say: frmSplash.serialport1 . . .

Yes, that should work as you only hide the splash screen.  I assume the frmSplash is a reference to the splash screen that is open and hidden.
0
 
LVL 16

Accepted Solution

by:
hjgode earned 500 total points
ID: 40352808
Hello

instead of dropping the serialport control onto a form you can also create an instance of the serialport class.

But finally there is no difference.

To be able to access the serialport object from other forms/code, the referrer ( another form for example) needs a reference to the 'global' object. If you place the serialport on form A and want to access that from Form B or Module X, the serialport in Form A must be public  and the referrer needs a ref to Form A to be able to access the object. In example in Form B you need to write FormA.SerialPort1 to get the object.

Instead of placing the 'global' object into a Form it seems better to move it to a global module where all code can access this without a reference to a form. Using a module makes it also easier to maintain a single instance of this  SerialPort. It results in a better pattern and practice and you can easier avoid multiple instances for the same port (which will not work).
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 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

19 Experts available now in Live!

Get 1:1 Help Now