math - How can one check for "safe" conversions between value types in .NET? -


Back to the basics ...

For reference types, someone can do this:

/ P>
  Some Objects Some Objects = First Object As SomeType;  

For price types, I understand that we have built-in conversions (no data loss), clear conversion (if there is a risk of data loss), Convert class (I think of a "conversion clipper") and also type-specific conversions (like double x = double.Parse ("2"); ), but I There is nothing similar to the operator as .

So, my question is: What framework does some methods / operators / techniques to do something with these methods:

  if (! CanConvert (someValue, someValueType)) // // Beware! Data loss may occur} and {// there is no data loss}  

If not, then what kind of a Canconvert method to build concrete Can suggest the approach?

Thanks a lot!

Edit (1): The user-case / problem is as follows: Looking at the some given by the code user (my second itself, but it is irrelevant), (1) Check that some is a number (easy enough) and (2) keep some in the "smallest" numerical type where it fits without getting into data loss is.

Some backgrounds: What I am trying to do is that the nature is more mathematical than the technical: I am trying to see how I can fit existing numerical types into something I'm a algebraic hierarchy of monoid => group => ring => field (or its simplified version). Working on this, and it is not certain that "one thing has happened" and I had to deal with the conversion type myself ...

How about the TryParse method at different price types?

  int x; If (int.TryParse (some type. ToString (), X out)) Return X;  

Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -