【问题标题】:Select Variable Array Name From Another Variable从另一个变量中选择变量数组名称
【发布时间】:2021-10-22 08:32:40
【问题描述】:

以多个数组为例:

Dim Common() As String = {"value1", "value2", "value3", "value4"}
Dim Browser() As String = {"item1", "item2", "item3", "item4", "item5"}
Dim AddedItems() As String = {"item6", "item7", "item8", "item9"}
...

字符串 (getItem) 将被设置为上面列出的数组名称之一,如下所示: Dim getItem As String = "Browser"

如何使用它来拉取适当的数组列表? 类似于以下内容:

For Each listItem As String In {getItem}
'Do this...
Next

这需要输出到数组项“item1”、“item2”、“item3”、“item4”、“item5”

这可以在 VB NET 中完成吗?

【问题讨论】:

  • 请停止在问题的标题和正文中添加标签,并正确使用标签。这个问题与 VS 无关,因此不应应用 VS 标签。如果您阅读过该标签的描述,那么您就会知道它不是针对用 VS 编写的代码的问题;仅适用于 IDE 的问题。
  • 你所问的整个概念是错误的。您不会将变量设置为另一个变量的名称。如果您希望能够按名称识别数组,请使用单个Dictionary(Of String, String()),其中名称是键,数组是值。或者,将数组放入另一个数组并通过索引来识别它们。
  • @jmcilhinney 该网站要求提供标签,我提供了。我是新手,我正在使用 Visual Studio / Windows Forms App 制作这个脚本。这似乎与我有关。我很抱歉。
  • 阅读标签的描述,看看它们是否相关。提供它们是有原因的。人们观看这些标签并在人们使用它们时收到通知,而您正在浪费人们的时间来通知他们实际上并不适用于该标签的问题。

标签: arrays vb.net


【解决方案1】:

您所建议的不是事情的运作方式。以正确方式做事的最接近的选择是使用Dictionary 按名称识别数组:

Dim arrays As New Dictionary(Of String, String()) From
        {
            {"Common", {"value1", "value2", "value3", "value4"}},
            {"Browser", {"item1", "item2", "item3", "item4", "item5"}},
            {"AddedItems", {"item6", "item7", "item8", "item9"}}
        }

然后你可以这样做:

Dim arrayName = "Browser"

For Each item In arrays(arrayName)
    'Use item here.
Next

【讨论】:

  • 非常感谢! - 将阅读更多关于此的内容。
  • @MBF,也许还有更好的选择。我的答案基于问题中提供的信息,但如果您对其进行编辑以提供更具体的背景,那么我们可能会看到更好的选择。
  • 我添加了对dicWordLists.Containskey("Browser") 的测试,因为如果“浏览器”拼写错误,您将收到未处理的异常(错误)。尝试使用“Browser1”作为键,看看会发生什么。
  • @user94325897,我提供了一个示例来回答所问的问题。我不是在这里教授防御性编程技术作为我回答的每个问题的一部分。这是一个普遍的原则,应该适用于你所做的任何事情。此外,问题中没有任何具体内容表明可以使用无效密钥,因此没有任何内容表明需要检查无效密钥。我回答了被问到的问题。如果这导致另一个问题,那么我也会回答这个问题,但我不会假设它是必需的。
  • @user94325897,另外,如果我要允许无效的密钥,我倾向于使用TryGetValue,因为我评论了你的答案。如果密钥在一次操作中有效,它将进行检查并获取值。如果您只想知道密钥是否有效但实际上不想使用该值,请仅使用 ContainsKey
【解决方案2】:

如果您不想将所有字符串数组一次性注入到字典中,而是希望使用.Add() 命令按顺序添加字典元素,则可以使用以下方法:

Dim Common() As String = {"value1", "value2", "value3", "value4"}
Dim Browser() As String = {"item1", "item2", "item3", "item4", "item5"}
Dim AddedItems() As String = {"item6", "item7", "item8", "item9"}

'Define the dictionary
Dim dicWordLists As New Dictionary(Of String, String())
'Add string arrays to dictionary
dicWordLists.Add("Common", Common)
dicWordLists.Add("Browser", Browser)
dicWordLists.Add("AddedItems", AddedItems)

'Set the key="Browser"
Dim getString As String = "Browser1"

'First, test if key is in dictionary, and if key is found, fetch value the string array
If dicWordLists.ContainsKey(getString) Then
    Dim strArray() As String = dicWordLists(getString)
    'Loop though all elements in string array
    For Each item In strArray
        MsgBox(item)
    Next
End If

如果您需要处理字典中的所有键条目,您可以使用以下方法遍历所有键:

For Each kvp As KeyValuePair(Of String, String()) In dicWordLists
  Dim strArray1() As String = kvp.Value
  For Each item In strArray1
     MsgBox(item)
  Next
Next

【讨论】:

  • 您不会遍历 Dictionary 的键来寻找匹配项。 Dictionary 的全部意义在于它会进行查找,即您提供键并且它提供相应的值。
  • 同意,当您需要处理所有密钥时,您会循环。在字典里。查看修改后的答案。
  • 虽然在这种情况下使用ContainsKey不一定是错误的,但您应该使用TryGetValue进行检查并在一次操作中获取值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-09
  • 2015-05-24
  • 1970-01-01
  • 2021-08-22
  • 1970-01-01
  • 2016-10-17
相关资源
最近更新 更多