Results 1 to 14 of 14

Thread: [RESOLVED] reading a mathematical operation

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2011
    Posts
    154

    Resolved [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 ""

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: reading a mathematical operation

    val + cint don't process mathematical operations in strings

    but you can do this:

    vb Code:
    1. dim dt as new datatable
    2. x=dt.compute(replace(label1.text,"y",i))

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2011
    Posts
    154

    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
    Last edited by ugur; Dec 15th, 2011 at 04:52 PM.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: reading a mathematical operation

    vb Code:
    1. x = dt.Compute(Replace(Label1.Text, "k", i), Nothing)

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2011
    Posts
    154

    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

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: reading a mathematical operation

    where do you declare dt?
    try combining post #2 + post #4 + it'll work

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jan 2011
    Posts
    154

    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

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jan 2011
    Posts
    154

    Re: reading a mathematical operation

    do you understand why it says that ?

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jan 2011
    Posts
    154

    Re: reading a mathematical operation

    please guys i need help

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jan 2011
    Posts
    154

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

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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.
    My usual boring signature: Nothing

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: reading a mathematical operation

    my guess is operator precedence. i.e:

    vb Code:
    1. MsgBox(1 + 2 * 5)
    2. MsgBox((1 + 2) * 5)

    give different answers

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Jan 2011
    Posts
    154

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

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Jan 2011
    Posts
    154

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width