The Basics - Generating the VB .NET Code for the Logon Class
(Page 4 of 8 )
Now that you’ve created the skeleton model for the Logon class, it’s time to see the actual code that VEA generates from the model, which is what it’s ultimately all about—generating and maintaining the code for your application. Exercise 1-5 shows how to generate the VB .NET code.
EXERCISE 1-5
- Select the Logon class.
- Select UML -> Code -> Generate. This brings up the Generate dialog box.
- Make sure Visual Basic is selected in the Target language list.
- Select the Logon class in the Select classes for code generation tree view.
- Select the location you want the generated output placed in by typing the path in the Location text box or by browsing to the location after clicking the Browse button. Save the output with this name and path for easy access in later exercises: \EDWVSNETUMLMSF\Chapter 01\VB .NET\ TopPackage\Logon.vb. The Generate dialog box should now look like Figure 1-6. Note that when you select the VB .NET folder, VEA assumes the Top Package folder underneath it.

Figure 1-6. The Generate dialog box
6. Click OK. You might be prompted to create a nonexistent folder.
If so, click Yes.
NOTE The Top Package subfolder comes from the Model Explorer, which is usually shown on the left side of the Visio workspace, as you can see in Figure 1-7. We won’t be covering packages now, but a UML package in VEA resembles a namespace in .NET (that is, a logical grouping).
In Figure 1-6, you can see the code-generation options for the Logon class in the Generate dialog box. Once you click OK in the Generate dialog box, the code is generated and output to the specified location. The Output window shows if any errors were encountered during the code generation.
In Figure 1-7, you can see the Top Package UML package selected in the Model Explorer. You can give your package any name by double-clicking the package. This will bring up the UML Package Properties dialog box.

Figure 1-7. The Model Explorer with Top Package selected
Figure 1-8 shows the Output window after the VB .NET code is generated for the Logon class. In our case, there weren’t any semantic errors.

Figure 1-8. The Output window showing semantic errors after code generation
NOTE Semantic errors are programmatic errors such as incompatible data types. If you pick a data type as the return type for an operation that’s not supported in the target programming language, you’ll see a semantic error in the Output window.
Now let’s take a look at the generated code. Go to the output location you specified in the Generate dialog box and double-click the Logon.vb file. If your VS .NET installation is working properly, and you haven’t specified that VS .NET should open the last loaded solution when opening,3 the code file is opened in the VS .NET IDE, and it should look similar to the one shown in Figure 1-9.

Figure 1-9. The generated VB .NET code for the Logon class
Figure 1-9 shows the generated output for the Logon class from the UML model as VB .NET code. If you’re familiar with VB .NET or even VB, there shouldn’t be any surprises in the generated code. However, we’ll just run through the code listing, pointing out what the various code lines correspond to in the UML model:
- Lines 4 and 15 define the class boundary.
- Lines 6 and 8 are the two class attributes or member variables.
- Lines 10 through 13 are the Logon procedure, declared as a Function, because you specified a return type (Boolean). The two input parameters have been declared with ByVal, which effectively means they’re input parameters, because changes to the values of a parameter declared with ByVal won’t be reflected in the caller of the operation.
TIP To turn on line numbers in Visual Studio, select Tools -> Options -
> Text Editor/Basic and check the Line Numbers option.
We’re aware this isn’t a lot of code, and that for such a small class it would be easier to create it the normal way. However, many useful applications do start out as small, simple, and unplanned applications, don’t they?
Creating the C# Class You’ve created the VB .NET class, and since the content of the C# class is the same, you need to change only a few things in the class model. The differences are the data types you used for the attributes and the operation and the target programming language. Exercise 1-6 demonstrates how to change from VB .NET to C#.
EXERCISE 1-6
- Open the previously saved Logon.vsd UML model in VEA (if it isn’t already open).
- Open the UML Class Properties dialog box for the Logon class.
- Select the Code Generation Options category from the Categories list.
- Select C# from the Target language list.
5. Click OK.
Now you’ve changed the target programming language, but didn’t we just state that you need to change the data types for the new target programming language? Yes, we did. However, the data types chosen for the VB .NET class were common type system (CTS) compatible, meaning they exist in all .NET programming languages. Well, this isn’t quite true, but for data exchange between classes in different .NET languages to work, the exposed data types must be CTS compatible. You used the Boolean and String VB .NET data types, and they certainly have counterparts in C#. Generally, you won’t find a problem when going from VB .NET to C#. A problem is more likely to arise when you go in the other direction—if you select an unsigned data type in C# and want the corresponding data type in VB .NET. VB .NET doesn’t directly handle unsigned data types;4 therefore, VEA won’t be able to convert those data types for you.
If you feel confident that you can generate the C# code for the Logon class now, feel free to jump to the next section. However, for the purpose of having two different models saved in different locations and with the correct data types, follow along with Exercise 1-7.
EXERCISE 1-7
- Open the UML Class Properties dialog box for the Logon class.
- Select the Attributes category from the Categories list.
- Select C#::string from the Type list for the m_userID and m_password attributes.
- Select the Operations category from the Categories list.
- Select C#::bool from the Return Type list for the Logon operation.
- Click the Properties button while the Logon operation is selected to show the UML Operations dialog box.
- Select the Parameters category from the Categories list.
- Select C#::string from the Type list for the userID and password parameters.
9. Click OK twice.
The changes you’ve made to the Logon class should now be reflected on the diagram. Save the new model to this name and path for easy access in later exercises and chapters: \EDWVSNETUMLMSF\Chapter 01\C#\Logon.vsd.
This article is excerpted from Enterprise Development with Visual Studio .NET, UML, and MSF by John Erik Hansen and Carsten Thomsen (Apress, 2004; ISBN 1590590422). Check it out at your favorite bookstore today. Buy this book now. |
Next: Generating the C# Code for the Logon Class >>
More .NET Articles
More By Apress Publishing