• [Visual Basic 10] - Problems converting String to Double
    14 replies, posted
I currently am making a small little program that takes a string with variables (For Right Now, just "X"), and exports the data an external graph [i](not important information)[/i] As I was told, Strings with Numbers or Equations(IE 9 + 3) could be solved and converted to Integers/Doubles easily, Yet it seems to be having trouble here [code] Dim Arguments(200) as double Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'Send information Dim STR, Temp As String Dim Degree, X As Double 'Get the RAW string STR = TextBox1.Text.ToUpper 'Make sure the string is legitimate For X = 0 To STR.Length - 1 Temp = STR.Substring(X, 1) If IsNumeric(Temp) Or Temp = "X" Or Temp = "." Or Temp = " " Or Temp = "+" Or Temp = "-" Or Temp = "/" Or Temp = "*" Or Temp = "^" Then 'Clear ElseIf Temp = "Y" Or Temp = "Z" Or Temp = "A" Or Temp = "B" Or Temp = "C" Or Temp = "D" Then 'Change to X STR.Insert(X, "X") Else MsgBox("The entered equation could not handle '" & STR.Substring(X, 1) & "'. Fix this Character and Retry") Exit Sub End If Next Temp = 0 'Find out what it do. 'Degree = STR.IndexOf((vbInteger.ToString & "X"), 0) FofX = STR 'Accept X as arguments For X = 0 To 200 STR = STR.Replace("X", " * " & X) STR = STR.Trim() STR = ("1 " & STR) 'The Strinng should now be solvable Arguments(X) = Double.Parse(STR) 'Wont convert? Next End Sub [/code] The error I get is similar to this [b]"Conversion from string "(0)^2 + 49(0) + 7" to type 'Double' is not valid."[/b] (When X^2 + 49x + 7 was entered) Im not sure what Im doing wrong... Any Ideas? [editline]25th January 2011[/editline] Hell Ill even listen to a theory, shits ridiculous... [editline]26th January 2011[/editline] *Whistles* 11 people of presumably basic skills in VB cant even get this one. Im boned. :suicide: [editline]26th January 2011[/editline] It seems that VB throws a fit when It tries to get the sum of a string that is literal IE ("6 + 7") rather than (6 + 7). However it assues 49(0) is an array without the quotation marks.... An easy fix.. Problem solved right, hahaha Not at all. I need to find a way to get the literal string im reciving from a text box, to a straight up equation still...
you need to parse it, rearrange the tokens into postfix, and evaluate it
Integer.Parse turns a sting "763" into a number 763, that should be what you need
I always just used Convert.ToDouble
Whoever told OP that Double.Parse (or Double.TryParse) would parse expressions was lying to him / an idiot. Seems he could not figure this out himself. Next time I suggest you look at documentation. Seening as Visual Basic 10 would be part of .net, your first search should have been on MSDN. Which find this. [url]http://msdn.microsoft.com/en-us/library/fd84bdyt.aspx[/url] I also suggest you look into C# as well. Oh, and Goz3rr, Convert.ToDouble calls Double.Parse. It checks for null, which is the first real check done anyways.
Well shit sailor, Maybe im missing something; It kind of looks like you sent me a link, telling me to [i]use[/i] Double.Parse for this. Contradicting isn't it? [editline]26th January 2011[/editline] - Updated Code Snip, Minor changes in changing STR's formating
[QUOTE=Nova Prospekt;27674756]Well shit sailor, Maybe im missing something; It kind of looks like you sent me a link, telling me to [i]use[/i] Double.Parse for this. Contradicting isn't it? [editline]26th January 2011[/editline] - Updated Code Snip, Minor changes in changing STR's formating[/QUOTE] What I am trying to say is that Double.Parse does not solve equations. You will need to either code an equation solver yourself, or find one.
God damn ass raped :(
Be sure that you use some documentation so you are aware what the function does and what parameters it takes. In this case, it takes one string and outputs one number.
[QUOTE=Nova Prospekt;27663780] (When X^2 + 49x + 7 was entered) [/QUOTE] Quadratic Equation Solver I see. We've all been there :downs: I don't know the answer, but I think I have one in C# lying around if you want it...
[QUOTE=ShaRose;27667321] Oh, and Goz3rr, Convert.ToDouble calls Double.Parse. It checks for null, which is the first real check done anyways.[/QUOTE] What does it matter if it works? I don't see the problem here.
[QUOTE=Goz3rr;27706492]What does it matter if it works? I don't see the problem here.[/QUOTE] A very minute addition to call time. I mentioned it because it seemed that you thought it did something extra.
[QUOTE=Loli;27697351]Quadratic Equation Solver I see. We've all been there :downs: I don't know the answer, but I think I have one in C# lying around if you want it...[/QUOTE] Well considering the possibility of VB running codes of other languages, That would be appreciated.
I found this. It's for C++. [URL]http://warp.povusers.org/FunctionParser/[/URL] For C# [URL]http://www.bestcode.com/html/bcparser_net.html[/URL] This one seems interesting. [url]http://www.codeproject.com/KB/recipes/FastMathParser.aspx[/url]
[QUOTE=Pepin;27788809]I found this. It's for C++. [URL]http://warp.povusers.org/FunctionParser/[/URL] For C# [URL]http://www.bestcode.com/html/bcparser_net.html[/URL] This one seems interesting. [url]http://www.codeproject.com/KB/recipes/FastMathParser.aspx[/url][/QUOTE] If it's .net, it will work for the OP. It even comes with a VB sample.
Sorry, you need to Log In to post a reply to this thread.