tags:

views:

159

answers:

6

The following nullable decimal code fired Overload Method Error:

decimal? t1 = null;
decimal? t2 = null;
decimal? t3 = null;
decimal res = 0;
decimal tt1 = 0;
decimal tt2 = 0;
decimal tt3 = 0;

if (decimal.TryParse(textBox1.Text, out tt1))
    t1 = tt1;
if (decimal.TryParse(textBox2.Text, out tt2))
    t2 = tt2;
if (decimal.TryParse(textBox3.Text, out tt3))
    t3 = tt3;
res = Math.Abs(t1 + t2 - t3);
textBox4.Text = res.ToString();

The above code suggested calculation between three textBoxes and fourth textBox showing result of them. But the problem is Math.Abs method is not supported for nullable type decimal. How to overcome?. How to overcome means i wants to allow nullable in Math.Abs method by another way.

+2  A: 

You should perform null checks, but the code would be:

res = Math.Abs((decimal)t1 + (decimal)t2 - (decimal)t3);

Or:

res = Math.Abs(t1.Value + t2.Value - t3.Value);
Kirk Woll
@Kirk Your code solved the error but not nullable.
mahesh
+1  A: 

My guess is that you actually want Math.Abs(tt1 + tt2 - tt3) anyway.

If you actually want null to be returned if any of the inputs are null, then maybe you want something like this:

    decimal? res = t1 + t2 - t3;
    if (res != null)
        res = Math.Abs(res.Value);
    textBox4.Text = res.ToString();
Gabe
@Gabe, Yes but it's not solve nullable type problem.
mahesh
mahesh: `Math.Abs` doesn't have a defined answer for things that aren't numbers, like `null` so you're asking us to guess what result you want when your inputs might be null. How are we suppoesd to know what you expect? Is `Abs(null)` supposed to be 0? Is it supposed to be null?
Gabe
@Gabe, You are 100% right Math.Abs() Method will not accept any nullable directly. It will accepting indirectly e.g. Your above code. See in your code there is mistake.
mahesh
"Res=Math.Abs(Res)" You are trying to assign nullable, "t1+t2-t3" are nullable and Math.Abs() will not accept any nullable. have a look for value of t1,t2,t3 in my question
mahesh
Now edit your code: "res=Math.Abs(res)" To "res=Math.Abs((decimal)res)". It will solve the issue. and than after I will going to accept your answer which will help for community also. especially for our new commer. thanks
mahesh
This version looks good to me
Modan
@Gabe, Your Idea is click hence i give you first preference and hereby accept your answer. thanks for helping me out.
mahesh
A: 

How about something like this?

     decimal? a = 4;
     decimal? b = 3.254m;
     decimal? c = 9.765675m;

     decimal? d = (a + b - c);

     decimal? res = null;
     if (d != null)
     {
        res = Math.Abs((decimal)d);
     }
     textBox4.Text = (res != null) ? res.ToString() : "null";
SwDevMan81
SwDevMan Edit your code : decimal res=0; To decimal? res=null; because your old code unnecessary return =0 and i suggested would solved it. this code would helpful to the community which i am going to accept it so please do it first.
mahesh
@mahesh - Will that work for you?
SwDevMan81
@SwDevMan Look at my code which is solved the error as well as nullable typr problem from Math.Abs() Method. Your First Code I have modified In my code it was working very well in which I have advice to change like it.
mahesh
But Your this code show default value even Though It is null. I explain You how when The textbox4 value is null and you click on button it will show default value which is I not want.
mahesh
@SwDevMan Look at my answer. It's edited against my first comment.
mahesh
+1  A: 

Having reread your example more closely, I assume it was intended to be a contrived example, since the only reason you would have set t1, t2 or t3 was to give the opportunity of demonstrating the problem. I have therefore reworked my example to something that is hopefully more in keeping with your intent.

    private void button1_Click(object sender, EventArgs e)
    {
        //Read some values in a contrived example to get a mixture of null 
        //and not null values into t1, t2 & t3
        decimal? t1 = null;
        decimal? t2 = null;
        decimal? t3 = null;
        decimal res = 0;
        decimal tt1 = 0;
        decimal tt2 = 0;
        decimal tt3 = 0;


        if (decimal.TryParse(textBox1.Text, out tt1))
            t1 = tt1;
        if (decimal.TryParse(textBox2.Text, out tt2))
            t2 = tt2;
        if (decimal.TryParse(textBox3.Text, out tt3))
            t3 = tt3;

        //We have setup our inputs now, so lets get down to how to handle the problem                    
        //now.  This should probably be in a separate function, but we are in a _Click 
        //method, so I am assuming we are overlooking such things in this example...

        //return without setting textBox4 if any of t1, t2 & t3 are null
        if (!t1.HasValue || !t2.HasValue || !t2.HasValue)
        {
            return;
        }           
        //1, 2 & 3 are all valid, so set textBox4
        res = Math.Abs(t1.Value + t2.Value - t3.Value);
        textBox4.Text = res.ToString();
   }

The main points here are we should be explicit that textBox4 is not set when any of the 3 inputs are null, rather than inferring this from the return from Math.Abs(), and also to use the Value property on the nullable type rather than casting to the value type, which I just prefer stylistically..

Modan
@Modan, I am sorry but i didn't get you "tt1.Value over(decimal)tt21" please explain your way in details.
mahesh
@Modan, There is nothing in my question like "tt21"
mahesh
@Moden, You are did it. but sorry am not accept it as because there is compilling error "Invalid expression ')' look at your code at "if(!t1.HasValue|| !t2.HasValue|| !t2.HasValue))" So remove it ")" from your code.
mahesh
@Moden, I am welcome You with Your new Idea and it works too.
mahesh
@mahesh, Oops, sorry. Fixed now.
Modan
@Modan, There is nothing about sorry, It's a part of coding we are all here about sharing knowledge.and it's a flow of knowledge. So we must enjoy it.
mahesh
A: 

What result do you expect if you pass null into Math.Abs? But you might want to use the null coalescence operator ??.

res = Math.Abs(()??0);

where the value after ?? is the alternate value used if the expression before it is null. So you want to get null if the argument to Abs is null?

Decimal? temp=t1 + t2 - t3;
if(temp!=null)
    temp=Math.Abs(temp.Value);
textBox4.Text = temp.ToString();
CodeInChaos
@Codel, I don't want any result like "0", even any error!. when my three textbox are empty with test of Math.Abs() Method. You can see my comment on "Gabe" and "SWDev" you can realize.
mahesh
And it's solved with my suggestion. I request them to edit it. as I can not accept bad answer. It will misguide some one to choose bad answer.
mahesh
A: 

The Exact Solution For Above Problem is :-

private void button1_Click(object sender, EventArgs e)
    {
        decimal? t1 = null;
        decimal? t2 = null;
        decimal? t3 = null;
        decimal tt1 = 0;
        decimal tt2 = 0;
        decimal tt3 = 0;

        if (decimal.TryParse(textBox1.Text, out tt1))
            t1 = tt1;
        if (decimal.TryParse(textBox2.Text, out tt2))
            t2 = tt2;
        if (decimal.TryParse(textBox3.Text, out tt3))
            t3 = tt3;

        decimal? res = t1 + t2 - t3;
        if (res != null) 
        {
            res = Math.Abs((decimal) res);
        }

        textBox4.Text = res.ToString();
      }

See This code doesn't throw any error and clear the nullable problem on use of Math.Abs() Method.

mahesh