<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>RBDevZone &#187; RBScript</title>
	<atom:link href="http://www.rbdevzone.com/category/rbscript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rbdevzone.com</link>
	<description>The REALbasic Resource</description>
	<lastBuildDate>Tue, 31 Aug 2010 19:55:19 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Creating an Eval function using RBScript</title>
		<link>http://www.rbdevzone.com/2008/01/creating-an-eval-function-using-rbscript/</link>
		<comments>http://www.rbdevzone.com/2008/01/creating-an-eval-function-using-rbscript/#comments</comments>
		<pubDate>Mon, 21 Jan 2008 01:52:47 +0000</pubDate>
		<dc:creator>Paul Lefebvre</dc:creator>
				<category><![CDATA[RBScript]]></category>
		<category><![CDATA[Source Code]]></category>

		<guid isPermaLink="false">http://www.rbdevzone.com/2008/01/creating-an-eval-function-using-rbscript/</guid>
		<description><![CDATA[On the REAL Software forums, someone posted a question about Eval functionality in REALbasic.  In other languages, the Eval function is used to evaluate expressions.  REALbasic doesn&#8217;t have a direct equivalent, but it does have RBScript which allows you to write and run your own code within your REALbasic application.  With careful [...]]]></description>
			<content:encoded><![CDATA[<p>On the <a href="http://forums.realbasic.com/viewtopic.php?t=19431">REAL Software forums</a>, someone posted a question about Eval functionality in REALbasic.  In other languages, the Eval function is used to evaluate expressions.  REALbasic doesn&#8217;t have a direct equivalent, but it does have RBScript which allows you to write and run your own code within your REALbasic application.  With careful design, we can build our own implementation of Eval using RBScript, so let&#8217;s get started.</p>
<p>First, let&#8217;s understand what Eval does.  Essentially, it takes as input a string that contains an expression to evaluate.  Usage is like this: <strong>result = Eval(expression)</strong>.  For our purposes, we can return the result as a Variant.</p>
<p>In REALbasic, we&#8217;ll want a subclass of RBScript, so create a new class, which we&#8217;ll call <strong>Evaluator</strong>, and set its super to RBScript.  To this class, add a private property: <strong>mResult As Variant</strong>.  We&#8217;ll use this value to return the result of the evaluation.</p>
<p>Now add a new public method called Eval with this code:</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #000080;">Function</span> Eval(expression <span style="color: #000080;">As</span> <span style="color: #000080;">String</span>) Returns <span style="color: #000080;">Variant</span>
  <span style="color: #000080;">Dim</span> source <span style="color: #000080;">As</span> <span style="color: #000080;">String</span>
&nbsp;
  source = <span style="color: #800000;">&quot;Dim expr As Variant = &quot;</span> + expression + EndOfLine + _
  <span style="color: #800000;">&quot;Print(expr)&quot;</span>
&nbsp;
  Self.Source = source
&nbsp;
  Self.Run
&nbsp;
  Return mResult
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span></pre></div></div>

<p>This code creates a simple line of REALbasic code that will parse the expression and runs this code using RBScript.  The result it output using the Print command which will call the <strong>Print</strong> event in the Evaluator class.  Speaking of which, add this code to the <strong>Print</strong> event:</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;">mResult = msg</pre></div></div>

<p>This will assign the value that was output with Print to the property we created so that we can return it.</p>
<p>That&#8217;s it.  Our Eval function is now ready to use.  To test it, create a Window with two EditFields (ExpressionField and ResultField) and a PushButton and add this code to the button&#8217;s Action event:</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;">  <span style="color: #000080;">Dim</span> result <span style="color: #000080;">As</span> <span style="color: #000080;">Variant</span>
&nbsp;
  <span style="color: #000080;">Dim</span> myEval <span style="color: #000080;">As</span> <span style="color: #000080;">New</span> Evaluator
  result = myEval.Eval(ExpressionField.Text)
&nbsp;
  ResultField.Text = result.StringValue</pre></div></div>

<p><a href="http://www.rbdevzone.com/wp-content/uploads/2008/01/evaluatorrbp.zip">Download Evaluator project</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rbdevzone.com/2008/01/creating-an-eval-function-using-rbscript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
