Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > Assistance checking code


Reply
 
Thread Tools Display Modes
  #1  
Old 11-29-2007, 02:15 AM
jeneal jeneal is offline
Newcomer
 
Join Date: Nov 2007
Posts: 3
Default Assistance checking code


I was asked to create a program that would take two cities put into text boxes, compair them with a declared string array.

Also a 2d array will be created and filled with random numbers. The array is a mirror of itself such that position 1,2 and 2,1 are the same, the same concept repeating through the array, where variable x = y the result of the 2darray is 0.

The program loops through and checks if the city is vaild and stores it position in the array such that city1 = x and city2 = y. If the city is not found in the array an error box is displayed. When both cities are valid, a random number shows up in the third text box.

Theoretically since the array is mirrored, it shouldn't matter the city order the output should be the same between say cleveland and canton compaired to canton and cleveland. If the cities are the same then the result is zero.

For reasons that I can't explain random numbers apper in the outbox but the above conditions do not happen. I've tried many diffrent ways of writing the code, but here is my latest version. If someone can look at it and point me into the right direction I would appreciate it.

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cities() As String = {"Canton", "Cincinnati", "Cleveland", "Columbus", "Dayton", "Toledo", "Youngstown"}
Dim city1 As String = TextBox1.Text
Dim city2 As String = TextBox2.Text

Dim cityVal As Integer = 0
Dim xVal As Integer = 0
Dim yVal As Integer = 0
Dim mi As Integer

Dim miles(7, 7) As Integer
Dim randomGenerator As New Random

While cityVal <= 6
For check1 As Integer = 0 To check1 <= 6
cityVal = city1.CompareTo(cities(check1))
If cityVal <= 6 Then
xVal = check1
check1 = 7
For check2 As Integer = 0 To check2 <= 6
cityVal = city2.CompareTo(cities(check2))
If cityVal <= 6 Then
yVal = check2
check2 = 7
Else
MsgBox("Invalid City!", MsgBoxStyle.Exclamation)
End If
Next check2
Else
MsgBox("Invalid City!", MsgBoxStyle.Exclamation)
End If
Next check1
cityVal = 7
End While

For x As Integer = 0 To 6
For y As Integer = 0 To 6
If x = y Then
miles(x, y) = 0
End If
mi = randomGenerator.Next(1, 1000)
miles(x, y) = mi
miles(y, x) = miles(x, y)
Next
Next

Dim distance As Integer
distance = miles(xVal, yVal)
TextBox3.Text = distance

End Sub

End Class
Reply With Quote
  #2  
Old 11-29-2007, 08:10 AM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,416
Default

The first problem that I see is the following lines:
17: For check1 As Integer = 0 To check1 <= 6
22: For check2 As Integer = 0 To check2 <= 6


This causes the compilation process to fail on a machine with Option Strict On, for good reason. The For loop expects this format:
For startValue To endValue

And it expands to code like this:
Dim i as integer = startValue
While (i < endValue)
' code
End While


Now, endValue must be of the same data type as startValue for this to be true. The expression check1 <= 6 is a Boolean expression, so with Option Strict off the compiler does its best to convert the result to Integer. A quick look in the immediate window shows that as far as the compiler is concerned, True = -1 and False = 0, so your For loops look like this:
17: For check1 As Integer = 0 To 0
22: For check2 As Integer = 0 To 0

... and never execute. The first for loop is skipped, cityVal is set to 7 every time, then you break out of the While loop.

What you probably meant for those lines to say is:
17: For check1 As Integer = 0 To 7
22: For check2 As Integer = 0 To 7


The next problem is you haven't looked at what your code does. What you seem to want is to generate an array of distances once, then keep checking it. Instead, the array of distances is generated randomly every time you click the button, so if I use the same cities twice I get a different answer, and the odds of city1 -> city2 being equal to city2 -> city1 are very slim. You probably want to move the array generation out of the button click and into something that only executes once, like the Form's Load event. This will require moving the array of distances out of the method, since it is being destroyed every time the method completes.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #3  
Old 11-29-2007, 10:26 AM
jeneal jeneal is offline
Newcomer
 
Join Date: Nov 2007
Posts: 3
Default

I've applied your changes as suggested. Thank you for the For loop correction. I have attempted to create the array with the forms load procedure but run across many issues, like the entire array is filled with one number, and the zero mile distance when x=y doesn't work either. Any more sugestions?

Public Class Form1
Dim miles(7, 7) As Integer

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cities() As String = {"Canton", "Cincinnati", "Cleveland", "Columbus", "Dayton", "Toledo", "Youngstown"}
Dim city1 As String = TextBox1.Text
Dim city2 As String = TextBox2.Text

Dim cityVal As Integer = 0
Dim xVal As Integer = 0
Dim yVal As Integer = 0

While cityVal <= 6
For check1 As Integer = 0 To 6
cityVal = city1.CompareTo(cities(check1))
If cityVal <= 6 Then
xVal = check1
check1 = 7
For check2 As Integer = 0 To 6
cityVal = city2.CompareTo(cities(check2))
If cityVal <= 6 Then
yVal = check2
check2 = 7
Else
MsgBox("Invalid City!", MsgBoxStyle.Exclamation)
End If
Next check2
Else
MsgBox("Invalid City!", MsgBoxStyle.Exclamation)
End If
Next check1
cityVal = 7
End While

Dim distance As Integer
distance = miles(xVal, yVal)
TextBox3.Text = distance

End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim mi As Integer
Dim randomGenerator As New Random

For x As Integer = 0 To 6
For y As Integer = 0 To 6
mi = randomGenerator.Next(1, 1000)
miles(x, y) = mi
miles(y, x) = miles(x, y)
If x = y Then
miles(x, y) = 0
End If
Next
Next
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
End Sub
End Class
Reply With Quote
  #4  
Old 11-29-2007, 12:09 PM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,416
Default

I cannot reproduce this problem, it works fine for me.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->