Reputation: 797
Lets assume I have a block of code, this block of code accesses many variables, (lets say 20 just to keep the number high)
This block of code needs to be run 10 times with 10 different sets of variables. so each of the 20 variables has a number defining its set. For example:
Variable1_1
Variable2_1
Variable3_1
...
Variable1_2
Variable2_2
...
Is it better to copy the block of code 10 times and manually correct the variable names in each block, or is it better to make a function and pass all variables? How can i know what is better to do? is there a limit for the number of variables to pass in a function?
If it would be PHP i would use a variable in the variablename like $$variablename but in vb.net this is not supported.
Thanks!
Upvotes: 0
Views: 156
Reputation: 27342
You are probably better to define a data object such as a Class or Structure (depending on the type of data) and pass that to the method instead:
Public Class Params
Public Property Var1 As String
Public Property Var2 As String
'etc
Public Sub New(var1 As String, var2 As String) 'etc
Me.Var1 = var1
Me.Var2 = var2
'etc
End Sub
End Class
Public Sub Main()
Dim prm As New Params("foo", "Bar")
SomeMethod(prm)
End Sub
Public Sub SomeMethod(prm As Params)
'perform routine here on prm.Var1 etc
End Sub
Upvotes: 1
Reputation: 51
It is always better to make functions and not doing it manually.It not only saves memory but improves performance as well. There is no limit for the number of variables to pass in a function.
Upvotes: 0