Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 241
  • Last Modified:

Define Public Variable Correctly

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
rcblevins
Asked:
rcblevins
1 Solution
 
Jacques Bourgeois (James Burger)Commented:
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now