Results 1 to 8 of 8

Thread: convert string to int

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    convert string to int

    How is this possible? Anyone has any idea? the type "string" is different than the char* string in C++.


    For example, if I have the string "12", and I want it as an int 12

    best regards
    Henrik

  2. #2
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Because .NET enforces strong typing, converting a string to an int is not a valid conversion operation. I'm not exactly sure there is no way around this, so you many want to play with the System.Convert class. If I was at my computer, I would try for myself..

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    The thing I am trying to do is to read a line of text from a file...

    there I use string.substring() to fetch the parameter I need.
    Then I need to upload the info to an SQL db into an int field.
    The variable is really a week number, so I really don't want to change the db field. It feels very wrong. I won't do any arithmetics, just basic comparisons with boolean operators.



    Anyone with suggestion, please shout!!!!


    btw
    using System.Convert;

    won't work... unknown class...


    best regards
    Henrik

  4. #4
    Addicted Member MasterBlaster's Avatar
    Join Date
    Jul 2002
    Location
    Seattle
    Posts
    196
    here go

    Code:
    System.Convert.ToInt16(MyString)
    Last edited by MasterBlaster; Nov 4th, 2002 at 01:29 PM.

  5. #5
    Addicted Member MasterBlaster's Avatar
    Join Date
    Jul 2002
    Location
    Seattle
    Posts
    196
    Originally posted by MrNorth

    btw
    using System.Convert;

    won't work... unknown class...


    best regards
    Henrik
    Then you prolly have other problems post your code

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    The code is a bit large to post on the forum... but I declare my namespaces like standard procedure:

    using System.IO;
    using System.String;

    etc
    etc

    It seems like intellisense won't recognize the Convert class as member of System??? Anything I have to add???

    Anything I can try?


    thansk for the answer btw...

    best regards
    Henrik

  7. #7
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    You need this:

    Code:
    using System;
    //.... Rest of your code

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    Solved it

    I could just write Convert.whatevermethod();

    no namespace declaration was needed.... spoooky

    best regards
    Henrik

Posting Permissions

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



Click Here to Expand Forum to Full Width