|
-
Jul 2nd, 2007, 08:48 AM
#1
Thread Starter
Addicted Member
Loop Problem
My code is working fine, but when i add a textbox value into be loop, the form does not load anymore. Does anybody know what can be wrong?
-
Jul 2nd, 2007, 08:52 AM
#2
Re: Loop Problem
You'll have to post the code.
-
Jul 2nd, 2007, 08:52 AM
#3
Re: Loop Problem
What code?
EDIT: Too late again.
-
Jul 2nd, 2007, 08:58 AM
#4
Thread Starter
Addicted Member
Re: Loop Problem
vb Code:
Imports System.IO
Public Class Form1
Private Function CompareStrings(ByVal x As String, ByVal y As String) As Integer
Dim xtest = x.Substring(x.IndexOf(")") + 5)
Dim xtest2 = xtest.Substring(xtest.IndexOf(""))
Dim strArray1() As String = xtest2.Split(" ")
Dim xtest3 = strArray1(0)
Dim ytest = y.Substring(y.IndexOf(")") + 5)
Dim ytest2 = ytest.Substring(ytest.IndexOf(""))
Dim strArray2() As String = ytest2.Split(" ")
Dim ytest3 = strArray2(0)
Dim numX As Integer = xtest3
Dim numY As Integer = ytest3
If Integer.TryParse((numX), numX) AndAlso _
Integer.TryParse((numY), numY) Then
Return numY - numX
Else
MsgBox("Failed")
Return 0
End If
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim nwline As String
Dim strmReader As New StreamReader("C:\Users\Ethan Hayon\Desktop\bssoutput2.txt")
Do While strmReader.Peek > -1
nwline = strmReader.ReadLine
If nwline.Contains("nw") Then
ListBox1.Visible = True
ListBox1.Items.Add(nwline)
Dim count = ListBox1.Items.Count()
TextBox2.Text = count
Dim Strings As New List(Of String)
For Each itm As String In Me.ListBox1.Items
Dim myWords As String = Nothing
myWords &= itm & Environment.NewLine
Strings.Add(itm)
Next
Strings.Sort(New Comparison(Of String)(AddressOf CompareStrings))
Dim nwlinestring = String.Join(Environment.NewLine, Strings.ToArray())
TextBox9.Text = nwlinestring
Dim line As Integer = 1
Do While count <= 1
' Dim strArray() As String = nwlinestring.Split(" "c)
' Dim nwlinestringsplit = strArray(40)
' MsgBox(nwlinestringsplit)
Loop
'Do Until line <= count
' TextBox6.Text = nwlinestring
'Loop
End If
Loop
strmReader.Close()
End Sub
End Class
thats my code
look at the "Do While count...." one
in the loop with three commented out lines, when i define a textbox.text value, the form does not load
-
Jul 2nd, 2007, 09:03 AM
#5
Re: Loop Problem
Turn Option Strict ON and correct the errors you will get.
-
Jul 2nd, 2007, 09:06 AM
#6
Thread Starter
Addicted Member
Re: Loop Problem
is there any other way, my code worked before with no errors
now i got 30 errors
-
Jul 2nd, 2007, 09:12 AM
#7
Re: Loop Problem
That is because you have written the code in a bad way. Badly written code can cause problems like the one youre dealing with now.
Heres an example from your code:
VB.Net Code:
Dim count = ListBox1.Items.Count()
TextBox2.Text = count
You are declaring the variable count, but you do not specify what type it should be. Then you assign an integer to it. THEN you assign the count variable to TextBox2.Text, which expects a string.
Just some simple changes makes it all better:
VB.Net Code:
Dim count As String = Cstr(ListBox1.Items.Count)
TextBox2.Text = count
-
Jul 2nd, 2007, 09:12 AM
#8
Re: Loop Problem
Turning Option Strict Off is like removing the oil light from your dashboard instead of topping up the oil. 
Turn it on, fix the errors, and you'll have much nicer code that will be easier to debug. You should always start any new projects with it on.
-
Jul 2nd, 2007, 09:13 AM
#9
Re: Loop Problem
 Originally Posted by Atheist
VB.NET Code:
Dim count As String = Cstr(ListBox1.Items.Count)
TextBox2.Text = count
Simpler still:
VB.NET Code:
TextBox2.Text = ListBox1.Items.Count.ToString()
Unless you need them more than once, storing things like string representations of numbers in string variables is just inviting use of them instead of the original numerical variables. Converting to string should always be a one way operation, with the exception of parsing user input.
-
Jul 2nd, 2007, 09:21 AM
#10
Re: Loop Problem
Your code may have worked, but those errors were all real. Most of the errors are probably situations where you were implicitly converting something that now has to be explicitly converted. This can greatly increase speed, but it can also catch lots of subtle bugs. Option Strict will force you to be a better coder, and should be set to On for each and every project. You will have less bugs, faster code, and a better understanding of what you are doing. The cost will be that you have to type slightly more.
As for the code, here are a few things to consider:
1) You hold the count of items in the listbox in a textbox. Why not a label? What is the value of having the count be editable (which is all a textbox is good for)?
2) The same comment might be considered for what you are putting in TextBox9.
3) I see two sets of three commented out lines. The first set doesn't mention a textbox anywhere in it, and the second set doesn't seem to do anything except fill a textbox over and over. Therefore, I don't see where you are having problems, because neither group of lines seems to match the problem you have described.
However, in general, the thing to do would be to add a breakpoint somewhere above the area where the problem is (seems like you have identified that the problem occurs in the loop, so add the breakpoint above that). When the execution reaches the breakpoint it will pause. From that point, you can step through the code (F11), stepping over functions that you don't want to go through (F10) if there are any. You can also look at what is in any variable by either putting the mouse over it and seeing the tooltip, or by highlighting the variable and pressing Shift+F9. This will give you a much better idea of what is going wrong.
Still, you really SHOULD use Option Strict.
My usual boring signature: Nothing
 
-
Jul 2nd, 2007, 09:22 AM
#11
My usual boring signature: Nothing
 
-
Jul 2nd, 2007, 09:49 AM
#12
Thread Starter
Addicted Member
Re: Loop Problem
okay, i fixed that with option strict on, but i am still having the same problem...
vb Code:
Option Strict On
Imports System.IO
Public Class Form1
Private Function CompareStrings(ByVal x As String, ByVal y As String) As Integer
Dim xtest As String = x.Substring(x.IndexOf(")") + 5)
Dim xtest2 As String = xtest.Substring(xtest.IndexOf(""))
Dim strArray1() As String = xtest2.Split(" "c)
Dim xtest3 As String = strArray1(0)
Dim ytest As String = y.Substring(y.IndexOf(")") + 5)
Dim ytest2 As String = ytest.Substring(ytest.IndexOf(""))
Dim strArray2() As String = ytest2.Split(" "c)
Dim ytest3 As string = strArray2(0)
Dim numX As Integer = CInt(xtest3)
Dim numY As Integer = CInt(ytest3)
If Integer.TryParse(CStr((numX)), numX) AndAlso _
Integer.TryParse(CStr((numY)), numY) Then
Return numY - numX
Else
MsgBox("Failed")
Return 0
End If
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim nwline As String
Dim strmReader As New StreamReader("C:\Users\Ethan Hayon\Desktop\bssoutput2.txt")
Do While strmReader.Peek > -1
nwline = strmReader.ReadLine
If nwline.Contains("nw") Then
ListBox1.Visible = True
ListBox1.Items.Add(nwline)
Dim count As Integer = ListBox1.Items.Count()
TextBox2.Text = CStr(count)
Dim Strings As New List(Of String)
For Each itm As String In Me.ListBox1.Items
Dim myWords As String = Nothing
myWords &= itm & Environment.NewLine
Strings.Add(itm)
Next
Strings.Sort(New Comparison(Of String)(AddressOf CompareStrings))
Dim nwlinestring As String = String.Join(Environment.NewLine, Strings.ToArray())
TextBox9.Text = CStr(nwlinestring)
Dim line As Integer = 1
Do while count <= 10
' Dim strArray() As String = nwlinestring.Split(" "c)
' Dim nwlinestringsplit = strArray(40)
' MsgBox(nwlinestringsplit)
TextBox1.Text = "test"
Loop
'Do Until line <= count
' TextBox6.Text = nwlinestring
'Loop
End If
Loop
strmReader.Close()
End Sub
End Class
Last edited by ethanhayon; Jul 2nd, 2007 at 09:56 AM.
-
Jul 2nd, 2007, 09:52 AM
#13
Thread Starter
Addicted Member
Re: Loop Problem
oh, and by the way, count = 7
-
Jul 2nd, 2007, 10:09 AM
#14
Fanatic Member
Re: Loop Problem
oh, and by the way, count = 7
It looks as though you would be stuck in a never ending loop. Count is equal to the listitem count, but the system does not know to check again while it is in your loop. Maybe change the do while to an if else statement or change the criteria for the do while or add a statement that updates count in your do statment.
D
Last edited by dminder; Jul 2nd, 2007 at 10:13 AM.
Platforms of choice: Visual Studio 2005/2008 Professional : Visual Studio 2010 Enterprise : PHP - Notepad++/WAMP
Please Rate If I helped you. 
Please remember to mark threads as closed if your issue has been resolved.
Reserved Words in Access | Connection Strings
-
Jul 2nd, 2007, 10:11 AM
#15
Re: Loop Problem
dminder is right. If you do the breakpoint thing and step through the loop, you will see that since count remains always less than 10 in the loop, then the loop never finishes.
My usual boring signature: Nothing
 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|