473,405 Members | 2,171 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Can I save an operator in a variable?

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
Expand|Select|Wrap|Line Numbers
  1.  
  2. protected void button_add(object sender, EventArgs e)
  3. {
  4. operatorvar opx=+;}
  5. protected void button_equals(object sender, EventArgs e)
  6. {
  7. int answer=a opx b;}
  8.  
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.
Feb 4 '09 #1
3 3992
Curtis Rutland
3,256 Expert 2GB
What I'd do in that situation is write a method like this:
Expand|Select|Wrap|Line Numbers
  1. //i'm just doing this off the top of my head, haven't tested this code
  2. private double PerformOperation(double a, char op, double b)
  3. {
  4.   double retVal = 0;
  5.   switch(op)
  6.   {
  7.     case '+':
  8.       retVal = a + b;
  9.       break;
  10.     case '-':
  11.       retVal = a - b;
  12.       break;
  13.       .
  14.       .
  15.       .
  16.     default:
  17.       throw new Exception("Invalid Operator");
  18.       break;
  19.   }
  20.   return retVal;
  21. }
And just pass your operator to it as a character.
Feb 4 '09 #2
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.
Feb 4 '09 #3
Curtis Rutland
3,256 Expert 2GB
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
Expand|Select|Wrap|Line Numbers
  1. case 1:
  2.   doSomething();
  3.   doSomethingElse();
  4.   break;
  5. case 2:
  6.   something();
  7.   break;
Good (in this case, the code under 2 will be executed for either 1 or 2)
Expand|Select|Wrap|Line Numbers
  1. case 1:
  2. case 2:
  3.   something();
  4.   break;
Bad
Expand|Select|Wrap|Line Numbers
  1. case 1:
  2.   doSomething();
  3.   //the missing break; will throw an exception
  4. case 2:
  5.   something();
  6.   break;
Also, the "default" case will be used if none of the other cases are matched.
Feb 4 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: user | last post by:
Hi, Is it possible to override assignment, the way that '+' can be overridden for example? Thanks, Toby
18
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...
11
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...
14
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&...
5
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) {...
9
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...
5
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...
9
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...
19
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...
0
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
0
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...
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
jinu1996
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...
0
tracyyun
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...
0
agi2029
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,...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.