?
Solved

Define Public Variable Correctly

Posted on 2012-03-21
1
Medium Priority
?
239 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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 …
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

764 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