C# と VB.NET の質問掲示板

わんくま同盟

ASP.NET、C++/CLI、Java 何でもどうぞ

C# と VB.NET の入門サイト


(過去ログ 124 を表示中)
■73836 / )  Re[11]: txtファイルを1行づつ比較
□投稿者/ 開発者 (6回)-(2014/11/09(Sun) 12:09:47)
No73834 (ainax さん) に返信
> 横より失礼します。
> ちょっとコードを書いて見ました。
>
> 新規プロジェクトのフォームにラベル3個とボタン1個で動くと思います。
>
> Public Class Form1
>
> Private Comp As Compare = New Compare()
>
> Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
> Me.Button1.Text = "Next"
>
> Comp.ReadTextA("C:\AAA.txt")
> Comp.ReadTextB("C:\BBB.txt")
>
> Comp.MoveFirst()
>
> AddHandler Comp.NextResult, AddressOf OnNextResult
> End Sub
>
> Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
> Comp.OnNextResult()
> End Sub
>
> Private Sub OnNextResult(value1 As String, value2 As String, value3 As String)
> Me.Label1.Text = value1
> Me.Label2.Text = value2
> Me.Label3.Text = value3
> End Sub
>
> End Class
>
>
> Public Class Compare
> Private TextA As List(Of String) = New List(Of String)
> Private TextB As List(Of String) = New List(Of String)
>
> Public Sub ReadTextA(path As String)
> TextA.Clear()
> TextA.AddRange(Read(path))
> End Sub
> Public Sub ReadTextB(path As String)
> TextB.Clear()
> TextB.AddRange(Read(path))
> End Sub
>
> Private Iterator Function Read(path As String) As IEnumerable(Of String)
> Using sr As New IO.StreamReader(path)
> While (Not sr.EndOfStream)
> Yield sr.ReadLine()
> End While
> End Using
> End Function
>
> Private _Current As Integer = 0
>
> Public Sub MoveFirst()
> _Current = 0
> End Sub
>
> Public Property EndOfList()
> Get
> Return (_Current >= TextA.Count And _Current >= TextB.Count)
> End Get
> Private Set(value)
>
> End Set
> End Property
>
> Public Event NextResult As Action(Of String, String, String)
>
> Public Sub OnNextResult()
> If EndOfList Then Exit Sub
>
> Dim retA As String = TextA.Skip(_Current).Take(1).FirstOrDefault()
> Dim retB As String = TextB.Skip(_Current).Take(1).FirstOrDefault()
>
> _Current += 1
>
> If retA Is Nothing OrElse retB Is Nothing Then
> If retA Is Nothing AndAlso retB Is Nothing Then
> RaiseEvent NextResult(retA, retB, Nothing) ' 両方ともNothing
> Else
> RaiseEvent NextResult(retA, retB, "") ' 片方だけNothing
> End If
>
> Exit Sub
> End If
>
> RaiseEvent NextResult(retA, retB, IIf(retA = retB, retA, ""))
> End Sub
> End Class
>
> # ファイル読み込みのエラー処理とかしてません。

ありがとうございます。
返信 編集キー/


管理者用

- Child Tree -