conquistador
conquistador

Reputation: 693

Using string as a name of a variable

Is it possible to use a string as a name of a variable? For Example..
I declared x as a private double

 Private TextBox1Store,TextBox2Store,TextBox3Store As Double

I will use that as a variables for storing value.

This function multiplies the number inside a label and textbox and returns a product.

 Private Function mqtyCalc(ByVal y As Integer, ByVal z As Integer) As Integer
    Dim w As Integer
    w = y * z
    Return w
 End Function

This part handles three textbox events.

 Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged
    Dim tb As TextBox = sender
    Dim tempObjNm As String
    tempObjNm =    tb.Name + "Strore"
    tempObjNm = mqtyCalc(someVariable.Text, Label1.Text)
End Sub

And this the part I'm trying to solve.

 tempObjNm = someVariable.Name + "Strore"
 tempObjNm = mqtyCalc(tb.Text, Label1.Text)

The "tempObjNm" is declared inside this sub as string.
Everytime I Input a value inside the textboxs, this sub will get the name of the textbox that has been inserted a value and the name will be added "Store" at their end Jus to mimic the variable name declared above. Example,

temObjNm = TextBox1Store (mimicking the Private TextBox1Store)
temObjNm is currently a string declared by

  Dim tempObjNm As String

As a string

This part is the storing part of the sub

 tempObjNm = mqtyCalc(tb.Text, 4)

(Take note that the value of tempObjNm = "TextBox1Store"

When I print TextBox1Store, it prints 0

How is that? Is it not possible to use a string for mimicking the varible just to store value in it?

Upvotes: 3

Views: 15522

Answers (3)

Camahalan Royette
Camahalan Royette

Reputation: 166

I would like to share the code that I am using. Hope this helps future viewers of this question.

Dim tb As TextBox = sender
Dim tempObjNm As String
tempObjNm =    tb.Name + "Strore"
Me.GetType.GetField(tempObjNm).SetValue(Me, CType(mqtyCalc(someVariable.Text, Label1.Text), Double))

Upvotes: 0

djv
djv

Reputation: 15772

Can you use a Dictionary(Of String, Double)?

Private values As New Dictionary(Of String, Double)

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    setValue(sender)
End Sub

Private Sub setValue(sender As Object)
    Dim tb As TextBox = CType(sender, TextBox)
    Dim tbName As String = tb.Name & "Strore"
    If Not values.ContainsKey(tbName) Then
        values.Add(tbName, tb.Text)
    Else
        values(tbName) = tb.Text
    End If
End Sub

Private Function getValue(sender As Object) As Double
    Dim tbName As String = CType(sender, TextBox).Name & "Strore"
    If Not values.ContainsKey(tbName) Then
        Return Double.NaN
    Else
        Return values(tbName)
    End If
End Function

Upvotes: 1

SysDragon
SysDragon

Reputation: 9888

Just do this:

Dim tb As TextBox = CType(sender, TextBox)
Me.Controls(tb.Name & "Store") = mqtyCalc(CInt(someVariable.Text), CInt(Label1.Text))

I strongly suggest you a couple of things. First, enable Option Strict On in you project properties, as it will improve your programming practices. And, as you can see in my code, concatenate strings with & instead of + in VB.NET.

Upvotes: 2

Related Questions