You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.Net中如何用For Loop读取元组指定行的Item2值并判断是否为空

VB.Net 遍历元组集合并判断Item2是否为空的正确实现

你代码里的核心问题是元组元素的访问语法错误,正确的写法应该是先通过索引获取集合中的元组对象,再访问它的Item2属性。

修正后的For Loop实现代码

Public MyTuple As New List(Of Tuple(Of String, String))

MyTuple.Add(Tuple.Create("John", "Claire"))
MyTuple.Add(Tuple.Create("Dave", "Juan"))
MyTuple.Add(Tuple.Create("Amy", String.Empty)) ' 增加一个空值测试项

For i As Integer = 0 To MyTuple.Count - 1
    ' 先取集合中的元组,再访问Item2属性
    If MyTuple(i).Item2 <> String.Empty Then
        ' 执行你的业务逻辑,比如输出Item2内容
        Console.WriteLine(MyTuple(i).Item2)
    End If
Next

更简洁的For Each遍历方式

如果不需要使用索引操作集合,推荐用For Each遍历,代码更直观易读:

For Each tupleItem In MyTuple
    If tupleItem.Item2 <> String.Empty Then
        ' Do something
        Console.WriteLine(tupleItem.Item2)
    End If
Next

内容的提问来源于stack exchange,提问作者Daniel Santos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 04:06:36