473,395 Members | 2,689 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,395 software developers and data experts.

Math.Abs() Method Compiling Error

maheshwag
here is my code:

Expand|Select|Wrap|Line Numbers
  1.   private void button1_Click(object sender, EventArgs e)
  2.         {
  3.  
  4.             decimal? t1 = null;
  5.             decimal? t2 = null;
  6.             decimal? t3 = null;
  7.             decimal res = 0;
  8.             t1 = Convert.ToDecimal(textBox1.Text);
  9.             t2 = Convert.ToDecimal(textBox2.Text);
  10.             t3 = Convert.ToDecimal(textBox3.Text);
  11.             res = Math.Abs(t1 + t2 - t3);
  12.             textBox4.Text = res.ToString();
  13.  
  14.         }
  15.  
  16.  
Which is throw compilling error like:
1. "The best overloaded method match for System.Math.Abs(sbyte)' has some invalid arguments"

2. "Argument'1' cannot convert from decimal to sbyte"

Actually i am trying to remove negative sign from the result of calculation. e.g.

textbox1.text value = 10
textbox2.text value = 10
textbox3.text value = 40

Then Result is -20, I want to remove '-' sign from the result.

How to solve it?.
Oct 14 '10 #1

✓ answered by Christian Binder

You should use try-catch, so invalid strings don't cause the application to exit/throw error to the user.

What do you want to do if you gat wrong string, e.g. due to empty textbox?
Eighter you set the value to zero (or anything else) or you stop computation. You have to decide that.

This were the two ways:
(the variables declaration/init-part is omitted)
Expand|Select|Wrap|Line Numbers
  1. // ...
  2.   try { t1 = Convert.ToDecimal(textBox1.Text); } catch {}
  3.   try { t2 = Convert.ToDecimal(textBox2.Text); } catch {}
  4.   try { t3 = Convert.ToDecimal(textBox3.Text); } catch {}
  5.  
  6. // First way, take zero instead of wrong input
  7.   res = Math.Abs(t1 ?? 0 + t2 ?? 0 - t3 ?? 0)
  8.   textBox4.Text = res.ToString();
  9.  
  10. // Second way, say "error" if any input was wrong
  11.   if(t1.HasValue && t2.HasValue && t3.HasValue) {
  12.     res = Math.Abs(t1.Value + t2.Value - t3.Value)
  13.     textBox4.Text = res.ToString();
  14.   } else 
  15.     textBox4.Text = "Wrong input!";
  16.  

4 8372
Christian Binder
218 Expert 100+
Because t1, t2 and t3 are of type Nullable<decimal>, their addition also returns an Nullable<decimal> and there's no overload for Math.Abs which takes a Nullable<decimal>. You have to convert it back to decimal.

Expand|Select|Wrap|Line Numbers
  1. res = Math.Abs((decimal)(t1 + t2 - t3));
  2. //OR
  3. res = Math.Abs((t1 + t2 - t3).Value);
  4.  
Oct 14 '10 #2
You are very right but there is problem. when data on textbox null then it's fire error like " Input string was in not correct format"

how to overcome from this nullable type problem please suggest.
Oct 14 '10 #3
Christian Binder
218 Expert 100+
You should use try-catch, so invalid strings don't cause the application to exit/throw error to the user.

What do you want to do if you gat wrong string, e.g. due to empty textbox?
Eighter you set the value to zero (or anything else) or you stop computation. You have to decide that.

This were the two ways:
(the variables declaration/init-part is omitted)
Expand|Select|Wrap|Line Numbers
  1. // ...
  2.   try { t1 = Convert.ToDecimal(textBox1.Text); } catch {}
  3.   try { t2 = Convert.ToDecimal(textBox2.Text); } catch {}
  4.   try { t3 = Convert.ToDecimal(textBox3.Text); } catch {}
  5.  
  6. // First way, take zero instead of wrong input
  7.   res = Math.Abs(t1 ?? 0 + t2 ?? 0 - t3 ?? 0)
  8.   textBox4.Text = res.ToString();
  9.  
  10. // Second way, say "error" if any input was wrong
  11.   if(t1.HasValue && t2.HasValue && t3.HasValue) {
  12.     res = Math.Abs(t1.Value + t2.Value - t3.Value)
  13.     textBox4.Text = res.ToString();
  14.   } else 
  15.     textBox4.Text = "Wrong input!";
  16.  
Oct 14 '10 #4
Curtis Rutland
3,256 Expert 2GB
As an alternative to using Convert.To methods in a try statement, you can also use the TryParse method. The page I linked has examples for it's usage with decimals.
Oct 14 '10 #5

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

Similar topics

6
by: bmwrob | last post by:
I’m very new in C++ programming and am trying to make a decoder tool just for the purpose of learning. I started my project in VC++ 6.0, but after a change of PC, I continued my programming in VS...
0
by: mjm | last post by:
Hi, My script errors when compiling with: SCRIPT: Compiling Error => Line 59 : Expected statement (-2146827264) 'Variables available on this screen can be declared and initialized here. Dim...
2
by: stevenruiz | last post by:
Hi Everyone, The Strings.h has the function Get_Line which is defined and the error is shown below: Strings.h: void get_line( istream & );
5
by: Yansky | last post by:
Anyone any idea why the Math.abs() isn't working in this code? I thought it was supposed to convert negative numbers to whole numbers? var se = ; var time = 100 if(se < 0){ Math.abs(se);...
1
by: seforo | last post by:
Hi guys, I have a class declared and defined in AliHLTMessageQueue.h and AliHLTMessageQueue.cxx respectively template<typename MessageType, int kSignalNumber = SIGUSR1> class...
2
by: beet | last post by:
Hi, I am really not good at c/c++. Some simple code.. #include <stdio.h> #include <stdlib.h> #include <math.h> #include "simlibdefs.h"
0
by: beet | last post by:
On Apr 18, 3:03 pm, beet <whh...@gmail.comwrote: BTW...this C code is written by other people.
2
by: bips2008 | last post by:
The code seems to work fine in other browser but in IE it throws this error. This is very urgent for me and any help would be greatly appreciated For your convienence i have posted the code for the...
2
by: abrown07 | last post by:
When i try to compile: #include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h>
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.