-
Dec 4th, 2020, 07:30 AM
#1
Thread Starter
New Member
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?
-
Dec 4th, 2020, 08:01 AM
#2
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
-
Dec 4th, 2020, 11:18 AM
#3
Re: String with parameters to expression
Originally Posted by techgnome
Hopefully someone will come along and know what I'm talking about.
-tg
I'm always hoping for that
My usual boring signature: Nothing
-
Dec 4th, 2020, 01:00 PM
#4
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
-
Dec 4th, 2020, 01:39 PM
#5
Re: String with parameters to expression
The DataTable’s Compute method could do that...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 4th, 2020, 01:42 PM
#6
Re: String with parameters to expression
Code:
Dim dt as New DataTable
txt1.text = dt.Compute(i + j, Nothing)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 4th, 2020, 01:52 PM
#7
Re: String with parameters to expression
Originally Posted by .paul.
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
-
Dec 4th, 2020, 02:57 PM
#8
Re: String with parameters to expression
Originally Posted by techgnome
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)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 4th, 2020, 02:59 PM
#9
Re: String with parameters to expression
Originally Posted by techgnome
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 5th, 2020, 01:15 AM
#10
New Member
Re: String with parameters to expression
Hi,
You have to convert string value to double/integer, Then only the equation works.
-
Dec 5th, 2020, 09:39 AM
#11
Re: String with parameters to expression
Originally Posted by Sanjeevkumar P
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)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 5th, 2020, 02:22 PM
#12
Re: String with parameters to expression
Don't believe that will work with Option Strict On.
-
Dec 5th, 2020, 03:09 PM
#13
Re: String with parameters to expression
Originally Posted by wes4dbt
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 9th, 2020, 05:53 AM
#14
Thread Starter
New Member
Re: String with parameters to expression
Originally Posted by .paul.
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
-
Dec 9th, 2020, 06:39 AM
#15
Re: String with parameters to expression
Where do you set i and j?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 9th, 2020, 06:42 AM
#16
Thread Starter
New Member
Re: String with parameters to expression
Originally Posted by .paul.
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
-
Dec 9th, 2020, 07:57 AM
#17
Re: String with parameters to expression
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 9th, 2020, 08:01 AM
#18
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 9th, 2020, 08:23 AM
#19
Thread Starter
New Member
Re: String with parameters to expression
Originally Posted by .paul.
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!
-
Dec 9th, 2020, 08:36 AM
#20
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
-
Dec 9th, 2020, 09:03 AM
#21
Thread Starter
New Member
Re: String with parameters to expression
Originally Posted by techgnome
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|