当前位置: 首页>>代码示例>>VB.NET>>正文


VB.NET IndexOutOfRangeException类代码示例

本文整理汇总了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
开发者ID:VB.NET开发者,项目名称:System,代码行数:12,代码来源:IndexOutOfRangeException

输出:

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
开发者ID:VB.NET开发者,项目名称:System,代码行数:12,代码来源:IndexOutOfRangeException

输出:

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
开发者ID:VB.NET开发者,项目名称:System,代码行数:9,代码来源:IndexOutOfRangeException

输出:

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()
开发者ID:VB.NET开发者,项目名称:System,代码行数:44,代码来源:IndexOutOfRangeException

示例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.
开发者ID:VB.NET开发者,项目名称:System,代码行数:40,代码来源:IndexOutOfRangeException

示例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
开发者ID:VB.NET开发者,项目名称:System,代码行数:16,代码来源:IndexOutOfRangeException

输出:

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
开发者ID:VB.NET开发者,项目名称:System,代码行数:16,代码来源:IndexOutOfRangeException

输出:

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
开发者ID:VB.NET开发者,项目名称:System,代码行数:20,代码来源:IndexOutOfRangeException

输出:

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
开发者ID:VB.NET开发者,项目名称:System,代码行数:20,代码来源:IndexOutOfRangeException

输出:

10   6   7   5   8


注:本文中的System.IndexOutOfRangeException类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。