本文整理汇总了VB.NET中System.IndexOutOfRangeException类的典型用法代码示例。如果您正苦于以下问题:VB.NET IndexOutOfRangeException类的具体用法?VB.NET IndexOutOfRangeException怎么用?VB.NET IndexOutOfRangeException使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了IndexOutOfRangeException类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: Main
' 导入命名空间
Imports System.Collections.Generic
Module Example
Public Sub Main()
Dim characters As New List(Of Char)()
characters.InsertRange(0, { "a"c, "b"c, "c"c, "d"c, "e"c, "f"c} )
For ctr As Integer = 0 To characters.Count
Console.Write("'{0}' ", characters(ctr))
Next
End Sub
End Module
输出:
a' 'b' 'c' 'd' 'e' 'f' 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.Collections.Generic.List`1.get_Item(Int32 index) at Example.Main()
示例2: Main
' 导入命名空间
Imports System.Collections.Generic
Module Example
Public Sub Main()
Dim characters As New List(Of Char)()
characters.InsertRange(0, { "a"c, "b"c, "c"c, "d"c, "e"c, "f"c} )
For ctr As Integer = 0 To characters.Count - 1
Console.Write("'{0}' ", characters(ctr))
Next
End Sub
End Module
输出:
a' 'b' 'c' 'd' 'e' 'f'
示例3: Main
Module Example
Public Sub Main()
Dim values1() As Integer = { 3, 6, 9, 12, 15, 18, 21 }
Dim values2(5) As Integer
' Assign last element of the array to the new array.
values2(values1.Length - 1) = values1(values1.Length - 1)
End Sub
End Module
输出:
Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array. at Example.Main()
示例4: Main
' 导入命名空间
Imports System.Collections.Generic
Module Example
Dim numbers As New List(Of Integer)
Public Sub Main()
Dim startValue As Integer
Dim args() As String = Environment.GetCommandLineArgs()
If args.Length < 2 Then
startValue = 2
Else
If Not Int32.TryParse(args(1), startValue) Then
startValue = 2
End If
End If
ShowValues(startValue)
End Sub
Private Sub ShowValues(startValue As Integer)
' Create a collection with numeric values.
If numbers.Count = 0 Then
numbers.AddRange( { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22} )
End If
' Get the index of a particular number, in this case 7.
Console.WriteLine("Displaying values greater than or equal to {0}:",
startValue)
Dim startIndex As Integer = numbers.IndexOf(startValue)
' Display all numbers from startIndex on.
For ctr As Integer = startIndex To numbers.Count - 1
Console.Write(" {0}", numbers(ctr))
Next
End Sub
End Module
' The example displays the following output if the user supplies
' 7 as a command-line parameter:
' Displaying values greater than or equal to 7:
'
' 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.Collections.Generic.List`1.get_Item(Int32 index)
' at Example.ShowValues(Int32 startValue)
' at Example.Main()
示例5: Main
' 导入命名空间
Imports System.Collections.Generic
Module Example
Dim numbers As New List(Of Integer)
Public Sub Main()
Dim startValue As Integer
Dim args() As String = Environment.GetCommandLineArgs()
If args.Length < 2 Then
startValue = 2
Else
If Not Int32.TryParse(args(1), startValue) Then
startValue = 2
End If
End If
ShowValues(startValue)
End Sub
Private Sub ShowValues(startValue As Integer)
' Create a collection with numeric values.
If numbers.Count = 0 Then
numbers.AddRange( { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22} )
End If
' Get the index of startValue.
Dim startIndex As Integer = numbers.IndexOf(startValue)
If startIndex < 0 Then
Console.WriteLine("Unable to find {0} in the collection.", startValue)
Else
' Display all numbers from startIndex on.
Console.WriteLine("Displaying values greater than or equal to {0}:",
startValue)
For ctr As Integer = startIndex To numbers.Count - 1
Console.Write(" {0}", numbers(ctr))
Next
End If
End Sub
End Module
' The example displays the following output if the user supplies
' Unable to find 7 in the collection.
示例6: Main
Module Example
Public Sub Main()
Dim values = Array.CreateInstance(GetType(Integer), { 10 }, { 1 })
Dim value As Integer = 2
' Assign values.
For ctr As Integer = 0 To values.Length - 1
values(ctr) = value
value *= 2
Next
' Display values.
For ctr As Integer = 0 To values.Length - 1
Console.Write("{0} ", values(ctr))
Next
End Sub
End Module
输出:
Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array. at System.Array.InternalGetReference(Void* elemRef, Int32 rank, Int32* pIndices) at System.Array.SetValue(Object value, Int32 index) at Microsoft.VisualBasic.CompilerServices.NewLateBinding.ObjectLateIndexSetComplex(Obje ct Instance, Object[] Arguments, String[] ArgumentNames, Boolean OptimisticSet, Boolean RV alueBase) at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateIndexSet(Object Instance, Object[] Arguments, String[] ArgumentNames) at Example.Main()
示例7: Main
Module Example
Public Sub Main()
Dim values = Array.CreateInstance(GetType(Integer), { 10 }, { 1 })
Dim value As Integer = 2
' Assign values.
For ctr As Integer = values.GetLowerBound(0) To values.GetUpperBound(0)
values(ctr) = value
value *= 2
Next
' Display values.
For ctr As Integer = values.GetLowerBound(0) To values.GetUpperBound(0)
Console.Write("{0} ", values(ctr))
Next
End Sub
End Module
输出:
2 4 8 16 32 64 128 256 512 1024
示例8: Main
Module Example
Public Sub Main()
' Generate array of random values.
Dim values() As Integer = PopulateArray(5, 10)
' Display each element in the array.
For Each value In values
Console.Write("{0} ", values(value))
Next
End Sub
Private Function PopulateArray(items As Integer,
maxValue As Integer) As Integer()
Dim values(items - 1) As Integer
Dim rnd As New Random()
For ctr As Integer = 0 To items - 1
values(ctr) = rnd.Next(0, maxValue + 1)
Next
Return values
End Function
End Module
输出:
6 4 4 Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array. at Example.Main()
示例9: Main
Module Example
Public Sub Main()
' Generate array of random values.
Dim values() As Integer = PopulateArray(5, 10)
' Display each element in the array.
For Each value In values
Console.Write("{0} ", value)
Next
End Sub
Private Function PopulateArray(items As Integer,
maxValue As Integer) As Integer()
Dim values(items - 1) As Integer
Dim rnd As New Random()
For ctr As Integer = 0 To items - 1
values(ctr) = rnd.Next(0, maxValue + 1)
Next
Return values
End Function
End Module
输出:
10 6 7 5 8