Solved

Define Public Variable Correctly

Posted on 2012-03-21
1
232 Views
Last Modified: 2012-03-25
I just finished integrating Motorola's SDK into my vb.net 2010 project.  The SDK depends on a driver to be installed on the computer which maintains the interaction between the vb.net program and the scanner.  My problem is that this program is used by people who will not be using a scanner, so now the program is throwing an error because the referenced object is not present on the computer.  I assume the variable definition of the scanner classs is where it causes the problem.  Is there a way to catch the error thrown by this since it's outside of a sub or function?  Thanks for any help.
0
Comment
Question by:rcblevins
1 Comment
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 37750780
If you defined your variable as a "global" variable, a Public variable in a module, I do not think that you can do that.

Personnally, I would design my application in another way.

If you know beforehand which users have a scanner and which don't, you could use conditional compilation or partial classes to create 2 versions of the application.

As a simple introduction, let's say that conditional compilation has portions of code compiling according to a configuration that you add to the Release/Debug configurations from which you can select between 2 modes of compiling. You could add a third and end up with code like the following:
#If NoScanner
    Public ScannerVariable As ScannerClass
#End If

#If NoScanner
   mnuScan.Visible=False
#End If¨

Open in new window

Just by switching your configuration between Release and NoScanner, you would end up with 2 different executables from the same source code. One for some user, another one for others.

Another approach would be to use partial classes. This is a technique that enables you to split a class (a form is a class) in many files. Your classes and forms would end up splitted in 2 files having the following structure:

File 1:
Class YourClass
   'Code available to everybody
End Class

Open in new window

File 2:
Partial Class YourClass
   'Code available only to the users who have a scanner
End Class

Open in new window

You would have 2 projects. One with only File 1, for users who do not have a scanner. The other with all the files, for users who have a scanner. Each compilation would end up generating an application for a specific type or user.

If you do not know beforehand which users have the scanner libraries installed or not, then I would put all the code that deals with the scanners in a dll that would be distributed to everybody. Your application would reference that dll, not the scanner dll. It would thus be able to start even if the scanner classes are not available.

When the application launch, detect the presence of the scanner software, by looking at the presence of the necessary files, registry information or any mean available. If it is not present, then do not instantiate any of the classes in your library. It won't be loaded in memory and won't trigger the error you have now. You will then also be able to deactivate or make invisible any interface option dealing with the scanner.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

707 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

16 Experts available now in Live!

Get 1:1 Help Now