Calculator

  • Thread starter Thread starter Rodrigo Ferreira
  • Start date Start date
R

Rodrigo Ferreira

I'm developing a user control with a textbox inside. When i write something
like this:

123 + 456.456 + 456 / 453 * 45 + 4.155

and press ENTER, the control must have to show the result.

I don't know how to start!

Anyone can help me?
 
Rodrigo,

My suggestion would be to get a third party math package. Either that,
or you could parse it apart yourself, but you would have to handle all the
precidence rules yourself (not hard, but not something you should have to
do).

What you could do is create a static method dynamically, something like
this:

public static double Calc()
{
return 123 + 456.456 + 456 / 453 * 45 + 4.155;
}

And then invoke it. It is easy to create a dynamic type to do this.
However, this could cause a good amount of bloat (considering how many times
you have to do this).

Hope this helps.
 
Ok!

But in textbox the type is string! How can i pass this values to decimal in
the same order?


Nicholas Paldino said:
Rodrigo,

My suggestion would be to get a third party math package. Either that,
or you could parse it apart yourself, but you would have to handle all the
precidence rules yourself (not hard, but not something you should have to
do).

What you could do is create a static method dynamically, something like
this:

public static double Calc()
{
return 123 + 456.456 + 456 / 453 * 45 + 4.155;
}

And then invoke it. It is easy to create a dynamic type to do this.
However, this could cause a good amount of bloat (considering how many
times you have to do this).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Rodrigo Ferreira said:
I'm developing a user control with a textbox inside. When i write
something like this:

123 + 456.456 + 456 / 453 * 45 + 4.155

and press ENTER, the control must have to show the result.

I don't know how to start!

Anyone can help me?
 
Rodrigo,

You can use the Convert class to convert the elements in the expression
(numbers) to various numeric types, but that means parsing it apart on your
own.

Taking that string, you could create the code that I laid out for you.
The problem with that is that it is susceptable to injection attacks (if you
create a code segment and compile and run it).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Rodrigo Ferreira said:
Ok!

But in textbox the type is string! How can i pass this values to decimal
in the same order?


Nicholas Paldino said:
Rodrigo,

My suggestion would be to get a third party math package. Either
that, or you could parse it apart yourself, but you would have to handle
all the precidence rules yourself (not hard, but not something you should
have to do).

What you could do is create a static method dynamically, something
like this:

public static double Calc()
{
return 123 + 456.456 + 456 / 453 * 45 + 4.155;
}

And then invoke it. It is easy to create a dynamic type to do this.
However, this could cause a good amount of bloat (considering how many
times you have to do this).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Rodrigo Ferreira said:
I'm developing a user control with a textbox inside. When i write
something like this:

123 + 456.456 + 456 / 453 * 45 + 4.155

and press ENTER, the control must have to show the result.

I don't know how to start!

Anyone can help me?
 
Sorry but,

I don't understand! if you can write a few code it's better to me!


Nicholas Paldino said:
Rodrigo,

You can use the Convert class to convert the elements in the expression
(numbers) to various numeric types, but that means parsing it apart on
your own.

Taking that string, you could create the code that I laid out for you.
The problem with that is that it is susceptable to injection attacks (if
you create a code segment and compile and run it).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Rodrigo Ferreira said:
Ok!

But in textbox the type is string! How can i pass this values to decimal
in the same order?


Nicholas Paldino said:
Rodrigo,

My suggestion would be to get a third party math package. Either
that, or you could parse it apart yourself, but you would have to handle
all the precidence rules yourself (not hard, but not something you
should have to do).

What you could do is create a static method dynamically, something
like this:

public static double Calc()
{
return 123 + 456.456 + 456 / 453 * 45 + 4.155;
}

And then invoke it. It is easy to create a dynamic type to do this.
However, this could cause a good amount of bloat (considering how many
times you have to do this).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


I'm developing a user control with a textbox inside. When i write
something like this:

123 + 456.456 + 456 / 453 * 45 + 4.155

and press ENTER, the control must have to show the result.

I don't know how to start!

Anyone can help me?
 
I've done something!

Thank's anyway!

Greetings,
Rodrigo Ferreira,
Portugal

Rodrigo Ferreira said:
Sorry but,

I don't understand! if you can write a few code it's better to me!


Nicholas Paldino said:
Rodrigo,

You can use the Convert class to convert the elements in the
expression (numbers) to various numeric types, but that means parsing it
apart on your own.

Taking that string, you could create the code that I laid out for you.
The problem with that is that it is susceptable to injection attacks (if
you create a code segment and compile and run it).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Rodrigo Ferreira said:
Ok!

But in textbox the type is string! How can i pass this values to decimal
in the same order?


in message
Rodrigo,

My suggestion would be to get a third party math package. Either
that, or you could parse it apart yourself, but you would have to
handle all the precidence rules yourself (not hard, but not something
you should have to do).

What you could do is create a static method dynamically, something
like this:

public static double Calc()
{
return 123 + 456.456 + 456 / 453 * 45 + 4.155;
}

And then invoke it. It is easy to create a dynamic type to do this.
However, this could cause a good amount of bloat (considering how many
times you have to do this).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


I'm developing a user control with a textbox inside. When i write
something like this:

123 + 456.456 + 456 / 453 * 45 + 4.155

and press ENTER, the control must have to show the result.

I don't know how to start!

Anyone can help me?
 
Hi,

he meants that .NET has features that let you create a type on the fly,
like compiling a piece of code on demand. This may relieve you of parsing
the expression.

Also he mention a possible problem that the user instead of a valid numeric
expression enter a valid C# set of instructions that may have side
consequences like delete a file or so.

I suggest you to do a search as this is a regular homework for computer
sciences courses ( I do remember I had to make one ).

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Rodrigo Ferreira said:
Sorry but,

I don't understand! if you can write a few code it's better to me!


Nicholas Paldino said:
Rodrigo,

You can use the Convert class to convert the elements in the
expression (numbers) to various numeric types, but that means parsing it
apart on your own.

Taking that string, you could create the code that I laid out for you.
The problem with that is that it is susceptable to injection attacks (if
you create a code segment and compile and run it).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Rodrigo Ferreira said:
Ok!

But in textbox the type is string! How can i pass this values to decimal
in the same order?


in message
Rodrigo,

My suggestion would be to get a third party math package. Either
that, or you could parse it apart yourself, but you would have to
handle all the precidence rules yourself (not hard, but not something
you should have to do).

What you could do is create a static method dynamically, something
like this:

public static double Calc()
{
return 123 + 456.456 + 456 / 453 * 45 + 4.155;
}

And then invoke it. It is easy to create a dynamic type to do this.
However, this could cause a good amount of bloat (considering how many
times you have to do this).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


I'm developing a user control with a textbox inside. When i write
something like this:

123 + 456.456 + 456 / 453 * 45 + 4.155

and press ENTER, the control must have to show the result.

I don't know how to start!

Anyone can help me?
 
Back
Top