Skip to Content
Programming Visual Basic .NET
book

Programming Visual Basic .NET

by Dave Grundgeiger
December 2001
Beginner
464 pages
13h 51m
English
O'Reilly Media, Inc.
Content preview from Programming Visual Basic .NET

Assignment

In Visual Basic .NET, assignment statements are of the form:

variable, 
field, or 
property = 
expression

Either the type of the expression must be the same as that of the item receiving the assignment, or there must exist an appropriate implicit or explicit conversion from the type of the expression to the type of the item receiving the assignment. For information on implicit and explicit conversions, see Section 2.5.5 earlier in this chapter.

When an assignment is made to a value type, the value of the expression is copied to the target. In contrast, when an assignment is made to a reference type, a reference to the value is stored in the target. This is an important distinction that is worth understanding well. Consider the code in Example 2-3.

Example 2-3. Value-type assignment versus reference-type assignment

Public Structure SomeStructure Public MyPublicMember As String End Structure Public Class SomeClass Public MyPublicMember As String End Class Public Class AssignmentTest Public Shared Sub TestValueAndReferenceAssignment( ) Dim a, b As SomeStructure Dim c, d As SomeClass ' Test assignment to value type. a.MyPublicMember = "To be copied to 'b'" b = a a.MyPublicMember = "New value for 'a'" Console.WriteLine("The value of b.MyPublicMember is """ _ & b.MyPublicMember & """") ' Test assignment to reference type. c = New SomeClass( ) c.MyPublicMember = "To be copied to 'd'" d = c c.MyPublicMember = "New value for 'c'" Console.WriteLine("The value of d.MyPublicMember is ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming Visual Basic .NET, Second Edition

Programming Visual Basic .NET, Second Edition

Jesse Liberty

Publisher Resources

ISBN: 0596000936Supplemental ContentCatalog PageErrata