本文整理汇总了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
示例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
输出:
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
示例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
输出:
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
示例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
输出:
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
示例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
输出:
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
示例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
输出:
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
示例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
输出:
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
输出:
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
输出:
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
输出:
Non-negative number required. Parameter name: length2