|
-
Mar 26th, 2004, 05:07 AM
#1
Thread Starter
Hyperactive Member
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
-
Mar 26th, 2004, 11:20 AM
#2
Sleep mode
You mean convert the string array to integer array ?
-
Mar 26th, 2004, 11:42 AM
#3
Frenzied Member
-
Mar 26th, 2004, 12:18 PM
#4
Thread Starter
Hyperactive Member
You mean convert the string array to integer array ?
That's exactly what I mean...
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.
-
Mar 26th, 2004, 12:20 PM
#5
Sleep mode
Try something like this :
VB Code:
Dim s(10) As String
Dim i() As Integer = DirectCast(s, Integer())
-
Mar 26th, 2004, 12:31 PM
#6
Frenzied Member
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.
-
Mar 26th, 2004, 01:04 PM
#7
Frenzied Member
what about the val() function? would that be appropriate in this situation?
-
Mar 26th, 2004, 01:23 PM
#8
Sleep mode
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..
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 .
-
Mar 26th, 2004, 05:54 PM
#9
Thread Starter
Hyperactive Member
On this code:
VB Code:
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:
While sLine <> Nothing
sLineArray = Split(sLine, " ")
iLineArray = DirectCast(sLineArray, Integer())
sLine = sreStream.ReadLine()
End While
Grtz,
Bloged
-
Mar 26th, 2004, 08:48 PM
#10
Frenzied Member
Isn't that came from old VB6 function ? btw , it doesn't apply in this case
what's the .net equivalent?
-
Mar 26th, 2004, 09:49 PM
#11
Sleep mode
Originally posted by Bloged
On this code:
VB Code:
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:
While sLine <> Nothing
sLineArray = Split(sLine, " ")
iLineArray = DirectCast(sLineArray, Integer())
sLine = sreStream.ReadLine()
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
-
Mar 26th, 2004, 09:55 PM
#12
Sleep mode
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 .
-
Mar 27th, 2004, 12:16 PM
#13
Thread Starter
Hyperactive Member
Ok incl. the declarations:
VB Code:
Dim sreStream As StreamReader
Dim sLine As String
Dim sLineArray() As String
Dim iLineArray() As Integer
sreStream = File.OpenText("c:\input.txt")
sLine = sreStream.ReadLine()
While sLine <> Nothing
sLineArray = Split(sLine, " ")
iLineArray = DirectCast(sLineArray, Integer())
sLine = sreStream.ReadLine()
For iX = 0 To 9
pioPion(iLoop).SetCoordinates(iX, iY)
pioPion(iLoop).SetRang(iLineArray(iLoop))
Next
iY += 1
End While
Thanks for all the help guys!
Grtz,
Bloged
-
Mar 27th, 2004, 01:17 PM
#14
Sleep mode
You should try this line at the end of the While loop .
VB Code:
Dim i() As Integer = DirectCast(s, Integer())
-
Mar 27th, 2004, 02:20 PM
#15
Thread Starter
Hyperactive Member
One question: Will this:
VB Code:
sLineArray = Split(sLine, " ")
Add the current split line to the array or just overwrite it?!
Grtz,
Bloged
-
Mar 27th, 2004, 09:56 PM
#16
Sleep mode
Since arrays are referenced type , so it will replace it with what the string array returned by Split function.
-
Mar 28th, 2004, 06:27 AM
#17
Thread Starter
Hyperactive Member
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
-
Mar 28th, 2004, 12:14 PM
#18
Sleep mode
VB Code:
Private Sub test()
Dim sreStream As StreamReader
Dim sLine As String
Dim sLineArray() As String
sreStream = File.OpenText("c:\test.txt")
sLine = sreStream.ReadLine()
While sLine <> Nothing
sLineArray = Split(sLine, " ")
sLine = sreStream.ReadLine()
End While
For i As Integer = 0 To sLineArray.Length - 1
MsgBox(sLineArray(i))
Next
Dim ilineArray(sLineArray.Length - 1) As Integer
'Convert from string to interger
For i As Integer = 0 To sLineArray.Length - 1
ilineArray(i) = sLineArray(i)
Next
End Sub
-
Mar 28th, 2004, 12:58 PM
#19
PowerPoster
Here is another conversion you may want to use instead of CInt.
Integer.Parse(mystring)
-
Mar 29th, 2004, 06:29 AM
#20
Thread Starter
Hyperactive Member
VB Code:
Private Sub test()
Dim sreStream As StreamReader
Dim sLine As String
Dim sLineArray() As String
sreStream = File.OpenText("c:\test.txt")
sLine = sreStream.ReadLine()
While sLine <> Nothing
sLineArray = Split(sLine, " ")
sLine = sreStream.ReadLine()
End While
For i As Integer = 0 To sLineArray.Length - 1
MsgBox(sLineArray(i))
Next
Dim ilineArray(sLineArray.Length - 1) As Integer
'Convert from string to interger
For i As Integer = 0 To sLineArray.Length - 1
ilineArray(i) = sLineArray(i)
Next
End Sub
Wouldn't this just convert the last line from my file to integers?! Because this:
VB Code:
sLineArray = Split(sLine, " ")
Replaces the old Array with the new one created after Split()
would probably give the same error as
VB Code:
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|