[RESOLVED] reading a mathematical operation
hello all :)
im back! :)
heres the problem:)
lets assume i have a label1 which its text is "(y+1)"
i defined an integer, lets assume its x ...
i made some operations up and lets say there can be diffrent results
and i want to replace y with that math ops result and then define x as it writes in label1...
e.g. label1.text="y+1"
dim x as integer, i as integer
i=textbox1.text
' this part is my thoughts..
x=?????(replace(label1.text,"y",i)))
'i tried val or cint clng instead of "???" but not the result that i want showed up....
please i need answers :)
ps: i just realized i made a bit more funny faces than i need ":)"
Re: reading a mathematical operation
val + cint don't process mathematical operations in strings
but you can do this:
vb Code:
dim dt as new datatable
x=dt.compute(replace(label1.text,"y",i))
Re: reading a mathematical operation
as it says a filter is required but i didnt understand the process so i dont know what to write as a filter
and also it says nulreference when i wrote this ...:
Code:
dt.Compute((Replace(Label1.Text, "k", i)), 100)
i couldnt understand this process also the whole new system.nullreferenceexception thing...:S
Re: reading a mathematical operation
vb Code:
x = dt.Compute(Replace(Label1.Text, "k", i), Nothing)
Re: reading a mathematical operation
but its still says nullreference for dt
Code:
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
Dim i As Integer, array(CInt(en) - CInt(ka)) As Integer, ii As Integer, rslt As Integer, dt As DataTable
For i = CInt(ka) To CInt(en)
array(i - CInt(ka)) = dt.Compute((Replace(Label1.Text, "k", i)), Nothing)
Next
For ii = 0 To UBound(array)
rslt = rslt + array(ii)
Next
Label1.Text = CStr(rslt)
End Sub
End Class
here s my progress
Re: reading a mathematical operation
where do you declare dt?
try combining post #2 + post #4 + it'll work
Re: reading a mathematical operation
Code:
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
Dim i As Integer, array(CInt(en) - CInt(ka)) As Integer, ii As Integer, rslt As Integer
Dim dt As DataTable
For i = CInt(ka) To CInt(en)
array(i - CInt(ka)) = dt.Compute((Replace(Label1.Text, "k", i)), Nothing)
Next
For ii = 0 To UBound(array)
rslt = rslt + array(ii)
Next
Label1.Text = CStr(rslt)
End Sub
i already did but it s not working maybe for another reason
Re: reading a mathematical operation
do you understand why it says that ?
Re: reading a mathematical operation
Re: reading a mathematical operation
Code:
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
Dim i As Integer, array(CInt(en) - CInt(ka)) As Integer, ii As Integer, rslt As Integer
Dim dt As DataTable
dt = New DataTable
For i = CInt(ka) To CInt(en)
array(i - CInt(ka)) = dt.Compute((Replace(Label1.Text, "k", i)), Nothing)
Next
For ii = 0 To UBound(array)
rslt = rslt + array(ii)
Next
Label1.Text = CStr(rslt)
rslt = 0
End Sub
i got the problem but now it doesnt work well...
the solution isnt what i want it to be (en is 10 and ka is 1 i couldnt find a mistake but i dont know anything about the datatable classes so im not sure whats going on...
Re: reading a mathematical operation
I can't say that I'm thrilled with this line:
array(CInt(en) - CInt(ka))
If you are sure that en>ka, it will work ok, but you have to be sure.
As for the overall problem, what .Paul. suggested is certainly clever, but it seems kind of like a dead end. It will work for simple equations, but will get increasingly cumbersome as you increase the complexity of the equation. I assume that there is a point to this that requires the equation from the label, or else there is probably a better way to get to the end you want. I would say that a more complete explanation of the actual problem would result in a better solution.
As for it not working well, what does that mean? Are you getting the wrong answer, or is it just slow? If you are getting the wrong answer, then it is time to put a breakpoint on that first For loop and step through the code taking a look at what is happening with each step. If you haven't learned about breakpoints, then now is certainly the time to learn.
Re: reading a mathematical operation
my guess is operator precedence. i.e:
vb Code:
MsgBox(1 + 2 * 5)
MsgBox((1 + 2) * 5)
give different answers
Re: reading a mathematical operation
@paul
thanks but its only k + 1 so there s no operator precedence
@shagy
dont worry about the array because i prevented the other oportunity...
and i meant it doesnt give the result that it should give...i get 1 instead of anything.. whatever i write i get 1....
Re: reading a mathematical operation
i fixed it by the way i gues i forgot checking a code basically after a break...its just variable issues