3

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?

3
  • When I print TextBox1Store, it prints 0 What do you need to be printed while printing the variable TextBox1Store ? Commented Mar 12, 2013 at 10:00
  • Do yourself a favor and put Option Strict On at the top of your code or go into your VB preferences and set Option Strict to On. Make that the default setting for new projects. You have this line of code: mqtyCalc(someVariable.Text, Label1.Text). The .Text would imply that those are strings, yet your mqtyCalc method takes doubles. How can this even compile? Commented Mar 12, 2013 at 16:19
  • Option Strict is On in my project properties, while typing does codes and didn't get any error. Commented Mar 12, 2013 at 22:18

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh sorry, thought your TextBox1Store was controls. You can't access variables from its string name (or its too complicated). But you can store the values on controls, hidden labels for example, with this method.
1

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

Comments

0

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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.