Dim Answer as Double
Answer = Function("(10 + 6) / 8")
where Answer gets the value 2. I seem to recall a function that could do
this in VBA. Thanks in advance!
John
Hope is that what you are asking for
Haitham Salama
MCP, MCSD, MCDBA
Applications Development Manager
Miraco Carrir (Egypt)
"John S. Ford" <johnsfor...@hotmail.com> wrote in message
news:e#FHiDsgCHA.1996@tkmsftngp10...
That's cool!
Tom
"Haitham Salama" <Haitham...@miraco.com.eg> wrote in message
news:#iIFluxgCHA.2256@tkmsftngp12...
CInt and CDbl both throw exceptions:
"Cast from string "(10 + 6)/8" to type 'Integer' is not valid." or "Cast
from string "(10 + 6)/8" to type 'Double' is not valid."
the Value() function you mentioned doesn't appear to be supported by Visual
Basic .NET. Is there a namespace I have to import before I can use it?
John
"Haitham Salama" <Haitham...@miraco.com.eg> wrote in message
news:#iIFluxgCHA.2256@tkmsftngp12...
Dim dtCalculator As New System.Data.DataTable()
MsgBox(dtCalculator.Compute("((10 + 6) / 8)", ""))
Just make sure you have closing braces or you will get an
exception.
-Chris
>.
>
That worked perfectly but Jeez! How was I supposed to find that solution?
I can't seem to figure out how to research these problems on my own through
the .NET Framework. How did you find that answer?
John
"Chris Balmer" <balm...@msu.edu> wrote in message
news:4bf501c2835d$336269c0$36ef2ecf@tkmsftngxa12...
-Chris Balmer
>.
>
Thanks for the help. Unfortunately, this technique doesn't seem to work
with the exponentiation operator ("^"). It doesn't recognize expressions
such as 5 ^ 2 let alone trig functions. Still doesn't work even if I import
the System.Math namespace. Any other ideas?
John
"Chris Balmer" <balm...@msu.edu> wrote in message
news:55c201c28361$cb0230e0$2ae2...@phx.gbl...
Cheers,
Jason
"John S. Ford" <johnsfor...@hotmail.com> wrote in message
news:#vBLUn3gCHA.1516@tkmsftngp09...
> Dear Chris,
>
> Thanks for the help. Unfortunately, this technique doesn't seem to work
> with the exponentiation operator ("^"). It doesn't recognize expressions
> such as 5 ^ 2 let alone trig functions. Still doesn't work even if I
import
> the System.Math namespace. Any other ideas?
[Snip]
--------------------
Dim results As CompilerResults
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdBuild.Click
Dim vbProvider As VBCodeProvider = New VBCodeProvider()
Dim compiler As ICodeCompiler = New
VBCodeProvider().CreateCompiler()
Dim parameters As CompilerParameters = New CompilerParameters()
parameters.GenerateInMemory = True
parameters.ReferencedAssemblies.Add("System.dll")
parameters.ReferencedAssemblies.Add("Microsoft.VisualBasic.Compatibility.dll
")
parameters.ReferencedAssemblies.Add("Microsoft.VisualBasic.dll")
parameters.WarningLevel = 99
results = compiler.CompileAssemblyFromSource(parameters, _
"Imports System" +
vbCrLf + _
"Imports
Microsoft.VisualBasic" + vbCrLf + _
"NameSpace QED" + vbCrLf
+ _
"Class Macro" + vbCrLf +
_
Me.SourceCode.Text +
vbCrLf + _
"End Class" + vbCrLf + _
"End Namespace")
lstErrors.Items.Clear()
If results.Errors.Count > 0 Then
Dim cError As CompilerError
For Each cError In results.Errors
lstErrors.Items.Add(cError.ToString)
Next
Else
lstErrors.Items.Add("Compile successful")
End If
End Sub
Private Sub cmdRun_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdRun.Click
Dim ass As [Assembly]
ass = results.CompiledAssembly
Dim t As Type
t = ass.GetType("QED.Macro")
Dim o As Object
o = Activator.CreateInstance(t)
Dim res As Object
res = o.GetType().GetMethod("Test").Invoke(Nothing, Nothing)
Console.WriteLine(res.ToString())
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
SourceCode.Text = _
" Shared Function Test() as String" + vbCrLf + _
" Console.WriteLine(""From Main"")" + vbCrLf + _
" Return(""Jason"")" + vbCrLf + _
" End Function"
End Sub
Stephen Kent
"John S. Ford" <johnsfor...@hotmail.com> wrote in message
news:e#FHiDsgCHA.1996@tkmsftngp10...
Function Function1Eval(ByVal X As Double) As Double
Try
AxScriptControl1.ExecuteStatement("X=" & X)
Function1Eval = CSng(AxScriptControl1.Eval(txtFunction1.Text))
Catch exc As Exception
Throw New Exception("Can't evaluate function at X=" & X)
End Try
End Function
"Stephen Kent" <sk...@l3digital.com> wrote in message
news:#nEfUFBhCHA.2508@tkmsftngp08...
int i=val(string).
with regards,
J.V.Ravichandran
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
John
"Ravichandran J.V." <jvravic...@yahoo.com> wrote in message
news:#CS9tOJhCHA.2472@tkmsftngp12...
John,
Here is an idea, that would avoid using COM interop... JScript has an eval
function that will do just what your doing, in fact it will run snippets of
arbitrary JScript code. So here is what I suggest (since the MS Scripting
Runtime is often turned off, and because you should avoid COM interop if
possible) - make a dll in JScript that exposes the Eval function for you...
You could do something like this:
1. Create a class in JScript:
// JScript source code
class EvalClass
{
function Evaluate(expression)
{
return eval(expression);
}
}
2. Compile it to a dll:
jsc /target:library evaluator.js ' Compiles to evaluator.dll
3. Reference evaluator.dll and Microsoft.JScript.dll in your VB project.
4. Write code that looks like this:
Dim ec As New EvalClass()
Dim expr As String = "(10 + 6) / 8"
Dim res As Double = CType(ec.Evaluate(expr), Double)
Console.WriteLine("{0} = {1}", expr, res)
You gotta love .NET!
HTH,
Tom Shelton
John,
I mean it, I'm just a lowly script kiddie and C# is making so
much sense to me I can't believe how unjustified my former
concerns were.
--
<%= Clinton Gallagher
A/E/C Consulting, Web Design, e-Commerce Software Development
Wauwatosa, Milwaukee County, Wisconsin USA
NET csgal...@REMOVETHISTEXTmetromilwaukee.com
URL http://www.metromilwaukee.com/clintongallagher/
LaGarde StoreFront 5 Affiliate: e-Commerce Software Development
SEE: http://www.storefront.net/default.asp?REFERER=-201499070
"John S. Ford" <johnsfor...@hotmail.com> wrote in message
news:e#FHiDsgCHA.1996@tkmsftngp10...
You are wrong... Most of it does apply. The problem is that most of the
answers endorse using COM interop to get the desired results, but you will
notice that I posted a solution that makes use of a .NET assembly produced
in another .NET language; JScript.NET, were the desired functionality is
native. You could do this in pure VB.NET or even C#, but it would be much
more difficult then just referencing a simple assembly created in
JScript.NET.
Tom Shelton
John
"Tom Shelton" <to...@dakcs.com> wrote in message
news:uAAffcOhCHA.2592@tkmsftngp09...
John,
I'm pretty sure you do actually. If I remember right, VB.NET standard still
installs the entire Framework SDK - which the JScript.NET compiler is part
of. Even with VS.NET EA you have to run the jscript compiler from the
command line. Do a search on your system for jsc.exe, I'd be willing to bet
it is there...
Really, if you can use this method, it will be much better from a
performance, and from a deployment standpoint.
Tom Shelton
//where Answer gets the value 2. I seem to recall a function that could
do
//this in VBA. Thanks in advance!
//dJohn
John,
I tried my hand with the problem using C# and I have come up with the
following program. See if it is of any use to you.
Remember, though, that the program works only for the given string
"(10+6)/8)" and uses INT32 conversion. For Double conversion use
appropriate conversion methods.
using System;
class a
{
public static string s;
public a()
{
s=null;
}
public static void Test(string str)
{
char ch=' ';
int ctr=0;
ch=str[ctr];
while (ch!=null)
{
ctr++;
}
Console.WriteLine(ctr);
}
public static void Main()
{
Test(s);
//Dim Answer as Double
//Answer = Function("(10 + 6) / 8")
//where Answer gets the value 2. I seem to recall a function that could
do
//this in VBA. Thanks in advance!
//dJohn
John,
I tried my hand with the problem using C# and I have come up with the
following program. See if it is of any use to you.
Remember, though, that the program works only for the given string
"(10+6)/8)" and uses INT32 conversion. For Double conversion use
appropriate conversion methods. I have used lots of variables as I coded
in a hurry without much thought to the finer nuances. I hope you don't
mind the disregard to naming conventions either.
using System;
class a
{
public static string s;
public a()
{
s=null;
}
public static void Test(string str)
{
char ch=' ';
int ctr=1,plusctr=0,slashctr=0,result=0;
string s1=null,s2=null,s3=null,s4=null;
bool plusSign=false,divSign=false,flag=false;
while (ch!='\n')
{
ch=str[ctr];
if (ch!='('){
if (ch!=')'){
s1+=ch;
}
}
ctr++;
}
//Console.WriteLine( s1);
s1+='\n';
ctr=0;
while (flag==false){
if (s1[ctr]=='+'){
//Console.WriteLine(s2.ToInt32());
int plus=s2.ToInt32();
plusSign=true;
plusctr=ctr;
flag=true;
if (s1[ctr]=='-'){
if ( s1[ctr]=='*'){
if (s1[ctr]=='/'){
}
}
}
}
else{
s2+=s1[ctr];
}
ctr++;
}
ctr=plusctr+1;
while (flag==true)
{
if (s1[ctr]=='/'){
slashctr=ctr;
divSign=true;
flag=false;
}
else{
s3+=s1[ctr];
}
ctr++;
}
ctr=slashctr+1;
while (flag==false)
{
if (s1[ctr]=='\n')
{
flag=true;
}
else{
s4+=s1[ctr];
}
ctr++;
}
if (plusSign==true)
{
result=s2.ToInt32()+s3.ToInt32();
}
if (divSign==true)
{
result=result/s4.ToInt32();
}
Console.WriteLine( result);
}
public static void Main()
{
Test("(10+6)/8\n");
}
}
//Dim Answer as Double
//Answer = Function("(10 + 6) / 8")
//where Answer gets the value 2. I seem to recall a function that could
do
//this in VBA. Thanks in advance!
//dJohn
with regards,
//where Answer gets the value 2. I seem to recall a function that could
do
//this in VBA. Thanks in advance!
//dJohn
Sorry for posting twice or thrice. Here is the correct one. The first
was a mispost; the second was a correct post too but I was not sure if
it was posted or not; this is a confirmation post. Sorry for any
confusion caused !
with regards,
"clintonG" <csgal...@REMOVETHISTEXTwi.rr.com> wrote in message
news:#SWEVh5hCHA.1364@tkmsftngp11...