Results 1 to 21 of 21

Thread: String with parameters to expression

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2020
    Posts
    5

    String with parameters to expression

    Hi Guys,
    im new here.

    I have form with 2 textboxes (txt1,txt2) and 1 button (btnEnd)

    I need help with code looking something like this:

    Private Sub btnEnd_Click(sender As Object, e As EventArgs) Handles btnEnd.Click

    Dim i As Integer = 3
    Dim j As Integer = 4
    Dim str As String

    str = txt1.text ' txt1.text = i + j if I write 3 + 4 the result is ok 7. But with "i" and "j" don't work.
    txt2.Text = AxScriptControl1.Eval(str)

    End Sub
    I' had trying with Script control but isn't work.
    How to send parameters to the equation?

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: String with parameters to expression

    The script control is pretty limited. It's also pretty stupid. I don't mean that in a bad way, just in that it only knows what you tell it. You never gave it i or j.... so it doesn't know what those are or what they mean. They need to be passed in. And it is possible. I wrote an entire scripting module based on it back in VB6 days....

    That said... I see the Ax on the front of it, and I see that you posted in the .NET section... so I'm thinking you're actually running .NET here.... and if that's the case, the script control is the wrong screwdriver. In stead you should be looking into the ...ugh... Ican't remember it exactly DOM-somthing or other.... it's much more easily used in .NET and more powerful as well. Hopefully someone will come along and know what I'm talking about.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: String with parameters to expression

    Quote Originally Posted by techgnome View Post
    Hopefully someone will come along and know what I'm talking about.


    -tg
    I'm always hoping for that
    My usual boring signature: Nothing

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: String with parameters to expression

    hehehe.... Dangit, you were my last hope there SH!

    Anyways, I found it... CodeDOM!... https://www.codeproject.com/articles...ng-the-codedom

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: String with parameters to expression

    The DataTable’s Compute method could do that...

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

    Re: String with parameters to expression

    Code:
    Dim dt as New DataTable
    txt1.text = dt.Compute(i + j, Nothing)

  7. #7
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: String with parameters to expression

    Quote Originally Posted by .paul. View Post
    Code:
    Dim dt as New DataTable
    txt1.text = dt.Compute(i + j, Nothing)
    is it hte datatable that computes i + j... or VB before passing that result to .Compute? I'm asking because your i + j is jsut vb code and not in a string, which is what the OP is after.... wouldn't the DT still need to know what i & j are in order to compute it?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: String with parameters to expression

    Quote Originally Posted by techgnome View Post
    is it hte datatable that computes i + j... or VB before passing that result to .Compute? I'm asking because your i + j is jsut vb code and not in a string, which is what the OP is after.... wouldn't the DT still need to know what i & j are in order to compute it?

    -tg
    I got it wrong. It works with strings, such as...

    Code:
    Dim str As String = "3 + 4"
    Dim dt as New DataTable
    txt1.text = dt.Compute(str, Nothing)

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: String with parameters to expression

    Quote Originally Posted by techgnome View Post
    is it hte datatable that computes i + j... or VB before passing that result to .Compute? I'm asking because your i + j is jsut vb code and not in a string, which is what the OP is after.... wouldn't the DT still need to know what i & j are in order to compute it?

    -tg
    It wouldn't work with...

    Code:
    Dim str As String = "i + j"
    But it would work with...

    Code:
    Dim str As String = i & " + " & j

  10. #10
    New Member
    Join Date
    Dec 2020
    Posts
    2

    Re: String with parameters to expression

    Hi,

    You have to convert string value to double/integer, Then only the equation works.

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: String with parameters to expression

    Quote Originally Posted by Sanjeevkumar P View Post
    Hi,

    You have to convert string value to double/integer, Then only the equation works.
    you have to convert variables to values...

    Code:
    Dim i As Integer = 3
    Dim j As Integer = 4
    
    Dim str As String = i & " + " & j
    Dim dt as New DataTable
    txt1.text = dt.Compute(str, Nothing)

  12. #12
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,196

    Re: String with parameters to expression

    Don't believe that will work with Option Strict On.

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: String with parameters to expression

    Quote Originally Posted by wes4dbt View Post
    Don't believe that will work with Option Strict On.
    The String concatenation is ok with Option Strict on, but dt.Compute returns an Object, and you'd need .ToString

  14. #14

    Thread Starter
    New Member
    Join Date
    Dec 2020
    Posts
    5

    Re: String with parameters to expression

    Quote Originally Posted by .paul. View Post
    you have to convert variables to values...

    Code:
    Dim i As Integer = 3
    Dim j As Integer = 4
    
    Dim str As String = i & " + " & j
    Dim dt as New DataTable
    txt1.text = dt.Compute(str, Nothing)
    Thx all for reply.

    Yes. Thats works. But my question is about

    Dim str as string
    str = txt1.text
    txt2.text = dt.compute(str, Nothing)

    when i fill txt1.text= i & " + " & j
    that dosn't work

  15. #15
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: String with parameters to expression

    Where do you set i and j?

  16. #16

    Thread Starter
    New Member
    Join Date
    Dec 2020
    Posts
    5

    Re: String with parameters to expression

    Quote Originally Posted by .paul. View Post
    Where do you set i and j?
    Code:
    Private Sub btnEnd_Click(sender As Object, e As EventArgs) Handles btnEnd.Click

    Dim i As Integer = 3
    Dim j As Integer = 4
    Dim str As String

    str = txt1.text
    Dim dt as New DataTable
    txt2.text = dt.Compute(str, Nothing)

    End Sub

  17. #17
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: String with parameters to expression

    Google reflection...

  18. #18
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: String with parameters to expression

    Or if it really is as simple as i + j...

    Code:
    Private Sub btnEnd_Click(sender As Object, e As EventArgs) Handles btnEnd.Click
    
        Dim i As Integer = 3
        Dim j As Integer = 4
        Dim str As String
    
        str = txt1.text
        Dim dt as New DataTable
        txt2.text = dt.Compute(str.replace("i", i).replace("j", j), Nothing)
    
    End Sub

  19. #19

    Thread Starter
    New Member
    Join Date
    Dec 2020
    Posts
    5

    Re: String with parameters to expression

    Quote Originally Posted by .paul. View Post
    Or if it really is as simple as i + j...

    Code:
    Private Sub btnEnd_Click(sender As Object, e As EventArgs) Handles btnEnd.Click
    
        Dim i As Integer = 3
        Dim j As Integer = 4
        Dim str As String
    
        str = txt1.text
        Dim dt as New DataTable
        txt2.text = dt.Compute(str.replace("i", i).replace("j", j), Nothing)
    
    End Sub
    Thanks!!!! Very much!! It's works!

  20. #20
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: String with parameters to expression

    That's only good if it's a simple expression and you know what's in it. Given that it's a text box that is the source of the exptression, what if the user changes it to " x + y" ??? that will break as I & J no longer exist to be replaced. And maybe that's OK, I don't know the situation, but I think the simplistic approach here is potentially asking for trouble.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  21. #21

    Thread Starter
    New Member
    Join Date
    Dec 2020
    Posts
    5

    Re: String with parameters to expression

    Quote Originally Posted by techgnome View Post
    That's only good if it's a simple expression and you know what's in it. Given that it's a text box that is the source of the exptression, what if the user changes it to " x + y" ??? that will break as I & J no longer exist to be replaced. And maybe that's OK, I don't know the situation, but I think the simplistic approach here is potentially asking for trouble.

    -tg
    Yes that true. But user will know which paramter may use. Otherwise i give him err msg.

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