A bug is a simple subroutine (Vb.net)

  • Thread starter Thread starter Head_one
  • Start date Start date
H

Head_one

Guest
Hello Everybody,

My program is stuck because a small bug, which I can not solve.

Last week I sent to the forum a function with a problem.

A few of you gave me the solution how to solve the problem:

Defining a list with a class instead of with a structure, as I did.

This helped me, but caused a new problem:

In the attached code Sub Create_all_sub_Strings does the following:

It get a string "MNOPQRST" , and has to fill a list with sub strings as follows


Loop 1: 1 substring of all 8 chars: "MNOPQRST" ‘ original string

Loop 2: 2 substrings of 7 chars: "MNOPQRS" "NOPQRST"

Loop 3: 3 substrings of 6 chars: "MNOPQR" "NOPQRS" "OPQRST"

..

..

Loop 8: 8 substrings of 1 char: “M” “N” “O” “P” “Q” “R” “S” “T”



Sub Print_Sub_Strings prints the results into listbox named LBOX1



Results:

With List1 defined as a structure - it works perfect. ‘ Uncomment and check.

With List1 defined as a class, Print_Sub_Strings displays WRONG RESULTS

But the results are 100% CORRECT when printing them from inside: Create_all_sub_Strings

It mean, there is a problem with List1 when going back to Run_but_Click,

and not with Sub Create_all_sub_strings itself.

This is the bug I can not solve, ask ask for your help.



Public Class Form1 ‘ This works

Structure Str_Rec ' String record

Dim Str As String

Dim Loc As String

End Structure



'Class Str_Rec ‘THIS DOED NOT WORK

' Property Str As String

' Property Loc As String

'End Class



Dim List1 As New List(Of Str_Rec)

Dim Found As Boolean

Dim s5 As String = "MNOPQRST"



Private Sub Run_but_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Run_but.Click

Dim k As Integer = s5.Length + 1


Do

k = k - 1 ' k loops between 8 downto 1

Create_all_sub_Strings(s5, k, List1)

Print_Sub_Strings(List1)

List1.Clear()

Loop Until k = 1 'Or Found = True

End Sub



Private Sub Create_all_sub_Strings(ByVal s As String, ByVal k As Integer, ByRef List1 As List(Of Str_Rec))

Dim n As Integer

Dim st As Integer

Dim Mila As New Str_Rec ' beter is global. This procedure runs a few times

Dim Max As Integer = s.Length - k + 1 ' Number of created strings

st = 0

For n = 1 To Max

Mila.Str = s.Substring(st, k)

' Mila.Loc = "" ' This is not the problem

List1.Add(Mila)

st = st + 1

' Lbox1.Items.Add(Mila.Str) ' Printing here gives CORRECT RESULTS WITH CLASS DEFINITION!!

Next

' Lbox1.Items.Add("")

End Sub



Private Sub Print_Sub_Strings(ByRef List1 As List(Of Str_Rec))

Dim Cell As New Str_Rec

For Each Cell In List1

Lbox1.Items.Add(Cell.Str) ' & " Loc: " & Cell.Loc)

Next

Lbox1.Items.Add("") ' Empty separation line

End Sub


End Class



Thanks to all Helpers,

Moshe.


All things are difficult before they are easy. (unknown)

Continue reading...
 
Back
Top