' テキストファイルから読み出すサンプル。' 鵜呑みにするべからず。Module Program Sub Main() Dim reader As New System.IO.StreamReader("data.txt") Dim buffer As String = reader.ReadLine() Dim data(32) As Integer Dim size As Integer = 0 Dim i As Integer Do Until IsNothing(buffer) For Each token As String In buffer.Split(" ") data(size) = Integer.Parse(token) size = size + 1 Next buffer = reader.ReadLine() Loop reader.Close() For i = 0 To size - 1 System.Console.WriteLine(data(i)) Next End SubEnd Module
> String型配列Xを > CSng(x) > と浮動小数点型に変換し、計算させようとしましたが、「キャストが有効でありません」と言われました。 当然です。CSngに与えるのは配列ではありません。 # マニュアルを読んでいますか? Module Program Sub Main() Dim data(2) As String Dim value As Single data(0) = "12.3" data(1) = "23.4" data(2) = "34.5" For Each item As String In data value = CSng(item) System.Console.WriteLine(value) Next End Sub End Module
Single配列に変換したかったんだ。ならば: Module Program Sub Main() Dim input(2) As String Dim output() As Single input(0) = "12.3" input(1) = "23.4" input(2) = "34.5" output = System.Array.ConvertAll(Of String,Single)(input, AddressOf Single.Parse) For Each value As Single In output System.Console.WriteLine(value) Next End Sub End Module
- Child Tree -