georg Posted June 29, 2003 Posted June 29, 2003 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 Quote
*Experts* jfackler Posted June 29, 2003 *Experts* Posted June 29, 2003 Dim StrGST As String Dim DDGST As Double DDGST = CDbl(StrGST) Quote
*Experts* mutant Posted June 29, 2003 *Experts* Posted June 29, 2003 Or :) Convert.ToDouble(yourstring) Quote
*Experts* jfackler Posted June 29, 2003 *Experts* Posted June 29, 2003 Mutants is the .NET method of choice. Better to develop and/or adopt healthy habits... Quote
georg Posted June 29, 2003 Author Posted June 29, 2003 Okaaay, I thought that this would be hard to explain. Another scenario: Imagine you have the function: 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: 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 Quote
Moderators Robby Posted June 29, 2003 Moderators Posted June 29, 2003 I still don't completely understand. Where is the GST value held? In the dataset or some global variable? Quote Visit...Bassic Software
JABE Posted June 29, 2003 Posted June 29, 2003 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: Dim GSTRate As Double = 0.1 Call GSTCalculation("GSTRate * 100") 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: Dim GSTRate As Double = 0.1 Call GSTCalculation("0.1 * 100") Won't that be simpler? Quote
georg Posted June 29, 2003 Author Posted June 29, 2003 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. Quote
Moderators Robby Posted June 29, 2003 Moderators Posted June 29, 2003 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. Quote Visit...Bassic Software
*Experts* Volte Posted June 29, 2003 *Experts* Posted June 29, 2003 Use the Type.InvokeMember() method to call a method by name. Here is a sample CallByName replacement function: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 SubYou 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. 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 SubThat would append some text to TextBox1. Quote
Moderators Robby Posted June 29, 2003 Moderators Posted June 29, 2003 I knew Volte would come through. :) Quote Visit...Bassic Software
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.