Results 1 to 20 of 20

Thread: Easy one: Typcasting String -> Integer

  1. #1

    Thread Starter
    Hyperactive Member Bloged's Avatar
    Join Date
    May 2001
    Location
    Rotterdam, The Netherlands
    Posts
    330

    Easy one: Typcasting String -> Integer

    I tried searching, nothing found

    I have an file with something like this in it:
    Code:
    01 12 11 13 09 12 11 09 10 11
    12 09 08 03 04 05 06 07 10 09
    01 12 11 13 09 12 11 09 10 11
    12 09 08 03 04 05 06 07 10 09
    00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00
    I want to read this file (wich I can get to work) using StreamReader... Spilt every line on " " wich works but then I get an array of 10 Strings and I want it to be 10 Integers... Anyone who can post the source for the last bit?

    Thanks in advance

    Grtz,

    Bloged

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    You mean convert the string array to integer array ?

  3. #3
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    You mean CInt()?

  4. #4

    Thread Starter
    Hyperactive Member Bloged's Avatar
    Join Date
    May 2001
    Location
    Rotterdam, The Netherlands
    Posts
    330
    You mean convert the string array to integer array ?
    That's exactly what I mean...

    You mean CInt()?
    You tell me But I will look it up in the MSDN

    Thnx for the help so far!

    Edit:
    Ok looked up CIint() this will create an variant don't think my function expecting a Integer excepts this one! That's why it has to be converted!


    Grtz,

    Bloged
    Last edited by Bloged; Mar 26th, 2004 at 12:23 PM.

  5. #5
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Try something like this :
    VB Code:
    1. Dim s(10) As String
    2. Dim i() As Integer = DirectCast(s, Integer())

  6. #6
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    I'd go with Pirate, but CInt doesn't return a variant, it returns an Integer.
    From VS Help under Type Conversion Functions:
    CInt Integer -2,147,483,648 through 2,147,483,647; fractions are rounded.
    Integer is the return type.

  7. #7
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    what about the val() function? would that be appropriate in this situation?

  8. #8
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by salvelinus
    I'd go with Pirate, but CInt doesn't return a variant, it returns an Integer.
    From VS Help under Type Conversion Functions:

    Integer is the return type.
    Other options , use the static function for different kinds of conversion:

    Convert.To..
    VB Code:
    1. Convert.To..



    Originally posted by thephantom
    what about the val() function? would that be appropriate in this situation?
    Isn't that came from old VB6 function ? btw , it doesn't apply in this case .

  9. #9

    Thread Starter
    Hyperactive Member Bloged's Avatar
    Join Date
    May 2001
    Location
    Rotterdam, The Netherlands
    Posts
    330
    On this code:
    VB Code:
    1. iLineArray = DirectCast(sLineArray, Integer())
    I get:
    An unhandled exception of type 'System.InvalidCastException' occurred in Stratego.exe

    Additional information: Specified cast is not valid.


    Code in wich function is called
    VB Code:
    1. While sLine <> Nothing
    2.     sLineArray = Split(sLine, " ")
    3.     iLineArray = DirectCast(sLineArray, Integer())
    4.     sLine = sreStream.ReadLine()
    5. End While

    Grtz,

    Bloged

  10. #10
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    Isn't that came from old VB6 function ? btw , it doesn't apply in this case
    what's the .net equivalent?

  11. #11
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by Bloged
    On this code:
    VB Code:
    1. iLineArray = DirectCast(sLineArray, Integer())
    I get:
    An unhandled exception of type 'System.InvalidCastException' occurred in Stratego.exe

    Additional information: Specified cast is not valid.


    Code in wich function is called
    VB Code:
    1. While sLine <> Nothing
    2.     sLineArray = Split(sLine, " ")
    3.     iLineArray = DirectCast(sLineArray, Integer())
    4.     sLine = sreStream.ReadLine()
    5. End While

    Grtz,

    Bloged
    I didn't mean you put that inside while loop . Can you show me declaration of these variables :
    sLineArray , iLineArray ,sLine

  12. #12
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by thephantom
    what's the .net equivalent?
    Umm , either IndexOf or Contains function of string obj . Ofcourse it doesn't work the same way as Val function as it needs more tweaking .

  13. #13

    Thread Starter
    Hyperactive Member Bloged's Avatar
    Join Date
    May 2001
    Location
    Rotterdam, The Netherlands
    Posts
    330
    Ok incl. the declarations:
    VB Code:
    1. Dim sreStream As StreamReader
    2.     Dim sLine As String
    3.     Dim sLineArray() As String
    4.     Dim iLineArray() As Integer
    5.  
    6.     sreStream = File.OpenText("c:\input.txt")
    7.     sLine = sreStream.ReadLine()
    8.     While sLine <> Nothing
    9.         sLineArray = Split(sLine, " ")
    10.         iLineArray = DirectCast(sLineArray, Integer())
    11.         sLine = sreStream.ReadLine()
    12.         For iX = 0 To 9
    13.             pioPion(iLoop).SetCoordinates(iX, iY)
    14.             pioPion(iLoop).SetRang(iLineArray(iLoop))
    15.         Next
    16.         iY += 1
    17.     End While

    Thanks for all the help guys!

    Grtz,

    Bloged

  14. #14
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    You should try this line at the end of the While loop .
    VB Code:
    1. Dim i() As Integer = DirectCast(s, Integer())

  15. #15

    Thread Starter
    Hyperactive Member Bloged's Avatar
    Join Date
    May 2001
    Location
    Rotterdam, The Netherlands
    Posts
    330
    One question: Will this:
    VB Code:
    1. sLineArray = Split(sLine, " ")
    Add the current split line to the array or just overwrite it?!

    Grtz,

    Bloged

  16. #16
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Since arrays are referenced type , so it will replace it with what the string array returned by Split function.

  17. #17

    Thread Starter
    Hyperactive Member Bloged's Avatar
    Join Date
    May 2001
    Location
    Rotterdam, The Netherlands
    Posts
    330
    But then my question will be how to add them to one long String Array so I can cast that to a Integer Array?!

    Thanks for the help!

    Grtz,

    Bloged

  18. #18
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    VB Code:
    1. Private Sub test()
    2.         Dim sreStream As StreamReader
    3.         Dim sLine As String
    4.         Dim sLineArray() As String
    5.  
    6.         sreStream = File.OpenText("c:\test.txt")
    7.         sLine = sreStream.ReadLine()      
    8.         While sLine <> Nothing            
    9.             sLineArray = Split(sLine, " ")
    10.             sLine = sreStream.ReadLine()
    11.         End While
    12.         For i As Integer = 0 To sLineArray.Length - 1
    13.             MsgBox(sLineArray(i))
    14.         Next
    15.  
    16.  
    17.         Dim ilineArray(sLineArray.Length - 1) As Integer
    18.  
    19.         'Convert from string to interger
    20.         For i As Integer = 0 To sLineArray.Length - 1
    21.             ilineArray(i) = sLineArray(i)
    22.         Next
    23.  
    24.     End Sub

  19. #19
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Here is another conversion you may want to use instead of CInt.

    Integer.Parse(mystring)

  20. #20

    Thread Starter
    Hyperactive Member Bloged's Avatar
    Join Date
    May 2001
    Location
    Rotterdam, The Netherlands
    Posts
    330
    VB Code:
    1. Private Sub test()
    2.         Dim sreStream As StreamReader
    3.         Dim sLine As String
    4.         Dim sLineArray() As String
    5.  
    6.         sreStream = File.OpenText("c:\test.txt")
    7.         sLine = sreStream.ReadLine()      
    8.         While sLine <> Nothing            
    9.             sLineArray = Split(sLine, " ")
    10.             sLine = sreStream.ReadLine()
    11.         End While
    12.         For i As Integer = 0 To sLineArray.Length - 1
    13.             MsgBox(sLineArray(i))
    14.         Next
    15.  
    16.  
    17.         Dim ilineArray(sLineArray.Length - 1) As Integer
    18.  
    19.         'Convert from string to interger
    20.         For i As Integer = 0 To sLineArray.Length - 1
    21.             ilineArray(i) = sLineArray(i)
    22.         Next
    23.  
    24.     End Sub
    Wouldn't this just convert the last line from my file to integers?! Because this:
    VB Code:
    1. sLineArray = Split(sLine, " ")
    Replaces the old Array with the new one created after Split()

    Integer.Parse(mystring)
    would probably give the same error as
    VB Code:
    1. iLineArray = DirectCast(sLineArray, Integer())

    I will try it out when I get home from school!

    Thanks for all the help

    Grtz,

    Bloged

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