30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Appendix G COM Integration 1339<br />

Performance Tip G.1<br />

The .<strong>NET</strong> and COM architectures are fundamentally different in terms of memory management<br />

and object representation. Method calls <strong>to</strong> COM objects can degrade program performance,<br />

because the RCW must convert all data types between the managed (.<strong>NET</strong>) context<br />

and the unmanaged (COM) context. G.1<br />

When we instantiate a COM object in <strong>Visual</strong> <strong>Basic</strong>, we are actually creating a new<br />

instance of the object’s RCW. The communication between the RCW and the COM component<br />

is entirely transparent, enabling the .<strong>NET</strong> developer <strong>to</strong> interact with the COM object<br />

as if it were a .<strong>NET</strong> object.<br />

We created an application (Fig. G.6) demonstrating how <strong>to</strong> use the Simple Addition<br />

DLL that we want <strong>to</strong> integrate in<strong>to</strong> .<strong>NET</strong>. This program includes three text boxes and a but<strong>to</strong>n.<br />

After entering an Integer in<strong>to</strong> each of the first two text boxes, the user clicks the Calculate<br />

but<strong>to</strong>n, and the program displays the sum of the two Integers in the third text box.<br />

1 ' Fig. G.6 : Addition.vb<br />

2 ' COM component integration example.<br />

3<br />

4 Imports System.Windows.Forms<br />

5<br />

6 ' adds two numbers entered in text boxes<br />

7 Public Class CAddition<br />

8 Inherits System.Windows.Forms.Form<br />

9<br />

10 Dim firstInteger As Integer<br />

11 Dim secondInteger As Integer<br />

12 Dim <strong>to</strong>tal As Integer<br />

13 Dim additionObject As Deitel_DLL.CAddition = _<br />

14 New Deitel_DLL.CAddition()<br />

15<br />

16 Friend WithEvents lblDisplay1 As Label<br />

17 Friend WithEvents lblDisplay2 As Label<br />

18<br />

19 Friend WithEvents txtFirstBox As TextBox ' first integer box<br />

20 Friend WithEvents txtSecondBox As TextBox ' second integer box<br />

21 Friend WithEvents txtResultBox As TextBox ' result text box<br />

22<br />

23 Friend WithEvents cmdCalculate As But<strong>to</strong>n ' calculate value<br />

24<br />

25 ' <strong>Visual</strong> Studio .<strong>NET</strong> generated code.<br />

26<br />

27 ' event handler that enables cmdCalculate<br />

28 Private Sub txtSecondBox_TextChanged( _<br />

29 ByVal sender As System.Object, _<br />

30 ByVal e As System.EventArgs) _<br />

31 Handles txtSecondBox.TextChanged, _<br />

32 txtFirstBox.TextChanged<br />

33<br />

34 ' enable but<strong>to</strong>n if text boxes contain text<br />

35 If (txtFirstBox.Text "" AndAlso _<br />

36 txtSecondBox.Text "") Then<br />

Fig. G.6 COM DLL component in <strong>Visual</strong> <strong>Basic</strong>.<strong>NET</strong> (part 1 of 2).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!