Solved

Define Public Variable Correctly

Posted on 2012-03-21
1
236 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 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

733 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