我有一个困扰我一段时间的问题。考虑下面的代码:
Public Class Class1
Dim VariableList as New List(of Object) From {MainForm.X, MainForm.Y,
SettingsForm.Z, SettingsForm.Textbox1.Text} '...etc.
Sub SetToZero()
For Each Element in VariableList
Element = 0
Next
End Sub
Sub SetToCustomValue(value As Double)
For Each Element in VariableList
Element = value
Next
End Sub
Sub LoadValuesFromFile()
Dim path As String = MainForm.GetPath()
For Each Element in VariableList
Element = File.Readline()
Next
End Sub
Sub SaveValuesToFile()
Dim path As String = MainForm.GetPath()
For Each Element in VariableList
Element = File.Writeline()
Next
End Sub
'and more similar functions/subs
正如您所看到的,这个类所做的是将大量不同的变量从不同的位置放入一个集合中,然后各种函数使用循环对该集合中的每个变量进行读写操作。在这个例子中,我只有几个变量,但大多数时候有几十个。
读取这些值不是问题。编写它们,是因为当我在类的顶部声明VariableList时,该列表只是创建每个变量的副本,而不是维护对它的引用。这意味着,如果某个函数修改了该列表中的MainForm.X,则实际的变量MainForm.X不会被修改。要使用引用,我将不得不放弃循环,并在每个函数中手动分配每个变量。这显然是一堆糟糕的代码。我只想声明一次变量列表,然后使用循环,就像我在上面编写的示例代码中一样。我的问题是,如何创建这样的容器(列表、数组等)来保留对其中原始变量的引用,并使上面的代码成为可能?
发布于 2019-01-04 06:28:09
在VB.NET中没有简单的方法来存储指向变量的指针。作为一种解决办法,您可以使用类来存储变量,因为类始终用作指针。
下面是一个使用拥有整数字典的ContainerClass
来实现这一点的示例。这种方法的一个有趣之处在于,您可以动态地声明和命名“变量”。实际上,它们将由KeyValuePair
管理。一旦你实例化了这个类的一个副本,你就可以通过使用这个类作为指针来“管理”你的变量。
我包含了一个循环,它将所有的整数设置为相同的数字,只是为了好玩,并演示了一种操作,这种操作最终会产生与您问题中描述的效果相似的效果。
Public Class Form2
'This is the container class which will be used to bypass the lack of pointers
'if you wanted to change a property, like the window width, it would be more difficult, but simples variables will be no trouble
Private variableContainer As New VariableContainer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
variableContainer.AddVar("X", 5)
variableContainer.AddVar("Y", 15)
Debug.Print(variableContainer.GetVar("X"))
Debug.Print(variableContainer.GetVar("Y"))
variableContainer.SetAllVar(42)
Debug.Print("Next line will print 42")
Debug.Print(variableContainer.GetVar("X"))
End Sub
End Class
Public Class VariableContainer
'I know a public variable wouldn't need the fancy functions down there, but it's usually better to encapsulate, especially if you're working with a team
'and "future you" count as a teammate, never forget that...
Private list As New Dictionary(Of String, Integer)
Public Sub AddVar(ByVal name As String, ByVal value As Integer)
list.Add(name, value)
End Sub
Public Function GetVar(ByVal name As String) As Integer
If list.ContainsKey(name) Then
Return list(name)
Else
'I choose -1 arbitrarily, don't put too much thinking into this detail
Return -1
End If
End Function
Public Sub SetVar(ByVal name As String, ByVal num As Integer)
If list.ContainsKey(name) Then
list(name) = num
End If
End Sub
Public Sub SetAllVar(ByVal num As Integer)
Dim dict As New Dictionary(Of String, Integer)
For Each item As KeyValuePair(Of String, Integer) In list
dict.Add(item.Key, num)
Next
list = dict
End Sub
End Class
玩得开心!
https://stackoverflow.com/questions/54028384
复制相似问题