isual Basic offers many methods for sharing data within an application. When we talk about sharing data, there are several possible levels of sharing:
- Within a procedure
- Within a form/code module/class
- Within an application
- Between applications
Sharing data within a procedure is easy using a variable. Let's say that you have a value, such as a factorial (although I've never, ever
used a factorial in a real application, it's still a good example, because it's a time-consuming operation) that you don't want to calculate over and over again. The item might also be a long string that you "stitched" together using converted numbers and chopped strings. You don't want to repeat the code all the way through your subroutine, so, in either case, you should create a local variable for the subroutine using a simple Dim statement at the top of the routine. Then, store your calculation in the variable and use it throughout the subroutine.
The next level of sharing is within a form, code module, or class. Sharing data within a class is extremely common, especially if you have a class that uses property procedures to access and modify data. You'll typically have quite a few code constructs that look something like this:
Private m_intCustomerID As Long
Public Property Get CustomerID() As Long
CustomerID = m_intCustomerID
Public Property Let CustomerID(vData As Long)
m_intCustomerID = vData
This is a perfect example of sharing data. The m_intCustomerID variable is declared in the declarations section of the module, and is therefore available to all code within the module. In this case, the variable is declared as Private, which means that it is inaccessible outside of the code module.
Sharing data between modules is an easy step up from the previous example. You can either make the variable Public, and have it available to all other modules, or you can create property procedures, as in the previous example. For code modules, you only have the option to declare the variable as Public, which shares it globally with the entire application.
To share data between applications, you'll have to employ more complicated methods. But one easy method is to write the data to a file. You do this all the time and probably don't even think about it. Your e-mail program, such as Eudora, receives e-mail with attachments. These attachments were created in some application on another computer and were transferred to your computer. You then open the file, and go about your business. This is a perfect example of sharing data between applications. It's not exactly the fastest solution, but it is sharing.
A faster way to share data, typically on the same machine, is through the Windows Registry. Windows writes most of its important data to the Registry, and your application can easily access the Registry to get common information. Need to know which Web browser to start? There's a Registry key for it. Need the default user name? Check the Registry.
Another way to share data is through a database. Databases are probably the most common way to share data between applications, especially applications running on different machines. All machines will usually have access to the database server to read, write, and make changes at the same time as other machines. This makes the data consistent and allows you to access it quickly.
In closing, there are many ways to share data. The answer to the question, "How do I know which method is right for me?" depends on the scope of the data sharing, and the timeliness required. Hopefully, this Solution has provided you with enough options to make an informed decision regarding which method to use.