英文:
VB.Net: Inverse of the NameOf(variable) operator. Building an array of large numbers of variables
问题
给定一个字符串常量的数量 n
,使用一个 2xn
的数组来构建一种“字典”,其中每个常量都可以通过其名称检索,对于小数量的 n
来说是直接的,例如 {{NameOf(CONST_FEN01),CONST_FEN01}, {NameOf(CONST_FEN02),CONST_FEN02}, ..., {NameOf(CONST_FEN15),CONST_FEN15}}
,但对于大数量的 n
来说,则变成了一项不可逾越的任务。然而,通过选择具有正则模式的名称,例如 <name>1, <name>2, <name>3, ..., <name>9999
,首先很容易自动创建一个包含这些变量名称/字符串的数组,如果存在一种操作符,可以从其名称/字符串生成每个变量(以与 NameOf 操作符相反的方式),那么这将自动构建 2xn 的“字典”。值得一提的是,我添加了一些代码片段,最后一步是一种愿望。
字符串常量可以在源代码中定义为:
Const CONST_FEN1 As String = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
Const CONST_FEN2 As String = "r2qk2r/pp1n2p/2p1p2n1p/3p/3P/B1PB1N/P1P2PPP/N2Q2K w - - 0 1"
'...
Const CONST_FEN15 As String = "1r3r1k/6pp/6b/pBp3B/Pn1N2P/4p2P1P/2KR3R/ b - - 0 1"
Function BuildNamesArray(ByVal _prefix As String,
ByVal _n As Integer) As String()
Dim names() As String = Nothing
Dim name As String = ""
Dim i As Integer
For i = 1 To _n
name = _prefix + i.ToString ' + CType(i, String)
names = HELPER(names, name)
Next
Return names
End Function
Function HELPER(ByVal _ar() As String,
ByVal _element As String) As String()
'appends the string array
If Not _ar Is Nothing Then
Dim Len As Integer = _ar.Length
If Len = 0 Then
ReDim _ar(0)
_ar(0) = _element
Else
ReDim Preserve _ar(Len)
_ar(_ar.Length - 1) = _element
End If
Else
ReDim _ar(0)
_ar(0) = _element
End If
Return _ar
End Function
接下来的代码将构建一个数组 { "FEN1", "FEN2", ..., "FEN13" }
:
Dim arrNames() As String = BuildNamesArray("FEN", 13)
要将其转换为 { FEN1, FEN2, ..., FEN13 }
,需要一个将字符串 "FEN<n>"
转换为变量名称 FEN<n>
的操作符。
英文:
Given a number n
of string constants, using a 2xn
array to build a kind of "dictionary" where each constant can then be retrieved by its name is straightforward for a small number n
, e.g. {{NameOf(CONST_FEN01),CONST_FEN01}, {NameOf(CONST_FEN02),CONST_FEN02}, ..., {NameOf(CONST_FEN15),CONST_FEN15}}
, but becomes an insurmountable task for large n
. Yet, by choosing names with a regular pattern, say <name>1, <name>2, <name>3, ..., <name>9999
, it is firstly an easy task to automate the creation of an array containing these variable name/strings and, if there were an operator producing each variable from its name/string (in a way the reverse of the NameOf operator), this would automate the building of the 2xn "dictionary". For what it's worth I am adding a few snippets of code, the final step being wishful thinking.
The string constants might be defined in the source code as:
Const CONST_FEN1 As String = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
Const CONST_FEN2 As String = "r2qk2r/pp1n2p/2p1p2n1p/3p/3P/B1PB1N/P1P2PPP/N2Q2K w - - 0 1"
'...
Const CONST_FEN15 As String = "1r3r1k/6pp/6b/pBp3B/Pn1N2P/4p2P1P/2KR3R/ b - - 0 1"
Function BuildNamesArray(ByVal _prefix As String,
ByVal _n As Integer) As String()
Dim names() As String = Nothing
Dim name As String = ""
Dim i As Integer
For i = 1 To _n
name = _prefix + i.ToString '+ CType(i, String)
names = HELPER(names, name)
Next
Return names
End Function
Function HELPER(ByVal _ar() As String,
ByVal _element As String) As String()
'appends the string array
If Not _ar Is Nothing Then
Dim Len As Integer = _ar.Length
If Len = 0 Then
ReDim _ar(0)
_ar(0) = _element
Else
ReDim Preserve _ar(Len)
_ar(_ar.Length - 1) = _element
End If
Else
ReDim _ar(0)
_ar(0) = _element
End If
Return _ar
End Function
The following then builds an array {"FEN1","FEN2",...,"FEN13"}
:
Dim arrNames() As String = BuildNamesArray("FEN", 13)
To turn this into {FEN1,FEN2,...,FEN13}
would require an operator that turns strings "FEN<n>"
into variable names FEN<n>
.
答案1
得分: 2
以下是已翻译的代码部分:
这是找到**所有**类型为`String`的`Const`的代码。请注意,“cherryPi”在其中以显示被拒绝:
Imports System.Reflection
Public Class Form1
Const CONST_FEN1 As String = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
Const CONST_FEN2 As String = "r2qk2r/pp1n2p/2p1p2n1p/3p/3P/B1PB1N/P1P2PPP/N2Q2K w - - 0 1"
'...
Const CONST_FEN15 As String = "1r3r1k/6pp/6b/pBp3B/Pn1N2P/4p2P1P/2KR3R/ b - - 0 1"
Const cherryPi As Double = 3.1415926
Private LookupValues As New Dictionary(Of String, String)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim FIs() As FieldInfo = Me.GetType.GetFields(BindingFlags.NonPublic Or BindingFlags.Static)
For Each FI As FieldInfo In FIs
If FI.FieldType.Equals(GetType(String)) AndAlso FI.IsLiteral Then
LookupValues.Add(FI.Name, FI.GetValue(Nothing))
End If
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim constName As String = "CONST_FEN2"
If LookupValues.ContainsKey(constName) Then
Dim value As String = LookupValues(constName)
MessageBox.Show(constName & " --> " & value)
Else
MessageBox.Show("Const not found: " & constName)
End If
End Sub
End Class
如果您更喜欢只获取与您的模式匹配的那些,并通过“名称”查找它们,那么您可以这样做:
Imports System.Reflection
Public Class Form1
Const CONST_FEN1 As String = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
Const CONST_FEN2 As String = "r2qk2r/pp1n2p/2p1p2n1p/3p/3P/B1PB1N/P1P2PPP/N2Q2K w - - 0 1"
'...
Const CONST_FEN15 As String = "1r3r1k/6pp/6b/pBp3B/Pn1N2P/4p2P1P/2KR3R/ b - - 0 1"
Private LookupValues As New Dictionary(Of String, String)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For i As Integer = 1 To 15
Dim FI As FieldInfo = Me.GetType.GetField("CONST_FEN" & i, BindingFlags.NonPublic Or BindingFlags.Static)
If Not IsNothing(FI) AndAlso FI.FieldType.Equals(GetType(String)) AndAlso FI.IsLiteral Then
LookupValues.Add(FI.Name, FI.GetValue(Nothing))
End If
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim constName As String = "CONST_FEN2"
If LookupValues.ContainsKey(constName) Then
Dim value As String = LookupValues(constName)
MessageBox.Show(constName & " --> " & value)
Else
MessageBox.Show("Const not found: " & constName)
End If
End Sub
End Class
英文:
Here's code that will find ALL Const
s of type String
. Note that "cherryPi" is in there to show that it is being rejected:
Imports System.Reflection
Public Class Form1
Const CONST_FEN1 As String = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
Const CONST_FEN2 As String = "r2qk2r/pp1n2p/2p1p2n1p/3p/3P/B1PB1N/P1P2PPP/N2Q2K w - - 0 1"
'...
Const CONST_FEN15 As String = "1r3r1k/6pp/6b/pBp3B/Pn1N2P/4p2P1P/2KR3R/ b - - 0 1"
Const cherryPi As Double = 3.1415926
Private LookupValues As New Dictionary(Of String, String)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim FIs() As FieldInfo = Me.GetType.GetFields(BindingFlags.NonPublic Or BindingFlags.Static)
For Each FI As FieldInfo In FIs
If FI.FieldType.Equals(GetType(String)) AndAlso FI.IsLiteral Then
LookupValues.Add(FI.Name, FI.GetValue(Nothing))
End If
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim constName As String = "CONST_FEN2"
If LookupValues.ContainsKey(constName) Then
Dim value As String = LookupValues(constName)
MessageBox.Show(constName & " --> " & value)
Else
MessageBox.Show("Const not found: " & constName)
End If
End Sub
End Class
If you'd rather have only those that match your pattern, looking for them "by name", then you could do it this way instead:
Imports System.Reflection
Public Class Form1
Const CONST_FEN1 As String = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
Const CONST_FEN2 As String = "r2qk2r/pp1n2p/2p1p2n1p/3p/3P/B1PB1N/P1P2PPP/N2Q2K w - - 0 1"
'...
Const CONST_FEN15 As String = "1r3r1k/6pp/6b/pBp3B/Pn1N2P/4p2P1P/2KR3R/ b - - 0 1"
Private LookupValues As New Dictionary(Of String, String)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For i As Integer = 1 To 15
Dim FI As FieldInfo = Me.GetType.GetField("CONST_FEN" & i, BindingFlags.NonPublic Or BindingFlags.Static)
If Not IsNothing(FI) AndAlso FI.FieldType.Equals(GetType(String)) AndAlso FI.IsLiteral Then
LookupValues.Add(FI.Name, FI.GetValue(Nothing))
End If
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim constName As String = "CONST_FEN2"
If LookupValues.ContainsKey(constName) Then
Dim value As String = LookupValues(constName)
MessageBox.Show(constName & " --> " & value)
Else
MessageBox.Show("Const not found: " & constName)
End If
End Sub
End Class
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论