Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Mastering UML with Rational Rose 2002.pdf
Скачиваний:
137
Добавлен:
02.05.2014
Размер:
9.68 Mб
Скачать

Chapter 15: Visual Basic Code Generation and Reverse Engineering

ErrorHandling adds error−handling code to the methods of the class. The default setting is False.

Note You cannot generate a DHTML page that is assigned to a component with a Standard EXE stereotype. Map the DHTML page to another component (ActiveX EXE, for example) before generating code.

Attributes

Aside from the class itself, Rose will generate the attributes for the class. For each attribute, Rose will include:

Visibility (public or private only; protected visibility is not allowed)

Data type

Default value

Get operation (optional)

Set operation (optional)

Let operation (optional)

For a given attribute, Rose will generate code similar to the following:

Option Explicit

'##ModelId=39601FD70178

Private mPrivateAttribute As Integer

'##ModelId=39602033026A

Public Property Get PrivateAttribute() As Integer PrivateAttribute = mPrivateAttribute

End Property

'##ModelId=396020340059

Public Property Let PrivateAttribute(ByVal vNewValue As Integer) mPrivateAttribute = vNewValue

End Property

You can control whether the Get, Let, and Set operations are generated by using the Model Assistant for the class. In the Properties portion of the treeview, find the appropriate attribute and check the Get, Let, or Set method check boxes under the property to generate those methods. You can also change the attribute visibility and data type using the Model Assistant.

An attribute with a stereotype of Const will be generated as a Visual Basic constant. Use the Initial Value field in the attribute specifications to set the value of the constant. In this example, we created a constant called DaysInWeek with an initial value of 7. The following line of code was generated:

Private Const DaysInWeek As Integer = 7

530

Chapter 15: Visual Basic Code Generation and Reverse Engineering

By default, Rose will prefix the attribute name with an "m" when generating code. To change the prefix used, select Tools → Visual Basic → Properties, and edit the Data Member Prefix field.

Operations

Rose generates code for each of the operations in the class. For each operation, the generated code includes the operation name, the parameters, the parameter data types, and the return type. Each operation will generate code similar to the following:

'This is documentation entered through Rose for the PublicOperation method.

'

'##ModelId=3960239403E1

Public Function PublicOperation(Argument1 As Integer) As Boolean

End Function

As you can see, the full operation signature is generated in the code. Any documentation you enter for the operation is also generated as a comment in the code. If you enter information for the operation protocol, qualifications, exceptions, time, space, preconditions, semantics, or postconditions, this information will not be included in the generated code. Once you have generated the code, you insert the implementation code for each operation.

As with other model elements, you can control the code generated for an operation by modifying its code−generation properties. For example, you can create static functions by modifying the IsStatic property. The code−generation properties for operations are listed earlier in this chapter, in Table 15.3, for your reference.

Bidirectional Associations

To support bidirectional associations, Rose will generate attributes in the code. Each of the classes in the relationship will contain an attribute to support the association. Unlike Visual C++, Rose Visual Basic does not require that you assign role names in order to generate attributes to support the relationship. However, if you do not assign role names, the attributes will be called NewProperty. If you do assign role names, Rose will use the role names as the names of the generated attributes. For example, let's look at the code generated for the following:

Here is the Visual Basic code generated for these two classes. First, let's look at the Passenger class:

Option Explicit

'##ModelId=39614F5402F7 Private FirstName As String

'##ModelId=39614F5702B5 Private LastName As String

531

Chapter 15: Visual Basic Code Generation and Reverse Engineering

'##ModelId=39614F5A01F1 Private Address As String

'##ModelId=39614F5D0250 Private City As String

'##ModelId=39614F5F01EE Private State As String

'##ModelId=39614F610179 Private Zip As Long

'##ModelId=39614F6B00DD Private Phone As String

'##ModelId=39614F990350

Public Account As FrequentFlyerAccount

Now let's look at the FrequentFlyerAccount class:

Option Explicit

'##ModelId=39614F7300C1 Private AccountID As int

'##ModelId=39614F7503A9 Private NumberOfMiles As int

'##ModelId=39614F7A02C0 Private DateEstablished As Date

'##ModelId=39614F99035A

Public AccountHolder As Passenger

'##ModelId=39614F8902FD

Public Function UseMiles(Miles As int) As int

End Function

'##ModelId=39614F840089

Public Function AddMiles(Miles As int) As int

End Function

'##ModelId=39614F7300C1 Private AccountID As int

'##ModelId=39614F7503A9 Private NumberOfMiles As int

'##ModelId=39614F8902FD

Public Function UseMiles(Miles As int) As int

End Function

'##ModelId=39614F840089

Public Function AddMiles(Miles As int) As int

End Function

532

Chapter 15: Visual Basic Code Generation and Reverse Engineering

As you can see, Rose will automatically generate attributes on both sides of the bidirectional association relationship. With the AccountHolder attribute, FrequentFlyerAccount can easily access Passenger. Using the Account attribute, Passenger can easily access FrequentFlyerAccount.

Tip Remember that if you supply no role names, the default (and less useful) attribute name NewProperty will be used. If you supply a role name, that role name will be used as the attribute name.

In the Model Assistant, you can elect to generate Get and Set methods for the attribute generated to support the relationship:

Rose will create the methods and then code them for you:

'##ModelId=396152BE0268

Public Property Get Account() As FrequentFlyerAccount Set Account = mAccount

End Property

'##ModelId=396152C00333

Public Property Set Account(ByVal vNewValue As FrequentFlyerAccount) Set mAccount = vNewValue

End Property

Note that this association has a multiplicity of one to one. See below for a discussion of how other multiplicity settings will affect code generation.

Unidirectional Associations

As with bidirectional associations, Rose will generate attributes to support unidirectional associations. With a unidirectional association, however, an attribute is generated only at one end of the relationship.

533