當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。