Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27501

I don't understand why this code doesn't work.

$
0
0
Hi, I don't understand why this code doesn't work:
vb.net Code:
  1. Public Class Form1
  2.  
  3.     Dim y(4) As String
  4.  
  5.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  6.         Dim a, b As Integer, Cx(4) As CheckBox, z As String
  7.  
  8.         z = "Apples+Pears+Oranges+Bananas+Cherries+  "
  9.         a = InStr(z, "+")
  10.         b = 0
  11.         While a > 0
  12.             y(b) = Mid(z, 1, a - 1)            
  13.             Cx(b) = CType(Me.Controls("CheckBox" & b + 1), CheckBox)
  14.             Cx(b).Text = y(b)
  15.             z = Mid(z, a + 1)
  16.             a = InStr(z, "+")
  17.             b += 1
  18.         End While
  19.  
  20.         Button1.Text = "Check"
  21.         Button2.Text = "Exit"
  22.         GroupBox1.Text = "Fruit Stall"
  23.     End Sub
  24.  
  25.       Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  26.         Me.Close()
  27.         End
  28.     End Sub
  29.  
  30. End Class
On the first pass of the While loop, Line 13 results in Cx(0) = Nothing.
Then the code bombs out and displays the Form1 with the text for Check Boxes 1 to 5 showing their original names: CheckBox1 to CheckBox5, the GroupBox name is still GroupBox1 and the two buttons are still called Button1 and Button2.

I think I can name CheckBox1 Cx(0) can't I ? Then I figure I ought to be able to change it's text to "Apples" (or whatever).
Because I'm now using VS2013, I can't find how to set up a 'Watch' page, I have the choice of 'Watch 1 to 4' but none of them seem to allow me to enter variables to watch, I have to use the ''Instants' but then I can't read the values of my global String Array, y(4), presumably because it's not an instant. I temporarily set up a String variable x, where line 12 is to to check that I am reading the Mid$ correctly, and I am.

So, Guys... Where am I going wrong ?


Poppa.

Viewing all articles
Browse latest Browse all 27501

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>