VB.Net:NameOf(variable)运算符的反操作。构建一个包含大量变量的数组。

huangapple go评论50阅读模式
英文:

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 Consts 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

huangapple
  • 本文由 发表于 2023年5月25日 15:40:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76329935.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定