Assigning a value to a variable (nameOfVariable)

Is it possible to use something like variable(nameOfVariable) = myValue

What am I doing:

My macro XLA Add-Inuses many module level variables; many of which Public256 are Const. Part of the public is some kind of pseudo-construction, which from time to time falls upon the contents of the config.init file (available network).

Thus, I need to assign a value to the Public variables heap , at the beginning of the macro or when the user starts some specific procedures.

The contents of the config.init file is very simple:

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

I am currently setting theses variables with this initialization 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

, OfVariable Select Case , , , (= Split(TextLine, ",")(1)).

Select Case - :

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

, ( Const).

VBA?

+4
2

Dictionary ( @Alex K.), .

.

VB_PredeclaredId True, , Item .

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.

Sub Add()

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

    PrintSettings
End Sub

- :

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

:

Settings("Name2")

:

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
+2

, . INI , enter image description here

CApp

Option Explicit

Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" _
                                                 (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, _
                                                  ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" _
                                                   (ByVal lpApplicationName As String, ByVal lpKeyName As Any, _
                                                    ByVal lpString As Any, ByVal lpFileName As String) As Long

Const gsINIFILENAME As String = ".. full file name of config.ini ..."


Private m_VAR1 As String
Private m_VAR2 As String
Private m_VAR3 As String
Private m_VAR4 As String

Private Const msSEC As String = "Variables"
Private Const msVAR1 As String = "gVAR1"
Private Const msVAR2 As String = "gVAR2"
Private Const msVAR3 As String = "gVAR3"
Private Const msVAR4 As String = "gVAR4"

Public Property Get gVAR1() As String
    gVAR1 = m_VAR1
End Property
Public Property Let gVAR1(ByVal sVAR1 As String)
    m_VAR1 = sVAR1
End Property
Public Property Get gVAR2() As String
    gVAR2 = m_VAR2
End Property
Public Property Let gVAR2(ByVal sVAR2 As String)
    m_VAR2 = sVAR2
End Property

Public Property Get gVAR3() As String
    gVAR3 = m_VAR3
End Property
Public Property Let gVAR3(ByVal sVAR3 As String)
    m_VAR3 = sVAR3
End Property

Public Property Get gVAR4() As String
    gVAR4 = m_VAR4
End Property
Public Property Let gVAR4(ByVal sVAR4 As String)
    m_VAR4 = sVAR4
End Property


Private Sub Class_Initialize()

Dim sReturn As String * 255
Dim lLen As Long

    lLen = GetPrivateProfileString(msSEC, msVAR1, "", sReturn, 255, gsINIFILENAME)
    Me.gVAR1 = Left(sReturn, lLen)

    lLen = GetPrivateProfileString(msSEC, msVAR2, "", sReturn, 255, gsINIFILENAME)
    Me.gVAR2 = Left$(sReturn, lLen)

    lLen = GetPrivateProfileString(msSEC, msVAR3, "", sReturn, 255, gsINIFILENAME)
    Me.gVAR3 = Left$(sReturn, lLen)

    lLen = GetPrivateProfileString(msSEC, msVAR4, "", sReturn, 255, gsINIFILENAME)
    Me.gVAR4 = Left$(sReturn, lLen)


End Sub

Option Explicit

Public gApp As CApp

Sub Auto_Open()

'Read ini file

    Set gApp = New CApp
    With gApp
        Debug.Print .gVAR1, .gVAR2, .gVAR3, .gVAR4
    End With  

End Sub
+2

Source: https://habr.com/ru/post/1694476/


All Articles