How to: Create a Nullable Type (Class Designer)
Certain value types do not always have (or need) a defined value. This is common practice in databases, where some fields might not be assigned any value. For example, you might assign a null value to a database field to signify that it has not yet been assigned a value.
A nullable type is a value type that you extend so that it takes the typical range of values for that type and also a null value. For example, a nullable of
Int32, also denoted as Nullable<Int32>, can be assigned any value from -2147483648 to 2147483647, or it can be assigned a null value. A Nullable<bool> can be assigned the values
False, or null (no value at all).
Nullable types are instances of the Nullable<T> structure. Each instance of a nullable type has two public read-only properties,
HasValueis of type
booland indicates whether the variable contains a defined value.
Truemeans that the variable contains a non-null value. You can test for a defined value by using a statement such as
if (y != null).
Valueis of the same type as the underlying type. If
Valuecontains a meaningful value. If
Valuewill throw an invalid operation exception.
By default, when you declare a variable as a nullable type, it has no defined value (
False), other than the default value of its underlying value type.
Class Designer displays a nullable type just as it displays its underlying type.
Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Personalizing the IDE.
To add a nullable type by using the Class Designer
In the Class Diagram, expand an existing class or create a new class.
To add a class to the project, on the Class Diagram menu, click Add, and then click Add Class.
To expand the class shape, on the Class Diagram menu, click Expand.
Select the class shape. On the Class Diagram menu, click Add, and then click Field. A new field that has the default name Field will appear in the class shape and also in the Class Details window.
In the Name column of the Class Details window (or in the class shape itself), change the name of the new field to a valid and meaningful name.
In the Type column of the Class Details window, declare the type as a nullable type, as shown in the following code:
To add a nullable type by using the Code Editor
Add a class to the project. Select the project node in Solution Explorer, and, on the Project menu, click Add Class.
In the .cs or .vb file for the new class, add one or more nullable types in the new class to the class declaration.
From Class View, drag the new class icon to the Class Designer design surface. A class shape appears in the class diagram.
Expand the details for the class shape and move the mouse pointer over the class members. The tooltip displays the declaration of each member.
Right-click the class shape and click Class Details. You can view or modify the new type's properties in the Class Details window.