Type casting string to integer 
Author Message
 Type casting string to integer

Hi,

   I am getting an error while type casting string to integer or float.

  Cannot convert type 'string' to 'int'
Cannot convert type 'string' to 'float'

  any solution for this problem

Regards,
R.Muralidharan



Sun, 16 Jan 2005 21:48:54 GMT  
 Type casting string to integer
Use:
int res = Convert.ToInt32("42");

--
Aymeric GAURAT
Consultant at 3IE (Institut d'Innovation Informatique pour l'Entreprise)
c/o EPITA (French Computer Engineering School)
http://www.3ie.org


Quote:
> Hi,

>    I am getting an error while type casting string to integer or float.

>   Cannot convert type 'string' to 'int'
> Cannot convert type 'string' to 'float'

>   any solution for this problem

> Regards,
> R.Muralidharan



Sun, 16 Jan 2005 21:57:45 GMT  
 Type casting string to integer
Try using the Convert class.

HTH



Sun, 16 Jan 2005 21:58:01 GMT  
 Type casting string to integer
Murali,

    You can not cast a string directly to an integer or float (I don't know
any language that allows a direct cast, without some manipulation below the
covers).

    You will want to use the static ToInt32 method on the Convert class, or
the static Parse method on the Int32 value type.  You don't have to use
Int32, you can use Int64, Int16, etc, etc.

    Hope this helps.

--
               - Nicholas Paldino [.NET MVP]


Quote:
> Hi,

>    I am getting an error while type casting string to integer or float.

>   Cannot convert type 'string' to 'int'
> Cannot convert type 'string' to 'float'

>   any solution for this problem

> Regards,
> R.Muralidharan



Sun, 16 Jan 2005 21:54:45 GMT  
 Type casting string to integer
Try this: Int32.Parse(<string>) or Double.Parse(<string>)


Hi,

   I am getting an error while type casting string to integer or float.

  Cannot convert type 'string' to 'int'
Cannot convert type 'string' to 'float'

  any solution for this problem

Regards,
R.Muralidharan



Sun, 16 Jan 2005 21:59:19 GMT  
 Type casting string to integer
Murali, you can't just cast strings to int or float.  You should use the
Convert class to change strings to numeric types.  (or for any base type to
other base type conversion)  Another option is Int16, Int32 et al have a
Parse method which will convert a string to the given type.

--
Greg
http://www.claritycon.com/


Quote:
> Hi,

>    I am getting an error while type casting string to integer or float.

>   Cannot convert type 'string' to 'int'
> Cannot convert type 'string' to 'float'

>   any solution for this problem

> Regards,
> R.Muralidharan



Sun, 16 Jan 2005 22:04:21 GMT  
 Type casting string to integer

Quote:

> Hi,

>    I am getting an error while type casting string to integer or float.

>   Cannot convert type 'string' to 'int'
> Cannot convert type 'string' to 'float'

>   any solution for this problem

You can call the Parse method of the int/float class

public class Test20020731 {
   public static void Main () {
     string power = "42";
     int p = int.Parse(power);
     p++;
     System.Console.WriteLine("string: {0}, int++: {1}", power, p);
     try {
       string message = "Kibology";
       int m = int.Parse(message);
     }
     catch (System.Exception e) {
       System.Console.WriteLine("Error: {0}", e);
     }
     string salary = "2000.50";
     float s = float.Parse(salary, new
System.Globalization.CultureInfo("en-US"));
     System.Console.WriteLine("s: {0}", s);
   }

Quote:
}

The CultureInfo is only necessary if your code runs on a system where
the culture/regional settings use a different symbol than the floating
point. I am running Windows XP German edition and there without passing
the en-US culture to the Parse method the "." is not parsed as intended.

Quote:

> Regards,
> R.Muralidharan

--

        Martin Honnen
        http://javascript.faqts.com/
        http://home.t-online.de/home/martin.honnen/jsgoddies.html



Sun, 16 Jan 2005 22:13:16 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. cast cstring type to integer type

2. Puzzle on type casting an integer

3. Casting a 32-bit unsigned integer to a 16-bit unsigned integer

4. Type Casting between Base and Derived Type (Serialization of objects with Type members)

5. About string type cast

6. About string type cast:

7. Checking an integer in a string against the max integer limit in limits.h

8. type casting to / from pointers to incomplete data types

9. type casting to / from pointers to incomplete data types

10. Casting to/from STL types to MFC types

11. Why can't overload type cast to base type (object)

12. q: Casting array of bytes into integer (ANSI C)

 

 
Powered by phpBB® Forum Software