3

I am aware that I can't dynamically refer to a variable (eg. 'var & var.Close()'). Can I somehow have a variable that can point to another variable by name?

My current code is (where func() requires an array):

Dim playerNum As String = “P1”
Select Case playerNum
  Case “P1”
    Func(P1(x, xx))
  Case “P2”
    Func(P2(x, xx))
  .........
End Select

Can something along these lines be done?

Dim playerNum As varNameRef = P1(,)
Func(playerNum(x, xx))

Really anything to stop so much repetition would be greatly appreciated. Cheers.

Ben Mate
  • 45
  • 1
  • 5
  • 2
    You can do this using Reflection, but unless you have hundreds of methods like this the readability/maintainability cost won't be worth it. – Joel Etherton Jul 18 '12 at 12:47
  • possible duplicate of [Store Variable Name in String in VB.NET](http://stackoverflow.com/questions/353131/store-variable-name-in-string-in-vb-net) – Hans Passant Jul 18 '12 at 13:00
  • Thanks for the response. I'll take your word for it - I only have 20 cases. – Ben Mate Jul 18 '12 at 13:00
  • You could store delegates to the appropriate functions in a dictionary. This way, you can refer to the function by its key with only one line. – Nico Schertler Jul 18 '12 at 13:01
  • Can you expand on your question to show why "dynamically refer to a variable" is essential to how your application must operate? I can't help but think you would save yourself a whole lot of trouble by refactoring your code to make better use of references, classes, indexers, etc. E.g., SteveDog's idea to make use of a Dictionary to connect `playerNum` to the appropriate `PlayerData` is a step in the right direction. – rskar Jul 18 '12 at 13:19
  • @rskar It isn't. I was just hacking away at the start of a project. SteveDog's answer solved my immediate problem and will definitely make things more effective long-term. – Ben Mate Jul 18 '12 at 14:00

2 Answers2

1

You can do this by reflection, but i would advise against for performance-, readability- and maintainabilty-reasons.

You could use Actions(Of T) if you really want, for example:

Class Player
    Public Name As String
End Class

Sub play(player As Player)
    Console.WriteLine(player.Name & " plays now")
End Sub

Create some players to demonstrate:

Dim playerList As New List(Of Player)
For i As Int32 = 1 To 10
    Dim p = New Player() With {.Name = "P" & i}
    playerList.Add(p)
Next

playerList.ForEach(AddressOf play)    
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1

What you need is a reference type (class), instead of a value type (structure). For instance, if you created a class that stored your array as a property, like this:

Public Class PlayerData
    Public Data(10, 30) As Object
End Class

This also gives you the advantage of easily expanding the data that you store about each player. Then, you could reference the same PlayerData object with multiple variables:

Dim player1 As New PlayerData()
Dim player2 As New PlayerData()
Dim player As PlayerData = Nothing
Select Case playerNum
    Case “P1”
        player = player1
    Case “P2”
        player = player2
End Select
Func(player.Data(x, xx))

However, it would be even better yet to store them in a dictionary as key value pairs, such as:

Dim players As New Dictionary(Of String, PlayerData)()
players("P1") = New PlayerData()
players("P2") = New PlayerData()

Then, you could access you players like this:

Func(players(playerNum).Data(x, xx))
Steven Doggart
  • 43,358
  • 8
  • 68
  • 105