4.1.1.1 - Data types and references (VB.NET)
Learn how a type determines the values and operations a program can use, then represent related data and distinguish copying a value from sharing an object. This route uses native written code and is incomplete: runnable support pending.
Built-in types and valid operations
A data type defines the kind of value a variable may hold and the operations that make sense for it. An age and a name are both data, but adding two ages has a different meaning from joining two names. Explicit declarations let the compiler detect many mismatches before the program starts.
Data type
A classification of data that determines its possible values and permitted operations.
In VB.NET, Dim declares a variable and As specifies its type. The expression after = supplies its initial value. The following statements are fragments inside a Sub Main procedure; the enclosing program is shown in the next lesson.
Dim attempts As Integer = 3
Dim speed As Double = 2.75
Dim doorOpen As Boolean = False
Dim grade As Char = "B"c
Dim candidateName As String = "Nadia"
Dim appointment As Date = New DateTime(2027, 6, 8, 9, 30, 0)
| Required kind of data | VB.NET example | Appropriate use |
|---|---|---|
| Integer | Integer | Whole-number counts, including negative values where meaningful |
| Real/float | Double | Fractional measurements; many decimal fractions are stored approximately |
| Boolean | Boolean | Exactly True or False, such as whether a condition is satisfied |
| Character | Char | One character; the c suffix makes "B"c a character literal |
| String | String | A sequence of characters, including an empty string |
| Date/time | Date (System.DateTime) | A calendar date and time, supporting date comparisons and date arithmetic |
"37" is text, whereas 37 is an integer. Keeping a postcode or telephone number as a string preserves leading zeroes and avoids meaningless arithmetic. Double suits measured distances; Decimal is another built-in numeric type useful when exact decimal arithmetic, such as money, matters. This is a choice of representation, not a requirement to memorise every .NET type.
Option Strict On at the top of a source file rejects many implicit conversions that could lose information. Convert explicitly when the meaning is deliberate. An integer is not an unlimited mathematical integer: Integer is a signed 32-bit value. Choose a suitable type and consider its range.
References and dynamic objects
For a reference type, a variable holds a reference to an object rather than containing a separate copy of all that object's data. A reference identifies where an object can be accessed; the specification describes storing its memory address. Managed VB.NET normally hides the numerical address and manages memory for you. Understanding references does not require unsafe pointer arithmetic.
Arrays and class instances are reference types. New creates an object during execution, so objects can be allocated dynamically when they are needed. Assigning an array variable to another array variable copies the reference: both names then refer to the same array.
Dim first() As Integer = {4, 7}
Dim second() As Integer = first
second(0) = 9
Dim count As Integer = 4
Dim copy As Integer = count
copy = 9
After these statements, both first(0) and second(0) are 9. There is one array with two references. In contrast, count remains 4 and copy is 9: Integer is a value type, so the assignment copies its value. A reference may be Nothing, meaning it does not identify an object; accessing a member through such a reference is an error.
Continue this lesson.
You’re previewing NovaLearn without an account. Create a free account to read the rest.
Create free account