[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.sdk

Re: Executing System math functions programmatically

Richard Grimes [MVP]

8/13/2003 8:39:00 PM

hmmm,

its possible to do this using reflection, something like this (pseudo code,
I haven't compiled it):

/* need to create an array of doubles, I cannot think of an easier way*/
int size = 0;
object[] params;
if (text3.Text.Length > 0) size++;
if (text2.Text.Length > 0) size++;
if (text1.Text.Length > 0) size++;
object[] params = new object[size];
if (size == 3) params[2] = Double.Parse(text3.Text);
if (size >= 2) params[1] = Double.Parse(text2.Text);
if (size >= 0) params[0] = Double.Parse(text1.Text);

/*get the method from Math class*/
MethodInfo mi = Math.GetType().GetMethod(text4.Text);
if (mi == null) throw MethodNotFoundException("text4.Text");
object result = mi.Invoke(null, params);
text5.Text = result.ToString();

If you have a custom class with methods you want to call, you could use a
similar technique.

Richard
--
my email evpuneqt@zicf.bet is encrypted with ROT13 (www.rot13.org)


Venkateswaran Ganesan wrote:
> Hi,
>
> Does any one know how to execute the system defined
> functions programmatically?
>
> something like Execute("String") where string is a formula
> with right parameter values in it. for example Execute("AVG
> (1,2,3)") should return 2.
>
> Here is the issue, in my application, i have an user
> application form which has 5 textboxes where first three
> named as field1, field2 and field 3 respectively. If an
> user enters any of the system defined function like Sum or
> Avg in 4th text box, then i need to show the resultant in
> 5th text box. Hence i need to first check whether the
> function is available in system function and secondly pass
> the parameters to the function from field1 to 3 and show
> the resultant value in 5th text box.
>
>
> Thanks in advance,
> venkat