VB.Net |有没有办法将动态量的变量作为函数/子函数的参数引用?

时间:2019-05-24 17:22:26

标签: vb.net variables dynamic reference arguments

我正在尝试使用ByRef将动态数量的变量传递给Sub;

本质上,我正在尝试创建一个可以轻松导入到我的项目中的模块,并使处理文件保存/加载过程自动化。

Sub / Function将许多变量作为引用,然后循环遍历它们以更改每个变量的值。

我意识到我在视觉基本语法的工作方式上缺少关键点,但我无法弄清楚我需要做什么。

我为此编写的代码是:

    Public Sub LoadSaveToVars(ByRef KeyNamesAndVars() As Object, ByVal FileLoc As String = "")
        If isEven(KeyNamesAndVars.Length) Then
            Dim Contents As String = My.Computer.FileSystem.ReadAllText(FileLoc)
            Dim isOnName As Boolean = True
            Dim CurrentVal As String = ""
            For i = 0 To KeyNamesAndVars.Length - 1
                If isOnName Then
                    CurrentVal = GetStringValue(KeyNamesAndVars(i), Contents) 'Get the value of the key with the key name in the array
                    isOnName = False
                Else
                    KeyNamesAndVars(i) = CurrentVal 'Set the variable referenced in the array to the value
                    isOnName = True
                End If
            Next
        Else
            Throw New ArgumentOutOfRangeException("The key names and variables supplied are not even.", "Error loading to variables!")
        End If
    End Sub

这是我尝试使用此功能的方法:

Dim TestVar1 As String = ""
Dim TestVar2 As String = ""
LoadSaveToVars({"key1", TestVar1, "key2", TestVar2})

为了使这个问题更清楚,我没有包括其他功能,但是我在绘制我想发生的事情上做得很糟糕:https://gyazo.com/eee34b8dff766401f73772bb0fef981a 最后,我希望TestVar1等于“ val1”,并且TestVar2等于“ val2”,并能够将其扩展为动态数量的变量。这可能吗?

0 个答案:

没有答案