I'm not quite sure if this should be in the asp.net, or possibly the c# forums...
I'm writing an asp.net calculator application in C#, and at this point it would be very useful if I could somehow save an operator in a variable, and then be able to use it later on, like this:
Let's say I have the integer session variables a=2 and b=4 -
-
protected void button_add(object sender, EventArgs e)
-
{
-
operatorvar opx=+;}
-
protected void button_equals(object sender, EventArgs e)
-
{
-
int answer=a opx b;}
-
I also want the operator to be in a session variable.
Is this at all possible? I did some googling, but ended up coming back here.
3 3992
What I'd do in that situation is write a method like this: - //i'm just doing this off the top of my head, haven't tested this code
-
private double PerformOperation(double a, char op, double b)
-
{
-
double retVal = 0;
-
switch(op)
-
{
-
case '+':
-
retVal = a + b;
-
break;
-
case '-':
-
retVal = a - b;
-
break;
-
.
-
.
-
.
-
default:
-
throw new Exception("Invalid Operator");
-
break;
-
}
-
return retVal;
-
}
And just pass your operator to it as a character.
Oh I see, that's really clever! I've never learned about that type of functions, but I understant how it works. Thanks a lot for the fast response.
No prob. A few notes on the switch statements:
In most languages, if you don't "break" out after a case, the code will fall through and execute the next line. In C#, unless your case is empty, you will get an exception. So always remember to break; your cases. Good - case 1:
-
doSomething();
-
doSomethingElse();
-
break;
-
case 2:
-
something();
-
break;
Good (in this case, the code under 2 will be executed for either 1 or 2) - case 1:
-
case 2:
-
something();
-
break;
Bad - case 1:
-
doSomething();
-
//the missing break; will throw an exception
-
case 2:
-
something();
-
break;
Also, the "default" case will be used if none of the other cases are matched.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: user |
last post by:
Hi,
Is it possible to override assignment, the
way that '+' can be overridden for example?
Thanks,
Toby
|
by: cppaddict |
last post by:
Hi,
Is it considered bad form to have the subscript operator return a
const reference variable? If not, what is the proper way to do it?
My question was prompted by the code below, my...
|
by: Jonan |
last post by:
Hello,
For several reasons I want to replace the built-in memory management with
some custom built. The mem management itlsef is not subject to my question -
it's ok to the point that I have...
|
by: Joost Ronkes Agerbeek |
last post by:
Should I remove const from a private member just for the sake of the
assignment operator?
I have a class that looks something like this.
class Foo
{
public:
Foo(const std::string&...
|
by: George |
last post by:
This program need to draw the some triangles into a 512 × 512 buffer
(in memory). Or save it to a file.
#include "project3.h"
Image::Image(int xres, int yres): xres(xres), yres(yres)
{...
|
by: linyanhung |
last post by:
A int in memory takes 32bits (4 bytes).
But if I use ofstream to save a int like this:
int i=1234567890;
ofstream ofs("c:\\intdata.bin", ios::binary);
ofs<<i;
It becomes 10 bytes in a...
|
by: raylopez99 |
last post by:
I need an example of a managed overloaded assignment operator for a
reference class, so I can equate two classes A1 and A2, say called
ARefClass, in this manner: A1=A2;. For some strange reason...
|
by: itdevries |
last post by:
Hi,
I've ran into some trouble with an overloaded + operator, maybe
someone can give me some hints what to look out for.
I've got my own custom vector class, as a part of that I've overloaded...
|
by: C++Liliput |
last post by:
I have a custom String class that contains an embedded char* member.
The copy constructor, assignment operator etc. are all correctly
defined. I need to create a map of my string (say a class...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |