Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ Option Strict On
Imports System.Globalization

Module Example
Public Sub Main()
Dim julian As New JulianCalendar()
Dim date1 As New Date(1905, 1, 9, julian)
Console.WriteLine("Date ({0}): {1:d}",
CultureInfo.CurrentCulture.Calendar,
date1)
Console.WriteLine("Date in Julian calendar: {0:d2}/{1:d2}/{2:d4}",
julian.GetMonth(date1),
julian.GetDayOfMonth(date1),
julian.GetYear(date1))
End Sub
Public Sub Main()
Dim julian As New JulianCalendar()
Dim date1 As New Date(1905, 1, 9, julian)

Console.WriteLine("Date ({0}): {1:d}",
CultureInfo.CurrentCulture.Calendar,
date1)
Console.WriteLine("Date in Julian calendar: {0:d2}/{1:d2}/{2:d4}",
julian.GetMonth(date1),
julian.GetDayOfMonth(date1),
julian.GetYear(date1))
End Sub
End Module
' The example displays the following output:
' Date (System.Globalization.GregorianCalendar): 1/22/1905
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,27 @@ Class CancelWithCallback
Shared Sub Main()
Dim cts As New CancellationTokenSource()
Dim token As CancellationToken = cts.Token

' Start cancelable task.
Dim t As Task = Task.Run(
Sub()
Dim wc As New WebClient()
' Create an event handler to receive the result.
AddHandler wc.DownloadStringCompleted,
Sub(obj, e)
' Check status of WebClient, not external token.
If Not e.Cancelled Then
Console.WriteLine("The download has completed:" + vbCrLf)
Console.WriteLine(e.Result + vbCrLf + vbCrLf + "Press any key.")
Else
Console.WriteLine("Download was canceled.")
End If
Dim wc As New WebClient()

' Create an event handler to receive the result.
AddHandler wc.DownloadStringCompleted,
Sub(obj, e)
' Check status of WebClient, not external token.
If Not e.Cancelled Then
Console.WriteLine("The download has completed:" + vbCrLf)
Console.WriteLine(e.Result + vbCrLf + vbCrLf + "Press any key.")
Else
Console.WriteLine("Download was canceled.")
End If
End Sub
Using ctr As CancellationTokenRegistration = token.Register(Sub() wc.CancelAsync())
Console.WriteLine("Starting request..." + vbCrLf)
wc.DownloadStringAsync(New Uri("http://www.contoso.com"))
End Using
Using ctr As CancellationTokenRegistration = token.Register(Sub() wc.CancelAsync())
Console.WriteLine("Starting request..." + vbCrLf)
wc.DownloadStringAsync(New Uri("http://www.contoso.com"))
End Using
End Sub, token)

Console.WriteLine("Press 'c' to cancel." + vbCrLf)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Public Structure StoreInfo
Public Function IsOpenNow() As Boolean
Return IsOpenAt(Date.Now.TimeOfDay)
End Function

Public Function IsOpenAt(time As TimeSpan) As Boolean
Dim local As TimeZoneInfo = TimeZoneInfo.Local
Dim offset As TimeSpan = TimeZoneInfo.Local.BaseUtcOffset
Expand Down Expand Up @@ -42,27 +42,27 @@ End Structure

' <Snippet2>
Module Example
Public Sub Main()
' Instantiate a StoreInfo object.
Dim store103 As New StoreInfo()
store103.store = "Store #103"
store103.tz = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")
' Store opens at 8:00.
store103.open = new TimeSpan(8, 0, 0)
' Store closes at 9:30.
store103.close = new TimeSpan(21, 30, 0)
Console.WriteLine("Store is open now at {0}: {1}",
Date.Now.TimeOfDay, store103.IsOpenNow())
Dim times() As TimeSpan = { New TimeSpan(8, 0, 0),
New TimeSpan(21, 0, 0),
New TimeSpan(4, 59, 0),
New TimeSpan(18, 31, 0) }
For Each time In times
Console.WriteLine("Store is open at {0}: {1}",
time, store103.IsOpenAt(time))
Next
End Sub
Public Sub Main()
' Instantiate a StoreInfo object.
Dim store103 As New StoreInfo()
store103.store = "Store #103"
store103.tz = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")
' Store opens at 8:00.
store103.open = new TimeSpan(8, 0, 0)
' Store closes at 9:30.
store103.close = new TimeSpan(21, 30, 0)

Console.WriteLine("Store is open now at {0}: {1}",
Date.Now.TimeOfDay, store103.IsOpenNow())
Dim times() As TimeSpan = {New TimeSpan(8, 0, 0),
New TimeSpan(21, 0, 0),
New TimeSpan(4, 59, 0),
New TimeSpan(18, 31, 0)}
For Each time In times
Console.WriteLine("Store is open at {0}: {1}",
time, store103.IsOpenAt(time))
Next
End Sub
End Module
' The example displays the following output:
' Store is open now at 15:29:01.6129911: True
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,54 @@ Option Strict On
<Assembly: CLSCompliant(True)>

Public Class Animal
Private _species As String
Public Sub New(species As String)
_species = species
End Sub
Public Overridable ReadOnly Property Species As String
Get
Return _species
End Get
End Property
Public Overrides Function ToString() As String
Return _species
End Function
Private _species As String

Public Sub New(species As String)
_species = species
End Sub

Public Overridable ReadOnly Property Species As String
Get
Return _species
End Get
End Property

Public Overrides Function ToString() As String
Return _species
End Function
End Class

Public Class Human : Inherits Animal
Private _name As String
Public Sub New(name As String)
MyBase.New("Homo Sapiens")
_name = name
End Sub
Public ReadOnly Property Name As String
Get
Return _name
End Get
End Property
Private Overrides ReadOnly Property Species As String
Get
Return MyBase.Species
End Get
End Property
Public Overrides Function ToString() As String
Return _name
End Function
Private _name As String

Public Sub New(name As String)
MyBase.New("Homo Sapiens")
_name = name
End Sub

Public ReadOnly Property Name As String
Get
Return _name
End Get
End Property

Private Overrides ReadOnly Property Species As String
Get
Return MyBase.Species
End Get
End Property

Public Overrides Function ToString() As String
Return _name
End Function
End Class

Public Module Example
Public Sub Main()
Dim p As New Human("John")
Console.WriteLine(p.Species)
Console.WriteLine(p.ToString())
End Sub
Public Sub Main()
Dim p As New Human("John")
Console.WriteLine(p.Species)
Console.WriteLine(p.ToString())
End Sub
End Module
' The example displays the following output:
' 'Private Overrides ReadOnly Property Species As String' cannot override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,29 @@ Option Strict On
' <Snippet27>
Imports System.Text

<Assembly:CLSCompliant(True)>
<Assembly: CLSCompliant(True)>

Public Class StringWrapper
Dim internalString As String
Dim internalSB As StringBuilder = Nothing
Dim useSB As Boolean = False
Public Sub New(type As StringOperationType)
If type = StringOperationType.Normal Then
useSB = False
Else
internalSB = New StringBuilder()
useSB = True
End If
End Sub
' The remaining source code...

Dim internalString As String
Dim internalSB As StringBuilder = Nothing
Dim useSB As Boolean = False

Public Sub New(type As StringOperationType)
If type = StringOperationType.Normal Then
useSB = False
Else
internalSB = New StringBuilder()
useSB = True
End If
End Sub

' The remaining source code...
End Class

Friend Enum StringOperationType As Integer
Normal = 0
Dynamic = 1
Normal = 0
Dynamic = 1
End Enum
' The attempt to compile the example displays the following output:
' error BC30909: 'type' cannot expose type 'StringOperationType'
Expand All @@ -37,9 +37,8 @@ End Enum
' </Snippet27>

Module Example
Public Sub Main()
End Sub
Public Sub Main()

End Sub
End Module


Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,29 @@ Option Strict On
<Assembly: CLSCompliant(True)>

Public Class Numbers
Public Shared Function GetTenPrimes() As Array
Dim arr As Array = Array.CreateInstance(GetType(Int32), {10}, {1})
arr.SetValue(1, 1)
arr.SetValue(2, 2)
arr.SetValue(3, 3)
arr.SetValue(5, 4)
arr.SetValue(7, 5)
arr.SetValue(11, 6)
arr.SetValue(13, 7)
arr.SetValue(17, 8)
arr.SetValue(19, 9)
arr.SetValue(23, 10)
Return arr
End Function
Public Shared Function GetTenPrimes() As Array
Dim arr As Array = Array.CreateInstance(GetType(Int32), {10}, {1})
arr.SetValue(1, 1)
arr.SetValue(2, 2)
arr.SetValue(3, 3)
arr.SetValue(5, 4)
arr.SetValue(7, 5)
arr.SetValue(11, 6)
arr.SetValue(13, 7)
arr.SetValue(17, 8)
arr.SetValue(19, 9)
arr.SetValue(23, 10)

Return arr
End Function
End Class
' </Snippet8>

Module Example
Public Sub Main()
For Each number In Numbers.GetTenPrimes()
Console.WriteLine(number)
Next
End Sub
Public Sub Main()
For Each number In Numbers.GetTenPrimes()
Console.WriteLine(number)
Next
End Sub
End Module

Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ Option Strict On
<Assembly: CLSCompliant(True)>

Public Class Numbers
Public Shared Function GetTenPrimes() As UInt32()
Return { 1ui, 2ui, 3ui, 5ui, 7ui, 11ui, 13ui, 17ui, 19ui }
End Function
Public Shared Function GetFivePrimes() As Object()
Dim arr() As Object = { 1, 2, 3, 5ui, 7ui }
Return arr
End Function
Public Shared Function GetTenPrimes() As UInt32()
Return {1ui, 2ui, 3ui, 5ui, 7ui, 11ui, 13ui, 17ui, 19ui}
End Function

Public Shared Function GetFivePrimes() As Object()
Dim arr() As Object = {1, 2, 3, 5ui, 7ui}
Return arr
End Function
End Class
' Compilation produces a compiler warning like the following:
' warning BC40027: Return type of function 'GetTenPrimes' is not CLS-compliant.
Expand All @@ -22,10 +22,10 @@ End Class
' </Snippet9>

Module Example
Public Sub Main()
For Each obj In Numbers.GetFivePrimes()
Console.WriteLine("{0} ({1})", obj, obj.GetType().Name)
Next
End Sub
Public Sub Main()
For Each obj In Numbers.GetFivePrimes()
Console.WriteLine("{0} ({1})", obj, obj.GetType().Name)
Next
End Sub
End Module

Loading