开发者

Overriding System methods

开发者 https://www.devze.com 2023-02-15 19:32 出处:网络
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 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 excep开发者_如何转开发tion 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?


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消