ตามด้านบน .NET ไม่ support "dynamic variable name" ลองดูตัวอย่างนี้
C# version
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
List<string> a1 = new List<string> {"123", "231", "312", "321"};
List<string> a2 = new List<string> {"456", "564", "645", "654"};
List<string> a3 = new List<string> {"789", "897", "978", "987"};
Dictionary<string, List<string>> data = new Dictionary<string, List<string>> {
{"a1", a1},
{"a2", a2},
{"a3", a3}
};
string test = "312";
string res = string.Empty;
foreach(KeyValuePair<string, List<string>> item in data) {
int t = item.Value.IndexOf(test);
if (t != -1) {
res = item.Key + " " + t.ToString();
break;
}
}
Console.WriteLine(res);
}
}
VB.NET version
Imports System
Imports System.Collections.Generic
Public Class Program
Public Shared Sub Main()
Dim a1 As List(Of String) = New List(Of String) From { "123", "231", "312", "321"}
Dim a2 As List(Of String) = New List(Of String) From { "456", "564", "645", "654"}
Dim a3 As List(Of String) = New List(Of String) From { "789", "897", "978", "987"}
Dim data As Dictionary(Of String, List(Of String)) = New Dictionary(Of String, List(Of String)) From {
{"a1", a1},
{"a2", a2},
{"a3", a3}
}
Dim test As String = "312"
Dim res As String = String.Empty
For Each item As KeyValuePair(Of String, List(Of String)) In data
Dim t As Integer = item.Value.IndexOf(test)
If t <> -1 Then
res = item.Key & " " & t.ToString()
Exit For
End If
Next
Console.WriteLine(res)
End Sub
End Class