9 Replies - 471 Views - Last Post: 21 January 2013 - 01:46 AM Rate Topic: -----

#1 talahib098  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 16-January 13

Need to know what did I do wrong in this VB code....

Posted 16 January 2013 - 06:37 AM

Public Class Form1
    Dim x As Integer = 1
    Dim y As Integer = 1
    Dim z As String
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Do
            z = ""
            Do
                z = z & (y * x) & " "

                y = y + 1

            Loop While (y <= 10)
            x = x + 1
            lstBox.Items.Add(z)
        Loop While (x <= 10)

    End Sub
End Class


Is This A Good Question/Topic? 0
  • +

Replies To: Need to know what did I do wrong in this VB code....

#2 DarenR  Icon User is offline

  • D.I.C Lover

Reputation: 484
  • View blog
  • Posts: 3,246
  • Joined: 12-January 10

Re: Need to know what did I do wrong in this VB code....

Posted 16 January 2013 - 06:38 AM

errors?
Was This Post Helpful? 0
  • +
  • -

#3 talahib098  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 16-January 13

Re: Need to know what did I do wrong in this VB code....

Posted 16 January 2013 - 06:39 AM

yep...I'm doing some multiplication table...
Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Need to know what did I do wrong in this VB code....

Posted 16 January 2013 - 08:10 AM

Look at your Y variable. I'm betting you never want it to get above 10 but you're code allows it to.

I'm betting resetting it back to an initial value somewhere will fix your issue.

This post has been edited by CharlieMay: 16 January 2013 - 08:13 AM

Was This Post Helpful? 0
  • +
  • -

#5 talahib098  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 16-January 13

Re: Need to know what did I do wrong in this VB code....

Posted 16 January 2013 - 03:00 PM

View PostCharlieMay, on 16 January 2013 - 08:10 AM, said:

Look at your Y variable. I'm betting you never want it to get above 10 but you're code allows it to.

I'm betting resetting it back to an initial value somewhere will fix your issue.


Well I'm on the right to Y...Because I need it to go up to 10...what I did wrong was the x part...it won't go down with 2,3,4, and so on...also the 2nd row of numbers together with the others are not showing the numbers right...that's why I'm asking what did I do wrong with the code...
Was This Post Helpful? 0
  • +
  • -

#6 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Need to know what did I do wrong in this VB code....

Posted 16 January 2013 - 03:49 PM

View Posttalahib098, on 16 January 2013 - 04:00 PM, said:

View PostCharlieMay, on 16 January 2013 - 08:10 AM, said:

Look at your Y variable. I'm betting you never want it to get above 10 but you're code allows it to.

I'm betting resetting it back to an initial value somewhere will fix your issue.


Well I'm on the right to Y...Because I need it to go up to 10...what I did wrong was the x part...it won't go down with 2,3,4, and so on...also the 2nd row of numbers together with the others are not showing the numbers right...that's why I'm asking what did I do wrong with the code...

Did you even look at your code while thinking about what CharlieMay said?

After your first time through the inner Do loop, y has a value 0f 11. You then put z into the ListBox, and go back to generate the next line. y STILL has a value of 11, and you multiply it by x, which has a value of 2, giving you 22. You then drop out of the loop because y is greater than 10.

So no, you do NOT have y right.

You would have found this problem in about 30 seconds if you had set a breakpoint and single-stepped through the code. For information on how to do that, see our tutorial on Debugging Skills.
Was This Post Helpful? 0
  • +
  • -

#7 talahib098  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 16-January 13

Re: Need to know what did I do wrong in this VB code....

Posted 16 January 2013 - 04:10 PM

View Postlar3ry, on 16 January 2013 - 03:49 PM, said:

View Posttalahib098, on 16 January 2013 - 04:00 PM, said:

View PostCharlieMay, on 16 January 2013 - 08:10 AM, said:

Look at your Y variable. I'm betting you never want it to get above 10 but you're code allows it to.

I'm betting resetting it back to an initial value somewhere will fix your issue.


Well I'm on the right to Y...Because I need it to go up to 10...what I did wrong was the x part...it won't go down with 2,3,4, and so on...also the 2nd row of numbers together with the others are not showing the numbers right...that's why I'm asking what did I do wrong with the code...

Did you even look at your code while thinking about what CharlieMay said?

After your first time through the inner Do loop, y has a value 0f 11. You then put z into the ListBox, and go back to generate the next line. y STILL has a value of 11, and you multiply it by x, which has a value of 2, giving you 22. You then drop out of the loop because y is greater than 10.

So no, you do NOT have y right.

You would have found this problem in about 30 seconds if you had set a breakpoint and single-stepped through the code. For information on how to do that, see our tutorial on Debugging Skills.


in saying that...I did thought that I was really wrong about y there...sorry didn't understand what CharlieMay said...
Was This Post Helpful? 0
  • +
  • -

#8 torind_2000  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 57
  • View blog
  • Posts: 292
  • Joined: 22-August 11

Re: Need to know what did I do wrong in this VB code....

Posted 17 January 2013 - 07:04 AM

Here's what I see your loops do.
Mind you, I am far from an expert.

'outer loop

Do
  z = ""
  ' sets Z, then start inner loop.

   'inner loop
     do 
       instructions 
     Loop while y <= 10
   'end inner loop

   ' inner loop processes itself until Y is less than or equal to 10
   ' then it jumps to the next instructions in the outer loop.

  x = x + 1
  listbox.add(Stuff)
While x <= 10
  ' so we add one to X, check to see if X is less than or equal to 10 and start over. Y STILL equals ten, so the inner loop doesn't loop but does process the instruction once since your do loop checks AFTER the instructions are done. But now Y = 11

'end outer loop


If you don't reset Y somewhere as instructed, Y will continue to increment past 10 until the outer loop finishes (X <= 10)
Was This Post Helpful? 0
  • +
  • -

#9 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Need to know what did I do wrong in this VB code....

Posted 17 January 2013 - 09:22 AM

View Posttorind_2000, on 17 January 2013 - 08:04 AM, said:

Here's what I see your loops do.
Mind you, I am far from an expert.

   ' inner loop processes itself until Y is less than or equal to 10
   ' then it jumps to the next instructions in the outer loop.


No, it processes until Y is greater than 10.
Was This Post Helpful? 0
  • +
  • -

#10 talahib098  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 16-January 13

Re: Need to know what did I do wrong in this VB code....

Posted 21 January 2013 - 01:46 AM

Public Class Form1
	    Dim x As Integer 
	    Dim y As Integer 
	    
	    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            x = 1
	        Do
	            y = 1
	            Do
	            lstBox.Items.Add(x * y)
	 
	                y = y + 1
	 
	            Loop While (y <= 10)
	            x = x + 1
	            
	        Loop While (x <= 10)
	 
	    End Sub
	End Class


well that's the answer, I got it just a moment ago...where we will gonna pass it to my prof...tnx for the advice and hints ^_^
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1