VB   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了对于VB.NET中2D阵列上的每个循环大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在编写一个循环来遍历2D循环的第一个数组,我现在有这样的:
For Each Dir_path In MasterIndex(,0)
    'do some stuff here
Next

但它给了我一个错误,说它希望在第一个字段中有一个表达式.但这就是我想要做的,循环通过第一个字段.我该如何解决?我会在那里放什么?

编辑:澄清一下,我特意在每个数组的子数组中寻找第0个元素,这就是第二个字段始终为0的原因.

您可以使用嵌套的For循环来完成此操作

注意:使用For Each循环迭代数组中的元素时,每次迭代生成的占位符都是实际数组中值的副本.对该值的更改不会反映在原始数组中.如果您想要执行除读取信息之外的任何操作,则需要使用For循环直接寻址数组元素.

假设二维数组,以下代码示例将为每个维度中的每个元素分配一个值.

Dim MasterIndex(5,2) as string

For iOuter As Integer = MasterIndex.GetLowerBound(0) To MasterIndex.GetUpperBound(0)
  'iOuter represents the first dimension
  For iInner As Integer = MasterIndex.GetLowerBound(1) To MasterIndex.GetUpperBound(1)
    'iInner represents the second dimension
    MasterIndex(iOuter,iInner) = "This Isn't Nothing" 'Set the value
  Next 'iInner

  'If you are only interested in the first element you don't need the inner loop
  MasterIndex(iOuter,0) = "This is the first element in the second dimension"
Next 'iOuter
'MasterIndex is now filled completely

您可以选择使用.Rank属性动态迭代每个维度

如果你想循环一个锯齿状的数组,就像Konrad Rudolph所建议的那样(这在函数上更接近地匹配其他更松散类型的语言,如php的数组实现),你可以像这样:

'This is a jagged array (array of arrays) populated with three arrays each with three elements
Dim JaggedIndex()() As String = {
  new String() {"1","2","3"},new String() {"1","3"}
}

For Each aOuter As String() In JaggedIndex
  'If you are only interested in the first element you don't need the inner for each loop
  Dim sDesiredValue As String = aOuter(0) 'This is the first element in the inner array (second dimension)

  For Each SELER_782_11845@ent As String In aOuter
    Dim sCatch As String = SELER_782_11845@ent 'Assign the value of each element in the inner array to sCatch
    SELER_782_11845@ent = "This Won't Stick" 'This will only hold value within the context of this loop iteration
  Next 'SELER_782_11845@ent
Next 'aOuter
'JaggedIndex is still the same as when it was declared

大佬总结

以上是大佬教程为你收集整理的对于VB.NET中2D阵列上的每个循环全部内容,希望文章能够帮你解决对于VB.NET中2D阵列上的每个循环所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。