给变量赋值(nameOfVariable) [英] Assigning a value to variable(nameOfVariable)

查看:89
本文介绍了给变量赋值(nameOfVariable)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用variable(nameOfVariable) = myValue

使用大量的模块级变量;许多是Public,而256是Const.公开的部分内容是一些 pseudo-Const (偶尔从网络可访问的config.init文件的内容中被删除).

On of my XLA Add-In macro uses a lot of module-level variables ; many being Public and 256 being Const. Part of the Public ones are some pseudo-Const being udpated once in a while from the content of a (network-accessible) config.init file.

因此,在宏的开头或用户启动某些特定过程时,我需要为一堆公共变量赋值.

I thus need to assign value to a bunch of Public variables, at the start of the macro or when the user launches some specific procedures.

config.init文件的内容非常简单:

nameOfVariable1,value1
nameOfVariable2,value2
nameOfVariable3,value3
...

我目前正在使用此初始化过程设置这些变量:

I'm currently setting theses variables using this Initialisation procedure :

Do While Not EOF(1)              ' Loop until end of config.init

    Line Input #fnum, TextLine   ' Read line into variable Textline
    myNameOfVariable = Split(TextLine, ",")(0)

    Select Case myNameOfVariable
    Case "nameOfVariable1"
        nameOfVariable1 = Split(TextLine, ",")(1)
    Case "nameOfVariable2"
        nameOfVariable2 = Split(TextLine, ",")(1)
    Case "nameOfVariable3"
        nameOfVariable3 = Split(TextLine, ",")(1)
    ...

Loop

但这意味着我需要在该过程的Select Case语句中列出每个nameOfVariable,即使分配值的指令完全相同(= Split(TextLine, ",")(1)).

我想用以下内容替换Select Case语句:

I'd like to replace the Select Case statement with something like :

Do While Not EOF(1)              ' Loop until end of config.init

    Line Input #fnum, TextLine   ' Read line into variable.
    nameOfVariable = Split(TextLine, ",")(0)

    If thisvariableexist(nameOfVariable) then
        variable(nameOfVariable) = Split(TextLine, ",")(1)
    End if

Loop

因此,如果我选择在init文件中添加新变量(新的变量或当前声明为Const的一个变量),则无需修改任何内容.

so that I don't ever need to modify anything if I choose to add a new variable in the init file (new one or one currently declared as a Const).

在VBA中可以吗?

推荐答案

考虑一个Dictionary对象(根据@Alex K.注释),其中的键将是变量名.

Consider a Dictionary object (as per @Alex K. comment) where the keys will be the variable names.

一个简单的 Settings 类示例.

VB_PredeclaredId设置为True以用作全局默认实例,而Item已设置为默认成员.

The VB_PredeclaredId set to True to act as a global default instance and the Item has been set as the default member.

VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "Settings"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private m_settings As Object

Public Property Get Item(ByVal Name As String) As String
Attribute Item.VB_UserMemId = 0
    Item = m_settings(Name)
End Property
Public Property Let Item(ByVal Name As String, ByVal Value As String)
    m_settings(Name) = Value
End Property

'For testing - can omit
Public Function Names() As Variant
    Names = m_settings.Keys
End Function

Private Sub Class_Initialize()
    Set m_settings = CreateObject("Scripting.Dictionary")
End Sub

将项目添加到字典中.可以在读取config.init文件时完成此操作.

Adding items to the dictionary. This could be done when reading the config.init file.

Sub Add()

    Settings("Name1") = "Value1"
    Settings("Name2") = "Value2"
    Settings("Name3") = "Value3"

    PrintSettings
End Sub

在您的情况下,将是这样的:

In your case would be something like this:

Dim Values As Variant

Do While Not EOF(1)              ' Loop until end of config.init

    Line Input #fnum, TextLine   ' Read line into variable.
    Values = Split(TextLine, ",")

    Settings(Values(0)) = Values(1)
Loop

通过提供名称来检索项目:

Retrieving items by providing the name:

Settings("Name2")

要测试输出,请执行以下操作:

To test the output:

Sub PrintSettings()
    Dim n As Variant
    For Each n In Settings.Names()
        Debug.Print "Name: " & n & ", Value: " & Settings(n)
    Next
End Sub

输出:

'Name: Name1, Value: Value1
'Name: Name2, Value: Value2
'Name: Name3, Value: Value3

这篇关于给变量赋值(nameOfVariable)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆