Here is a classic example of why C# is like beating your head against
the wall. I can tell you where the error is in the below code--it's
the "NumberStyl es" property of DecimalParse (which extracts a decimal
from a string)--and how to fix it--you leave it out--but why? WHY?
Y?
We'll never know... that's just the way it is. Some things were meant
to be...
(PS--my debugger is horrible--it doesn't usually work--but if I were
to 'guess' the problem it would be the NumberStyles flags comprise
more than one flag and they are "OR'd" together. As you logic freaks
know, when you "OR" more than two logical flags, you get potential
problems. BUT, I have to point out I got this from MSDN, which had
this example (four flags) AND, no pun intended, you would think they
would have the flags done right so that you can use more than two
flags at the same time. UPDATE: I just tried it with one flag, two
flags and it still fails for the NumberStyles version)
Below is the complete code, cut and paste and it should work to demo
the problem.
See the comments for where the 'error' is. I also am using a
NumberStyles I got from MSDN Help--so it should have worked--but did
not.
Here is the MSDN link for TryParse (similar to Decimal Parse):
http://msdn.microsoft.com/en-us/library/zf50za27.aspx
And here for NumberStyles: http://msdn.microsoft.com/en-us/libr...berstyles.aspx
RL
using System;
using System.Collecti ons.Generic;
using System.Diagnost ics;
using System.Globaliz ation;
namespace Console1
{
class Program
{
static void Main(string[] args)
{
NumberStyles styles = NumberStyles.Al lowParentheses |
NumberStyles.Al lowLeadingSign | NumberStyles.Al lowLeadingWhite |
NumberStyles.Al lowTrailingWhit e;
//PLAIN GENERIC NUMBERSTYLES! WHY DOES C# HAVE TO BE SO DIFFICULT,
WHY, why, Y?
string s1 = "1.23";
decimal d1 = DecimalParse(st yles, s1);
Console.WriteLi ne("s1, d1 are: {0}, {1}", s1, d1); //try
fails, catch thrown. you get d1=0.99 in decimal returned, indicating
the catch block was executed
//now leave out NumberStyles--and see what happens (it works)
decimal d2 = DecimalParse2(s 1);
Console.WriteLi ne("s1, d2: {0}, {1}", s1, d2); //you get
d2=1.23 in decimal returned, correct.
// output:
/*
* parse failed: Input string was not in a correct
format.s1, d1 are: 1.23, 0.99
* s1, d2: 1.23, 1.23
* Press any key to continue . . .
*
* */
// just for shi it s and grins, see if a non-static version makes a
difference (it does not, as I expected)
Program myProgram = new Program();
decimal aDecimal;
aDecimal = myProgram.nonSt atic_DecimalPar se(styles, s1);
Console.WriteLi ne("s1, aDecimal for non-static
DecimalParse: {0}, {1}", s1, aDecimal);
// still fails, like first static example above
//now should succeed, if you leave out NumberStyles
aDecimal = myProgram.nonSt atic_DecimalPar se2(s1);
Console.WriteLi ne("s1, aDecimal for non-static
DecimalParse2: {0}, {1}", s1, aDecimal);
//yes, succeeds to give 1.23, 1.23 as wanted
}
static decimal DecimalParse(Nu mberStyles styles, string
decimalString)
{
try
{
decimal decimalNumber;
decimalNumber = Decimal.Parse(d ecimalString, styles);
Debug.Write("de cimalNumber is: " +
decimalNumber.T oString());
return decimalNumber;
}
catch (Exception ex)
{
// Display the exception message if Parse failed
Console.Write(" parse failed: " + ex.Message);
return 0.99M;
}
} //
// leaving out NumberStyles version of method here:
static decimal DecimalParse2(s tring decimalString)
{
try
{
decimal decimalNumber;
decimalNumber = Decimal.Parse(d ecimalString);
return decimalNumber;
}
catch (Exception ex)
{
// Display the exception message if Parse failed
Console.Write(" parse failed: " + ex.Message);
return 0.69M;
}
} //
decimal nonStatic_Decim alParse(NumberS tyles styles, string
decimalString)
{
try
{
decimal decimalNumber;
decimalNumber = Decimal.Parse(d ecimalString, styles);
Debug.Write("de cimalNumber is: " +
decimalNumber.T oString());
return decimalNumber;
}
catch (Exception ex)
{
// Display the exception message if Parse failed
Console.Write(" parse failed: " + ex.Message);
return 0.99M;
}
} //
decimal nonStatic_Decim alParse2(string decimalString)
{
try
{
decimal decimalNumber;
decimalNumber = Decimal.Parse(d ecimalString);
return decimalNumber;
}
catch (Exception ex)
{
// Display the exception message if Parse failed
Console.Write(" parse failed: " + ex.Message);
return 0.69M;
}
} //
}
}