Home > Net >  How to declare a dictionary using a name the user specifies?
How to declare a dictionary using a name the user specifies?

Time:02-16

Obviously I cannot use strName in the Dictionary declaration line, but I'm just placing it there to represent what I'm trying to do. For example, if the User enters "carrot", I want the dictionary created to be named carrot. Is there a way to do this?

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim strName As String
        strName = TextBox1.Text

        Dim strName As New Dictionary(Of String, String)
    End Sub

End Class

CodePudding user response:

You can't. The name is useful for debugging and in reality the compiler will not use the name anyway - the name will be kept in the pdb file, nowhere else.

If you need to keep track of some dictionary by name, you could use another dictionary such as:

Private dictionaries As New Dictionary(Of String, Dictionary(Of String, String))()

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim strName = TextBox1.Text
    Dim myNamedDictionary As Dictionary(Of String, String)
    If dictionaries.ContainsKey(strName) Then
        myNamedDictionary = dictionaries(strName)
    Else
        myNamedDictionary = New Dictionary(Of String, String)()
        dictionaries.Add(strName, myNamedDictionary)
    End If
    ' now you have a dictionary for the name you entered (carrot)
End Sub

To retrieve

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim carrotDictionary = dictionaries("carrot")
End Sub

I think this is as close as you will get.

CodePudding user response:

You can, kind of. Using a compiler you can compile your own code, but the end result seems so unusable, I doubt this is what you want to do

Option Strict Off
Imports System.Reflection
Imports System.CodeDom.Compiler

Public Class Form1
    Private dictionaryInstance As Object
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim strName = TextBox1.Text
        Dim t = GetType(Form1)
        Dim ns = t.Namespace
        Dim provider = New VBCodeProvider()
        Dim params = New CompilerParameters()
        params.GenerateInMemory = True
        params.ReferencedAssemblies.Add(Assembly.GetEntryAssembly().Location)
        params.OutputAssembly = "OutputAssembly"
        Dim code =
$"
Imports System
Imports System.Collections.Generic
Namespace {ns}
Partial Public Class DictionaryClass
    Public {strName} As New Dictionary(Of String, String)
End Class
End Namespace"
        Dim results = provider.CompileAssemblyFromSource(params, {code})
        Dim assy = results.CompiledAssembly
        Dim o As Object = assy.CreateInstance($"{ns}.DictionaryClass")
        Me.dictionaryInstance = o
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        dictionaryInstance.carrot.Add("a", "b")
    End Sub
End Class
  • Related