Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I use operator (+, -, / , * ) in a variable

Status
Not open for further replies.

essa2000

Programmer
Dec 6, 2000
299
0
0
CA
Dears;

I want to store operator in a variable so that I can use these provided operator to manipulate two other variables. Problem is that when I store it in a string it gives me character i.e. string and I can't calculate those two variable. Scenario is that If I enter + in a text box then two already entered values are added and so on. I don't want to use Case Select or If / elseif conditions.

Thanks in advance. Muhammad Essa Mughal
Software Engineer
essamughal@yahoo.com
 
"Case Select or If / elseif conditions."

May I ask why?

Transcend
[gorgeous]
 
Just to learn the given task in shortest way. Actually, someone given me that task to do in this way so I can't use case select or if /elseif.

Muhammad Essa Mughal
Software Engineer
essamughal@yahoo.com
 
Ok so basically you have two values already

and if you type + in the textbox you want to add them,
if you type * you want to multiply them etc?

Transcend
[gorgeous]
 
a string is a string. If you want to have a string hold some formula and execute that formula they you have to parse the string out. There isn't any "Shortest Way" to do this in vb. The closest I can point you is there is an undocumented API call that when you pass it a pointer to a string it executes that string as code, like the imediate window.

so if you have 3 text boxes with the first and last with numbers in them and the middle with a operator you'd have to have a select case statement something like this.

Select Case txtOperator.text
Case Is = "+"
dAnswer = Cdbl(txtFirstNumber.text) + Cdbl(txtSecondNumber.text)
Case Is = "-"
dAnswer = Cdbl(txtFirstNumber.text) - Cdbl(txtSecondNumber.text)
.
.
.
End Select

for every operator you'd have to have a case statement.
This type of thing is VERY limited.
 
I have a feeling this a homework problem or puzzle ... especially seeing as you don't want to use select or if statements

Transcend
[gorgeous]
 
Actually, Transcend is feeling in the right direction , actually, office fellow given me that task and I have been thinking on it for long. Now, I posted that question on the tek-tip to get others response on that problem. Case Select solution is very accurate but I am limited to use it. The above post has solved the problem but I need other way. The problem is clear and I am looking for a solution.
Muhammad Essa Mughal
Software Engineer
essamughal@yahoo.com
 
I think strongm is getting me hooked...
Every time I see one of these... 2 three letter words come into mind... Reg Exp... though, I'm still not quite sure how to use it... Sounds like a pretty good challenge though... Sometimes... the BASIC things in life are the best...
cheers.gif

or at least the most fun ;-)
-Josh Stribling
 
if you have office...
use excel to calulate the variables...

say you have 3 textboxes...
Text1 = number
Text2 = +
Text3 = number

use:
Range("A1") = Text1.text
Range("A2") = Text3.text
Range("A3") = "=A1" & Text2 & "A2"

that might work... just for fun ;-)

Good Luck Sometimes... the BASIC things in life are the best...
cheers.gif

or at least the most fun ;-)
-Josh Stribling
 
Use the Microsoft Script Control component. Put it in your form, then use the Eval method:

Dim intTest As Integer
intTest = ScriptControl1.Eval("10+10")

intTest will have the value of 20.

Hope it solve the puzzle :)
 
Here's a VB Eval function:
[tt]
Public Function vbEval(strSource As String) As Variant Dim wsh As Object

Set wsh = CreateObject("MSScriptControl.ScriptControl")
wsh.Language = "vbscript"

vbEval = wsh.Eval(strSource)
Set wsh = Nothing
End Function
 
Oops - lost a line break when I posted the above. There should be one between the function declaration and the Dim wsh line...
 
For Some reason, I knew you would have a a VBScript solution...

I think I understand this one...

Public Function vbEval(strSource As String) As Variant
Dim wsh As Object

Set wsh = CreateObject("MSScriptControl.ScriptControl")
wsh.Language = "vbscript"

vbEval = wsh.Eval(strSource)
Set wsh = Nothing
End Function

BUT... what is the component/reference you need to use???
Microsoft Scripting Runtime?
Microsoft Scriptlet Library?

...??? Sometimes... the BASIC things in life are the best...
cheers.gif

or at least the most fun ;-)
-Josh Stribling
 
You don't need a reference. I'm late binding...

>I knew you would have a a VBScript solution

Heh! Mind you, you've got to stop seeing these things as 'vbscript' solutions. All I'm doing is leveraging the ability of VB to use existing COM components, thus eliminating the necessity to reinvent the wheel. It just so happens that in recent threads the component that has had the necessary functionality has been the VBScript library...
 
ok...

Why do I get an error...

Run-time error '429':

ActiveX component can't create object


>>thus eliminating the necessity to reinvent the wheel
I understand this...
Your Post have been helpful for quick solutions
...But sometimes it is fun to try to reinvent the wheel...
Just think ;-)...
If NO ONE ever tried to reinvent the wheel...
We would not have:
Radials Tires (wood tires suck)
Directional Tires (for higher speeds)
AquaTread Tires (for use on water/rain)
MonsterTruck Tires (just for fun ;-))
Racing Slicks (for Racing...duh...)

So... remember... 1 wheel does not always fit all ;-)

But... it is good to know you have options... THNX Sometimes... the BASIC things in life are the best...
cheers.gif

or at least the most fun ;-)
-Josh Stribling
 
Thanks for help from all the experts. I got the Eval Method.

Muhammad Essa Mughal
Software Engineer
essamughal@yahoo.com
 
hmmmmmmmmmm ... mmmmmmmm

twice already today with strange commentary from a cube ... did the dice roll awkardly?


MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top