當前位置: 首頁>>代碼示例>>VB.NET>>正文


VB.NET ArgumentOutOfRangeException類代碼示例

本文整理匯總了VB.NET中System.ArgumentOutOfRangeException的典型用法代碼示例。如果您正苦於以下問題:VB.NET ArgumentOutOfRangeException類的具體用法?VB.NET ArgumentOutOfRangeException怎麽用?VB.NET ArgumentOutOfRangeException使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了ArgumentOutOfRangeException類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。

示例1: Module1

Module Module1
   Public Sub Main()
       Try
           Dim guest1 As Guest = New Guest("Ben", "Miller", 17)
           Console.WriteLine(guest1.GuestInfo)
       Catch outOfRange As ArgumentOutOfRangeException
           Console.WriteLine("Error: {0}", outOfRange.Message)
       End Try
   End Sub
End Module

Class Guest
    Private FirstName As String
    Private LastName As String
    Private Age As Integer

    Public Sub New(ByVal fName As String, ByVal lName As String, ByVal age As Integer)
        MyBase.New()
        FirstName = fName
        LastName = lName
        If (age < 21) Then
            Throw New ArgumentOutOfRangeException("age", "All guests must be 21-years-old or older.")
        Else
            age = age
        End If
    End Sub

    Public Function GuestInfo() As String
        Dim gInfo As String = (FirstName + (" " _
                    + (Me.LastName + (", " + Me.Age.ToString))))
        Return gInfo
    End Function
End Class
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:33,代碼來源:ArgumentOutOfRangeException

示例2: Example

' 導入命名空間
Imports System.Collections.Generic

Module Example
   Public Sub Main()
      Dim list As New List(Of String)
      Console.WriteLine("Number of items: {0}", list.Count)
      Try
         Console.WriteLine("The first item: '{0}'", list(0))
      Catch e As ArgumentOutOfRangeException
         Console.WriteLine(e.Message)
      End Try
   End Sub
End Module
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:14,代碼來源:ArgumentOutOfRangeException

輸出:

Number of items: 0
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

示例3: list

If list.Count > 0 Then
   Console.WriteLine("The first item: '{0}'", list(0))
End If
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:3,代碼來源:ArgumentOutOfRangeException

示例4: Main

' 導入命名空間
Imports System.Collections.Generic

Module Example
   Public Sub Main()
      Dim numbers As New List(Of Integer)
      numbers.AddRange( { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 20 } )
      
      Dim squares As New List(Of Integer)
      For ctr As Integer = 0 To numbers.Count - 1
         squares(ctr) = CInt(numbers(ctr) ^ 2) 
      Next
   End Sub
End Module
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:14,代碼來源:ArgumentOutOfRangeException

輸出:

Unhandled Exception: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
at Example.Main()

示例5: List

Dim squares As New List(Of Integer)
For ctr As Integer = 0 To numbers.Count - 1
   squares.Add(CInt(numbers(ctr) ^ 2)) 
Next
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:4,代碼來源:ArgumentOutOfRangeException

示例6: Example

' 導入命名空間
Imports System.Collections.Generic

Module Example
   Public Sub Main()
      Dim list As New List(Of String) 
      list.AddRange( { "A", "B", "C" } )
      ' Get the index of the element whose value is "Z".
      Dim index As Integer = list.FindIndex(AddressOf (New StringSearcher("Z")).FindEquals)
      Try
         Console.WriteLine("Index {0} contains '{1}'", index, list(index)) 
      Catch e As ArgumentOutOfRangeException
         Console.WriteLine(e.Message)
      End Try
   End Sub
End Module

Friend Class StringSearcher
   Dim value As String
   
   Public Sub New(value As String)
      Me.value = value
   End Sub
   
   Public Function FindEquals(s As String) As Boolean
      Return s.Equals(value, StringComparison.InvariantCulture) 
   End Function
End Class
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:28,代碼來源:ArgumentOutOfRangeException

輸出:

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

示例7: StringSearcher

' Get the index of the element whose value is "Z".
Dim index As Integer = list.FindIndex(AddressOf (New StringSearcher("Z")).FindEquals)
If index >= 0 Then
   Console.WriteLine("Index {0} contains '{1}'", index, list(index)) 
End If
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:5,代碼來源:ArgumentOutOfRangeException

示例8: Example

' 導入命名空間
Imports System.Collections.Generic

Module Example
   Public Sub Main()
      Dim list As New List(Of String) 
      list.AddRange( { "A", "B", "C" } )
      Try
         ' Display the elements in the list by index.
         For ctr As Integer = 0 To list.Count
            Console.WriteLine("Index {0}: {1}", ctr, list(ctr)) 
         Next   
      Catch e As ArgumentOutOfRangeException
         Console.WriteLine(e.Message)
      End Try
   End Sub
End Module
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:17,代碼來源:ArgumentOutOfRangeException

輸出:

Index 0: A
Index 1: B
Index 2: C
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

示例9: list

' Display the elements in the list by index.
For ctr As Integer = 0 To list.Count - 1 
   Console.WriteLine("Index {0}: {1}", ctr, list(ctr)) 
Next
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:4,代碼來源:ArgumentOutOfRangeException

示例10: Main

Module Example
   Public Sub Main()
       Dim words() As String = { "the", "today", "tomorrow", " ", "" }
       For Each word In words
          Console.WriteLine("First character of '{0}': '{1}'", 
                            word, GetFirstCharacter(word))
       Next                     
   End Sub
   
   Private Function GetFirstCharacter(s As String) As Char
      Return s(0)
   End Function
End Module
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:13,代碼來源:ArgumentOutOfRangeException

輸出:

First character of 'the': 't'
First character of 'today': 't'
First character of 'tomorrow': 't'
First character of ' ': ' '

Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.
at Example.Main()

示例11: GetFirstCharacter

Function GetFirstCharacter(s As String) As Char
   If String.IsNullOrEmpty(s) Then 
      Return ChrW(0)
   Else   
      Return s(0)
   End If   
End Function
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:7,代碼來源:ArgumentOutOfRangeException

示例12: Main

Module Example
   Public Sub Main()
      Dim phrases() As String = { "ocean blue", "concerned citizen", 
                                  "runOnPhrase" }
      For Each phrase In phrases
         Console.WriteLine("Second word is {0}", GetSecondWord(phrase))
      Next                            
  End Sub
  
  Function GetSecondWord(s As String) As String
     Dim pos As Integer = s.IndexOf(" ")
     Return s.Substring(pos).Trim()
  End Function
End Module
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:14,代碼來源:ArgumentOutOfRangeException

輸出:

Second word is blue
Second word is citizen

Unhandled Exception: System.ArgumentOutOfRangeException: StartIndex cannot be less than zero.
Parameter name: startIndex
at System.String.Substring(Int32 startIndex, Int32 length)
at Example.GetSecondWord(String s)
at Example.Main()

示例13: Main

Module Example
   Public Sub Main()
      Dim phrases() As String = { "ocean blue", "concerned citizen", 
                                  "runOnPhrase" }
      For Each phrase In phrases
         Dim word As String = GetSecondWord(phrase)
         If Not String.IsNullOrEmpty(word) Then _
            Console.WriteLine("Second word is {0}", word)
      Next                            
   End Sub
  
   Function GetSecondWord(s As String) As String
      Dim pos As Integer = s.IndexOf(" ")
      If pos >= 0
          Return s.Substring(pos).Trim()
      Else
         Return String.Empty
      End If
  End Function
End Module
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:20,代碼來源:ArgumentOutOfRangeException

輸出:

Second word is blue
Second word is citizen

示例14: Main

' 導入命名空間
Imports System.Collections.Generic

Module Example
   Public Sub Main()
      Dim sentence As String = "This is a simple, short sentence."
      Console.WriteLine("Words in '{0}':", sentence)
      For Each word In FindWords(sentence)
         Console.WriteLine("   '{0}'", word)
      Next
   End Sub
   
   Function FindWords(s As String) As String()
      Dim start, ending As Integer
      Dim delimiters() As Char = { " "c, "."c, ","c, ";"c, ":"c,
                                   "("c, ")"c }
      Dim words As New List(Of String)()

      Do While ending >= 0
         ending = s.IndexOfAny(delimiters, start)
         If ending >= 0
            If ending - start > 0 Then
               words.Add(s.Substring(start, ending - start)) 
            End If
            start = ending + 1
         Else
            If start < s.Length - 1 Then
               words.Add(s.Substring(start))
            End If      
         End If
      Loop    
      Return words.ToArray()                         
   End Function
End Module
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:34,代碼來源:ArgumentOutOfRangeException

輸出:

Words in 'This is a simple, short sentence.':
This'
is'
a'
simple'
short'
sentence'

示例15: Example

Module Example
   Public Sub Main()
      Dim dimension1 As Integer = 10
      Dim dimension2 As Integer = -1
      Try
         Dim arr AS Array = Array.CreateInstance(GetType(String), 
                                                 dimension1, dimension2)
      Catch e As ArgumentOutOfRangeException
         If e.ActualValue IsNot Nothing Then
            Console.WriteLine("{0} is an invalid value for {1}: ", 
                              e.ActualValue, e.ParamName)
         End If                     
         Console.WriteLine(e.Message)
      End Try
   End Sub
End Module
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:16,代碼來源:ArgumentOutOfRangeException

輸出:

Non-negative number required.
Parameter name: length2


注:本文中的System.ArgumentOutOfRangeException類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。