The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.


Results 1 to 9 of 9

Thread: converting in c#

  1. #1

    converting in c#

    i know that there is this wondering class Convert that will convert any object into another object, so long as they impliment IConvertable. My problem is if the data can't be converted, maybe the object doesn't impliment it, maybe you are trying to convert an int into a guid. Either way it throws and exception if it fails. I'm wondering if anyone of you know a function or easy class to use that can test if it works. I just need to know if it is possible to convert to the type. I dont need the value to be converted, just need to know if it can be converted.

  2. #2

  3. #3
    close, but what if i have:

    string a = 123

    and i need to see if value of a can be casted as int.

    using int will cause a fail, is only takes into account inheritance tree. It makes sure it isn't null and can be cast like (type)(value), but doesn't do the actual convention of a string of 123 to the int 123.

    what i need is a function that will look at a type like 123, and see if it can be an int.

  4. #4
    One easy way is to use try/catch statements to ensure that invalid conversions get caught:
    Code:
                string foo = "123a";
    
                try
                {
                    int newNum = int.Parse(foo);
                }
                catch (FormatException e)
                {
                    string error = e.Message;
                }
    You can even create a function that checks whether a conversion can be made and returns true/false depending on the result.

    Cheers!
    Kirupa

    Great, now even Kirupa is { facebooking | twittering }

  5. #5
    Quote Originally Posted by kirupa View Post
    One easy way is to use try/catch statements to ensure that invalid conversions get caught:
    Code:
                string foo = "123a";
    
                try
                {
                    int newNum = int.Parse(foo);
                }
                catch (FormatException e)
                {
                    string error = e.Message;
                }
    You can even create a function that checks whether a conversion can be made and returns true/false depending on the result.

    Cheers!
    Kirupa
    i'm doing that more or less, but i need it more generic than what you have... i have

    bool returnValue = true;
    try
    {

    Convert.ChangeType(value, type);
    }
    catch
    {
    returnValue = false;
    }
    return returnValue;

    but the problem is that there is a lot of overhead in a try catch, and it slows down performance. What i'm looking for is a faster way of doing it, most likely through some already created function.

    If i wanted to do it right, i could just write a table of sorts of what can be converted to what, like we know that int and string can be converted back and forth, but the problem is more about object1 and object2, how do we know if they can.

    what would be even better is the moment it failed to parse, it would just return false. I believe that like int has a TryParse, but i can't go through every type and do a TryParse on each database, that would take even longer. I would really like it to be like my code above, only with a TryParseToType that returns false if it can't.

  6. #6
    If I may ask, why do you need to check whether or not one type can be converted to another? Sorry, I'm not understanding what you're trying to accomplish, so it's sounding like something needs to change in your design at the moment...

    Why wouldn't you know exactly what types you're handling, and handle any conversions or casts only as they're needed?

  7. #7
    Quote Originally Posted by yell0wdart View Post
    If I may ask, why do you need to check whether or not one type can be converted to another? Sorry, I'm not understanding what you're trying to accomplish, so it's sounding like something needs to change in your design at the moment...

    Why wouldn't you know exactly what types you're handling, and handle any conversions or casts only as they're needed?
    A common example of checking whether or not input can be converted is when asking to input a number of the command line:
    Code:
    string inp = Console.ReadLine();
    int num = Convert.ToInt32(inp);
    This would work if the user enters: 4
    But if the user's finger slips and he enters: 4#
    This would cause an unhandled exception as # is not expected.

    Doing the following we can remove this problem:
    Code:
    int askForNumber()
    {
    int num;
    string inp = Console.ReadLine();
    try
    {
    num = Convert.ToInt32(inp);
    } 
    catch(Exception e)
    {
    // if an error occurs repeat this function
    num = AskForNumber();
    }
    return num;
    }
    Web Design and Web Development blog
    Designer Notice: Browser List for design compatibility

  8. #8
    1,806
    posts
    My Pimp Hand is Strong
    A few other options to consider:

    Code:
    string mynum = "18928484";
    
    bool IsInteger = true;
    
    int newint = 0;
    
    foreach(char n in mynum) {
    	if(!Char.IsDigit(n)) {
    		IsInteger = false;
    		break;
    	}
    }
    
    IsInteger = Int32.TryParse(mynum, out newint);
    (off topic, I know)
    Last edited by norie; March 19th, 2008 at 03:01 PM.

  9. #9
    Quote Originally Posted by yell0wdart View Post
    If I may ask, why do you need to check whether or not one type can be converted to another? Sorry, I'm not understanding what you're trying to accomplish, so it's sounding like something needs to change in your design at the moment...

    Why wouldn't you know exactly what types you're handling, and handle any conversions or casts only as they're needed?
    The situation is when someone is defining an object through a file or i guess in a command line parameter, and giving me the initialized value. exampes

    <myObject test="MyName">

    well i can create myobject, and using reflection, see it is a string, so MyName will be fine at assaign. But what if test is an int, now it will fail. What if test is type myNextObject, can MyName be converted?

    this is more or less dealing with xaml, which is a way to define an object using xml. That is why i need to know. I'm parsing the xaml file, and i only need to know if the value can be assign, i dont want to be constantly converting files. What if i have a class that has a collection of 100, and the xaml document defines it. I'll have to convert 100 objects, only to see if it can or cannot be converted. That is why i'm looking for quick way to handle it.

    this problem comes around when you start doing extremely highly dynamic systems.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012