VB.Net Tutorial/Development/DirectCast

Материал из VB Эксперт
Перейти к: навигация, поиск

DirectCast

public class Test
   public Shared Sub Main
        Dim numberOfTimes As Object
        numberOfTimes = 12
        If TypeOf numberOfTimes Is Integer Then
            Dim repeatCount As Integer
            repeatCount = _
                DirectCast(numberOfTimes, Integer)
            For idx As Integer = 1 To repeatCount
                Console.WriteLine("Duncanma")
            Next
        End If
    End Sub
End class
Duncanma
Duncanma
Duncanma
Duncanma
Duncanma
Duncanma
Duncanma
Duncanma
Duncanma
Duncanma
Duncanma
Duncanma

DirectCast from Integer to MsgBoxResult

Option Strict On
Public Module TestDirectCast
   Public Sub Main
   Dim choice As Integer = MsgBox("Continue?", MsgBoxStyle.YesNo)
   Dim result As MsgBoxResult = DirectCast(choice, MsgBoxResult)
   Console.WriteLine(result.ToString)
   End Sub
End Module
No