| 2018/01/02(Tue) 21:53:23 編集(投稿者)
■No86236 (へなちょこ さん) に返信 >> とりあえず VB2017 向けのコードを書いてみました。 > > ただ、私の勉強不足でコードをそのまま張り付けた際のエラーがよく分からない状態です。
VB2017 以降でないと使用できない構文を利用しています。 お使いのバージョンは何でしょうか?
> Public Iterator Function EnumerateTuple(
「Iterator」や「Yield」を使うには、VB2012 以降のバージョンが必要です。
> As IEnumerable(Of (Hena As String, Choco As String))
Visual Basic のタプル構文を使うには、VB2017 以降のバージョンが必要です。
System.Tuple クラスを使うには .NET 4.0 以降が必要です。
System.ValueTuple 構造体を使うには .NET 4.7 以降を採用するか、 もしくは nuget から System.ValueTuple を参照する必要があります。
ひとまず、VB2005 で動くと思われる程度にまでコードを書き換えてみました。 (これでも 2002/2003 だと動かないのですが)
Sub Main() For Each entry As String() In EnumerateTuple("Sample.dat", System.Text.Encoding.Unicode) MsgBox(entry(0) & vbCrLf & entry(1)) Next End Sub
Public Function EnumerateTuple(fileName As String, enc As System.Text.Encoding) As List(Of String()) Dim list As New List(Of String())() Using stm As New FileStream(fileName, FileMode.Open), reader As New BinaryReader(stm) Do While stm.Position < stm.Length reader.ReadBytes(2) '最初の2バイトは読み捨て Dim len1 As Integer = 2 * reader.ReadByte() '文字1のバイト数 Dim len2 As Integer = 2 * reader.ReadByte() '文字2のバイト数 reader.ReadBytes(4) '次の4バイトは読み捨て Dim bin1 As Byte() = reader.ReadBytes(len1) '文字列1のデータ Dim bin2 As Byte() = reader.ReadBytes(len2) '文字列2のデータ Dim txt1 As String = enc.GetString(bin1) Dim txt2 As String = enc.GetString(bin1) list.Add(New String() {txt1, txt2}) Loop End Using Return list End Function |