Tommy B.
Tommy B.

Reputation: 3659

Overriding System methods

I'd like to modify a system method to add a condition into it.

The method is

System.Boolean.Parse(String value)

I'd like it to support other values such as "1.000" and "0.000".

Because for some reason a DataGridViewCheckBoxCell is throwing an exception about "0.000 is not a valid value for Boolean". And this is called from within the DataGridView's internal code... So I can't modify the call :/

I did not put those values nowhere as my TrueValue is "true" and my FalseValue is "false".

So that's why I want to override this method!

Any good way of doing so?

Upvotes: 1

Views: 1159

Answers (2)

Gordon Gustafson
Gordon Gustafson

Reputation: 41229

You want to modify a method of the Boolean class so you can pass strings to it? If you want to parse strings, there are plenty of other ways to do it. For example, you can handle 1.000 like so:

using System.Float;

string foo = "1.000";
float result = Float.parse( foo ); 
value = ( result == 1.0f );

It would probably best if you wrote your own extension method. If you are using "1.000" as a value to represent true and that's the only thing you need it for, you should use a simple method like return ( arg == "1.000" ), or just use a Boolean in the first place. If you have multiple values that the string could handle, you'll have different logic. If you post some code and requirements we can probably help you more. :D

Upvotes: 0

Oded
Oded

Reputation: 499132

You can't override this method as it is declared static.

Write a wrapper around it (or an extension method on Boolean) that will implement your checks and if they pass delegate to it.

Upvotes: 3

Related Questions