Published on System iNetwork (http://systeminetwork.com)
Share the Data Using AppDomain
By VickiHamende
Created May 20 2008 - 20:15

By:
Craig Pelkie [1]

Many Windows applications are composed of multiple forms. You will frequently need to make data from one form available to others, or you may have a startup process that gets initialization values that will be used throughout the application.

One way to share data is to write out values to database or text files that each form can read. Although that technique has the advantage of letting you persist data after the application ends, it is an expensive process and requires a bit of coding to provide the functionality. Another technique used with Windows forms is to add publicly accessible properties to the form. Using properties, other forms can access the value of the properties as long as the form that owns the properties is not closed.

When you run a .NET application, it runs within a process, which is roughly equivalent to an i5/OS job. Within a process, you can have one or more application domains or AppDomain objects. An AppDomain is similar to an activation group on the System i. Within the AppDomain, you can freely store and retrieve objects that can be shared among the programs that run in the AppDomain.

Here's How It Looks

Figure 1 [2] shows an example of two small Windows forms that share data. Form1 provides two textbox fields that are used to enter values. When you click the Form 2 button, the values are stored in the current application domain for the form, and then Form2 is loaded. When Form2 loads, it accesses the values in the application domain and displays them on the form.

The code for Form1 is shown in Figure 2 [3]. You can see that the ad object is declared as type System.AppDomain and is initialized to the CurrentDomain (this seems to be the equivalent of using the default activation group in ILE). After initializing the ad object, the SetData method is used to provide a name for the value to be stored and the value. You can store any type of object in the AppDomain; in this example, the types of objects that are stored are strings.

When Form2 is loaded, the code shown in Figure 3 [4] runs. As in Form1, you define and initialize an AppDomain object. In this example, the object is also named ad, although it does not need to be named the same as in Form1. After getting the reference to the CurrentDomain, the GetData method is used to retrieve the objects, using the names that were assigned in Form1. Because GetData retrieves an object, you need to cast it to the type of object that you are assigning it to. In this example, the values retrieved from the AppDomain are being assigned to label controls, which display string values, so the ToString method is used to convert the retrieved objects into strings.

As you can see, it is easy to use the AppDomain to store and retrieve values. This can be used to provide a capability similar to the Local Data Area (LDA) on the System i.

Craig Pelkie has worked as a programmer with IBM midrange computers for many years. He has also written and lectured extensively on AS/400 and System i technologies, including client/server programming, Client Access, Java WebSphere, .NET applications for the System i, and web development. You can reach him at craig@web400.com.

Copyright © Penton Media

Source URL: http://systeminetwork.com/article/share-data-using-appdomain

Links:
[1] http://systeminetwork.com/author/craig-pelkie
[2] http://pentontech.com/IBMContent/Images/article/56710_50550_AppDomain_F1.jpg
[3] http://pentontech.com/IBMContent/Documents/article/56710_612_AppDomainForm1_F2.txt
[4] http://pentontech.com/IBMContent/Documents/article/56710_613_AppDomainForm2_F3.txt