Hi all
Here's my code then i'll post my error.
Object reference not set to an instance of an object.
on this line of code.
Basically it's loading 2 dirs into 2 listbox's as strings then passing them through the Distance Function and then outputing the results into a treeview.
Here's my code then i'll post my error.
VB.NET:
Public Class Form1
Dim lb1 As String
Dim lb2 As String
Public Class StringSift2
Private maxOffset As Integer
Public Sub New()
Me.New(5)
End Sub
Public Sub New(ByVal maxOffset As Integer)
Me.maxOffset = maxOffset
End Sub
Public Function Distance(ByVal s1 As String, ByVal s2 As String) As Single
If [String].IsNullOrEmpty(s1) Then
Return If([String].IsNullOrEmpty(s2), 0, s2.Length)
End If
If [String].IsNullOrEmpty(s2) Then
Return s1.Length
End If
Dim c As Integer = 0
Dim offset1 As Integer = 0
Dim offset2 As Integer = 0
Dim dist As Integer = 0
While (c + offset1 < s1.Length) AndAlso (c + offset2 < s2.Length)
If s1(c + offset1) <> s2(c + offset2) Then
offset1 = 0
offset2 = 0
For i As Integer = 0 To maxOffset - 1
If (c + i < s1.Length) AndAlso (s1(c + i) = s2(c)) Then
If i > 0 Then
dist += 1
offset1 = i
End If
GoTo ender
End If
If (c + i < s2.Length) AndAlso (s1(c) = s2(c + i)) Then
If i > 0 Then
dist += 1
offset2 = i
End If
GoTo ender
End If
Next
dist += 1
End If
ender:
c += 1
End While
Return dist + (s1.Length - offset1 + s2.Length - offset2) / 2 - c
End Function
Public Function Similarity(ByVal s1 As String, ByVal s2 As String) As Single
Dim dis As Single = Distance(s1, s2)
Dim maxLen As Integer = Math.Max(s1.Length, s2.Length)
If maxLen = 0 Then
Return 1
Else
Return 1 - dis / maxLen
End If
End Function
End Class
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
FolderBrowserDialog2.ShowDialog()
TextBox2.Text = FolderBrowserDialog2.SelectedPath
If TextBox2.Text = "" Then
TextBox4.Text = ""
Else
TextBox4.Text = ListBox2.Items.Count.ToString
ListBox2.Items.AddRange(My.Computer.FileSystem.GetFiles(TextBox2.Text).ToArray)
ListBox2.SelectedIndex = +1
For i As Integer = 0 To Me.ListBox2.Items.Count - 1
Dim fi As New IO.FileInfo(Me.ListBox2.Items(i))
Me.ListBox2.Items(i) = Me.ListBox2.Items(i).ToString.Replace(fi.Directory.ToString & "\", "")
Next
lb2 = ListBox2.Items.IndexOf(ListBox2.SelectedItems)
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim MyObject As New StringSift2
Dim ReturnVal As Single
ReturnVal = MyObject.Distance(ListBox1.SelectedItem, ListBox2.SelectedItem)
If ReturnVal = -1 Then
End If
TreeView1.Nodes(lb1).Nodes.Add(ReturnVal.ToString())
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
FolderBrowserDialog1.ShowDialog()
TextBox1.Text = FolderBrowserDialog1.SelectedPath
If TextBox1.Text = "" Then
TextBox3.Text = ""
Else
TextBox3.Text = ListBox1.Items.Count.ToString
ListBox1.Items.AddRange(My.Computer.FileSystem.GetFiles(TextBox1.Text).ToArray)
ListBox1.SelectedIndex = +1
For i As Integer = 0 To Me.ListBox1.Items.Count - 1
Dim fi As New IO.FileInfo(Me.ListBox1.Items(i))
Me.ListBox1.Items(i) = Me.ListBox1.Items(i).ToString.Replace(fi.Directory.ToString & "\", "")
Next
lb1 = ListBox1.Items.IndexOf(ListBox1.SelectedItems)
End If
End Sub
End Class
Object reference not set to an instance of an object.
on this line of code.
VB.NET:
TreeView1.Nodes(lb1).Nodes.Add(ReturnVal.ToString())
Basically it's loading 2 dirs into 2 listbox's as strings then passing them through the Distance Function and then outputing the results into a treeview.