Converting Strings to variables

georg

Freshman
Joined
Jun 26, 2003
Messages
28
Location
Australia
Does anyone know of a way to convert a string to a variable?

Example:
Say I am row 5, column 1 of a datagrid.
When my user changes the text of (5,1) to "GST" using a combo box, I want to multiply the amount in (5,2) by 10 percent and put the product in (5,3).

I don't want to hardcode this, I'd rather to have it as one of the "properties" of (5,1).

So, the string that I would like to use as the property would be "3 = 2 * dGST"

In this scenario the "dGST" portion of the string would have to be converted/interpreted to a double (via a magical string converter) that holds the current GST rate (which would already be declared: Dim dGST as double).
I'd use some sort of parsing algorithm to pick up the alpha characters in any string.

(I'm not holding my breath though)

Thanks,

georg
 
Okaaay,

I thought that this would be hard to explain.

Another scenario:
Imagine you have the function:

Visual Basic:
Function GSTCalculation (Byval s as String) as Double
Dim GSTRate as Double = 0.1

Return MagicConverter(Left(s,7)) * CInt(Right(s,3))

End Function
You Call this function with the following bit of code:
Visual Basic:
Call GSTCalculation("GSTRate * 100")

The returned value from the GSTCalculation function would be 10.0.

The MagicConverter function has picked up the left 7 characters from "GSTRate * 100" and converted them to the variable that is declared: Dim GSTRate as Double, the same way that the CInt function has converted the right 3 characters to an Integer.

I'll get back into my box now.

georg
 
I think georg is looking for an expression evaluator (if that's the right term). If I'm correct w/ the interpretation of your problem, georg, your example should have been like this:

Visual Basic:
Dim GSTRate As Double = 0.1
Call GSTCalculation("GSTRate * 100")

Visual Basic:
Function GSTCalculation (ByVal s As String) As Double
'-Left(s,7) should give "GSTRate". MagicConverter should evaluate "GSTRate" to its assigned value 0.1
Return MagicConverter(Left(s,7)) * CInt(Right(s,3))
End Function

That's why his post was titled "Converting Strings to variables" because he wants to convert the string "GSTRate" to the assigned value of the GSTRate variable.

I've read an article that solves this problem by using J#. I forgot the exact method (eval, I guess) but before diving into it, just a suggestion, georg. How about passing in the value of GSTRate to the function call instead? So that instead, you'll have something like:


Visual Basic:
Dim GSTRate As Double = 0.1
Call GSTCalculation("0.1 * 100")

Won't that be simpler?
 
Thanks for understanding me JABE.

You are right. I could pass 0.1 * 100 to the function, but then I lose the flexibility of being able to set the GST (Australian Sales Tax) in a Global Variable.

The real scenario involves passing row and column co-ordinates for a cell, against which the GST amount would be calculated.

This problem is something that I have been thinking about for a while, and I was hoping that VB.Net might have the solution.

MS Access used to have something in the same league, where you could pass a string representing a Sub to the function: Application.Run("MySubProcedure").

Thanks for the discussion.
 
Yeah in VB6 and Access you can use CallByName().

There is a .NET equivalent, however I can't think of it at the moment.
Funny thing is that's what I assumed you wanted from your title but then got somewhat redirected from the post. Sorry.
 
Use the Type.InvokeMember() method to call a method by name. Here is a sample CallByName replacement function:
Visual Basic:
Private Sub MethodByName(ByVal obj As Object, ByVal method As String, ByVal args As String())
        Dim t As Type

        t = obj.GetType
        t.InvokeMember(method, Reflection.BindingFlags.InvokeMethod, t.DefaultBinder, obj, args)
End Sub
You need to retrieve the type of the object (object.GetType()) and use its InvokeMember() method to call a member of it.

The 'BindingFlags.InvokeMethod' binding flag tells it to invoke the method, and the DefaultBinder for the type is simply used to find the method you are trying to invoke.

Visual Basic:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MethodByName(TextBox1, "AppendText", New Object() {"Appended Text!"})
End Sub
That would append some text to TextBox1.
 
Back
Top