none
Difference between these two casting

    Question

  • Hello,

    What is the difference between

    Code Block

    int a = (int)Othertype;


    and

    Code Block

    int a = int.Parse(Othertype);


    Thursday, October 18, 2007 10:59 AM

Answers

  • The first statement attempts to cast 'Othertype' into in an integer type, and assign the result to the variable a. If the conversion fails, and invalid cast exception is thrown. This explicit conversion works only with other numerical types, such as long, byte, sbyte, float, double, decimal etc. Mind that the cast might result in an overflow in a checked context, because other numerical types can have a much larger numeric range, such as the double type.
    The second, int.Parse(string) parses a string and returns an integer, or throws an exception if the string cannot be parsed into a number. Parsing allows you to convert text (such as user input) to binary numbers that you can use in mathematical expressions.
    Here are some examples:

    Code Block

    // Using explicit casts:
    double x = Math.PI;
    int num;
    num = x; // fails
    num = (int)x; // OK, informs the compiler what cast to use and that loss of precision is desireable

    //Using parsing:
    Console.WriteLine("Please enter a number: ");
    string usr = Console.ReadLine();
    int num;
    num = usr; // fails
    num = (int); // fails too

    // The correct way:

    try
    {
        num = int.Parse(usr);
    }
    catch (FormatException)
    {
        Console.WriteLine("Invalid input.");
    }



    The difference is quite paramount.
    Thursday, October 18, 2007 11:28 AM

All replies

  • Yes, their is a difference in between two piece of code.

     

    The difference is that the parse method is used to convert the string of number to convert into numeric value.

     

    For e.g. int a= int.Parse("234")

    yield 234 number to variable "a" but if you try to convert

    int a = int.Parse("Two Hundered Thirty Four") it yeilds a error because this method only convert string of numbers into numeric value

     

    int a= int.Parse(234), this also yields a error.

     

    but

     

    in another case

    int a= (int) OtherType

    is used to cast the value of value of "OtherType" to type of int where data type of "OtherType" is also of numeric type.

    Thursday, October 18, 2007 11:24 AM
  • The first statement attempts to cast 'Othertype' into in an integer type, and assign the result to the variable a. If the conversion fails, and invalid cast exception is thrown. This explicit conversion works only with other numerical types, such as long, byte, sbyte, float, double, decimal etc. Mind that the cast might result in an overflow in a checked context, because other numerical types can have a much larger numeric range, such as the double type.
    The second, int.Parse(string) parses a string and returns an integer, or throws an exception if the string cannot be parsed into a number. Parsing allows you to convert text (such as user input) to binary numbers that you can use in mathematical expressions.
    Here are some examples:

    Code Block

    // Using explicit casts:
    double x = Math.PI;
    int num;
    num = x; // fails
    num = (int)x; // OK, informs the compiler what cast to use and that loss of precision is desireable

    //Using parsing:
    Console.WriteLine("Please enter a number: ");
    string usr = Console.ReadLine();
    int num;
    num = usr; // fails
    num = (int); // fails too

    // The correct way:

    try
    {
        num = int.Parse(usr);
    }
    catch (FormatException)
    {
        Console.WriteLine("Invalid input.");
    }



    The difference is quite paramount.
    Thursday, October 18, 2007 11:28 AM
  • Boxing and Unboxing (C# Programming Guide)

    http://msdn2.microsoft.com/en-us/library/yz2be5wk(VS.80).aspx

     

    I use instead of try catch-block something like that:

     

    decimal result;

    if (Decimal.TryParse(txt.Text, out result))

    workHour[txt.ID] = Convert.ToDecimal(txt.Text);

     

    Wednesday, October 24, 2007 2:11 PM
  • TryParse is a nice feature. I think it was a .NET 2.0 addition so if you're in 1.0 you may not be able to use it.
    Wednesday, October 24, 2007 3:58 PM
  •  

    WHY do the double Parse... It is just a waste of time!!!!!

     

    Code Block

    decimal result;

    if (Decimal.TryParse(txt.Text, out result))

    workHour[txt.ID] = result;

     

     

    Thursday, November 08, 2007 9:09 PM