Roadmap › VB.NET — Visual Basic .NET from Basics to Advanced (with C# Comparison) › Module: Object-Oriented VB.NET
Lessons in this course (12)
6. Classes, properties, constructors and Shared members
Intermediate★ Must knowLesson 6 of 12
vb
Public Class Customer
Private _name As String
Private Shared _count As Integer ' Shared = C# static
Public Property Id As Integer ' auto-implemented property
Public ReadOnly Property CreatedOn As Date = Date.UtcNow
Public Property Name As String ' full property with validation
Get
Return _name
End Get
Set(value As String)
If String.IsNullOrWhiteSpace(value) Then Throw New ArgumentException("Name required")
_name = value.Trim()
End Set
End Property
Public Sub New(name As String) ' constructor
Me.Name = name ' Me = C# this
_count += 1
End Sub
Public Shared ReadOnly Property Count As Integer
Get
Return _count
End Get
End Property
Public Overrides Function ToString() As String
Return $"{Id}: {Name}"
End Function
End Class
Dim c As New Customer("Asha") With {.Id = 1} ' object initializer uses With { .Prop = value }| C# | VB.NET |
|---|---|
this / base | Me / MyBase (and MyClass) |
static | Shared |
readonly field | ReadOnly |
const | Const |
internal | Friend |
protected internal | Protected Friend |
new Customer { Id = 1 } | New Customer With {.Id = 1} |
null | Nothing |