NET Common Type System and the relationship of. Because of this, we’ll begin by briefly discussing the. NET.Īs we’ll see, Visual Basic’s intrinsic data types directly correspond to particular. Instead, Object is the universal data type in Visual Basic. In addition, the Variant type, the “universal type” of COM-based Visual Basic programming, is no longer supported. The Currency type has been eliminated the Decimal type should be used instead. NET supports all but two of the scalar data types supported by COM-based versions of Visual Basic. Table 2-1 lists the data types supported by Visual Basic and shows which are value types and which are reference types. When we assigned studentVal1 to studentVal2, we assigned studentVal2 the values of the first structure, not a reference to a single set of data. That’s because studentVal1 and studentVal2 are instances of the StudentVal structure, a value type. In contrast, studentVal1.GPA remains unchanged. That’s because studentRef1 and studentRef2 are expressions that both refer to the same instance of the StudentRef class.
VISUAL BASIC DATA TYPES CODE
Note that studentRef1.GPA has changed even though it was studentRef2.GPA that our code modified. Then the GPAs of StudentRef1 and StudentVal1 are displayed to the console. The GPA fields of both studentRef2 and studentVal2 are next incremented by two. The value of studentRef1 is then assigned to a new class instance, studentRef2, and the value of studentVal1 is assigned to a new structure instance, studentVal2.
The Main procedure creates an instance of the class, named studentRef1, and an instance of the structure, named studentVal1. Public Structure StudentVal Public Name As String Public GPA As Double End Structure Public Class Test Public Shared Sub Main() Dim studentRef1 As New StudentRef() Dim studentVal1 As New StudentVal() studentRef1.Name = "Jill" studentRef1.GPA = 92.3 studentVal1.Name = "Jill" studentVal1.GPA = 92.3 Dim studentRef2 As StudentRef = studentRef1 Dim studentVal2 As StudentVal = studentVal1 studentRef2.GPA += 2 studentVal2.GPA += 2 Console.WriteLine("", studentVal1.Name, studentVal1.GPA) End Sub End Class Visual Basic 2005: The Complete Reference Visual Basic 2005: The Complete Reference / Petrusha / 226033-5 / Chapter 2 P:\010Comp\ComRef\033-5\ch02.vp Sunday, J1:54:16 PMĬolor profile: Generic CMYK printer profile Composite Default screen Complete Reference / Public Class StudentRef Public Name As String Public GPA As Double End Class To appreciate the difference, consider the following very simple code, which defines a StudentRef class (a reference type) and a StudentVal structure (a value type) to store the name and grade point average of a student, and then creates instances of both: L 2-1 NET Common Language Infrastructure (CLI) manipulates value types by value and reference types by reference. Although we will discuss the difference between value types and reference types in greater detail later in this chapter, suffice it to say that the. Structured data types are derived from the System.ValueType class, which in turn is derived from the System.Object class. Reference types are derived from System.Object. All data types are inherited from either of two basic types in the. Visual Basic 2005 supports two basic kinds of scalar or primitive data types: structured data types and reference data types. Scalar or primitive types are types that contain a single value.
(For these rules, see Appendix F.) Of course, every value of another type cannot always be converted to a value of the type one of the more common rules in this case is to throw an InvalidCastException, indicating that conversion is not possible. A set of rules to convert every value not in the type into a value in the type.An individual type consists of the following two items: NET Common Language Runtime (CLR), is a type system. Visual Basic Data Types At the center of any development or runtime environment, including Visual Basic and the. The chapter will conclude with a discussion of boxing and unboxing (that is, of converting between value types and reference types). It will then examine the ways in which variables are declared in Visual Basic and discuss variable scope, visibility, and lifetime. His chapter will begin by examining the intrinsic data types supported by Visual Basic and relating them to their corresponding types available in the. Visual Basic 2005: The Complete Reference / Petrusha / 226033-5 / Chapter 2
Color profile: Generic CMYK printer profile Composite Default screen Complete Reference