Results 1 to 33 of 33

Thread: [2005] for loop problem

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Hong Kong
    Posts
    384

    Wink [2005] for loop problem

    Code:
            For i = 0 To 27
                Dim value As Integer = CInt(Int((10 * Rnd()) + 1))
                If i / 9 = 0 Then
                   '...
                End If
            Next i
    If I havd the Labels, there are Label1,Label2,Label3,Label4...etc...Label27
    If i / 9 = 0, then the i th of label text is "Hello", otherwise, it will show "Hi"
    how can I do it?

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    Re: [2005] for loop problem

    The only time the if block will be executed is when i = 0, so I don't see why a simply if/else will do.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Hong Kong
    Posts
    384

    Re: [2005] for loop problem

    Quote Originally Posted by crptcblade
    The only time the if block will be executed is when i = 0, so I don't see why a simply if/else will do.
    but my if/else is doing in the for loop

  4. #4
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [2005] for loop problem

    vb Code:
    1. For i As Integer = 0 To 27
    2.             If i Mod 9 Then
    3.                 MsgBox("Hello")
    4.             Else
    5.                 MsgBox("Hi")
    6.             End If
    7.         Next

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Hong Kong
    Posts
    384

    Re: [2005] for loop problem

    Quote Originally Posted by shakti5385
    vb Code:
    1. For i As Integer = 0 To 27
    2.             If i Mod 9 Then
    3.                 MsgBox("Hello")
    4.             Else
    5.                 MsgBox("Hi")
    6.             End If
    7.         Next
    but it is doing in Label, not msg box
    in java, it may be done by:

    Label+i=...
    or
    Label[i]=...
    but I have created 27 labels

  6. #6
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: [2005] for loop problem

    What is the use of having
    Code:
    i / 9 = 0
    ?
    Won't the For Loop alone do?

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Hong Kong
    Posts
    384

    Re: [2005] for loop problem

    Quote Originally Posted by Hell-Lord
    What is the use of having
    Code:
    i / 9 = 0
    ?
    Won't the For Loop alone do?
    the for loop will add i from 1 to 27, then so
    i/9=0:
    label1's text= "HI"
    label2's text="HI"
    .
    .
    .
    label9's text="Hello"
    .
    .
    .
    label18's text="Hello"
    .
    .
    .
    label27's text="Hello"

  8. #8
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2005] for loop problem

    If you put your 27 labels into an array then you can reference them by index
    Code:
    Dim Labels() As Label = {Label1, Label2, Label3, ..., Label27}
    For i As Integer = 0 To 26            
        If (i Mod 9) = 0 Then                
            Labels(i).Text = "Hello"
        Else                
            Labels(i).Text = "Hi"
        End If        
    Next

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Hong Kong
    Posts
    384

    Re: [2005] for loop problem

    Quote Originally Posted by stanav
    If you put your 27 labels into an array then you can reference them by index
    Code:
    Dim Labels() As Label = {Label1, Label2, Label3, ..., Label27}
    For i As Integer = 0 To 26            
        If (i Mod 9) = 0 Then                
            Labels(i).Text = "Hello"
        Else                
            Labels(i).Text = "Hi"
        End If        
    Next
    how can i modify their location? If I count the location, it seems quite trouble
    And...Actually, I have 90 labels 27 is simple to say

  10. #10
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2005] for loop problem

    Quote Originally Posted by newpat
    the for loop will add i from 1 to 27, then so
    i/9=0:
    label1's text= "HI"
    label2's text="HI"
    .
    .
    .
    label9's text="Hello"
    .
    .
    .
    label18's text="Hello"
    .
    .
    .
    label27's text="Hello"
    i/9 = 0 happens only when i = 0 where as i \ 9 = 0 happens when i < 9
    The difference? The / operator is for floating point division and the \ operator is for integer division.

  11. #11
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [2005] for loop problem

    vb Code:
    1. 'This code will not work when lable text is not label
    2.         For Each ctl As Control In Me.Controls
    3.             If TypeOf ctl Is Label Then
    4.                 If (Mid(ctl.Text.ToString, 6)) Mod 9 Then
    5.                     ctl.Text = "Hello"
    6.                 Else
    7.                     ctl.Text = "Hi"
    8.                 End If
    9.             End If
    10.         Next
    11.         'This code work on the more then 27 label also so you can change it in your hand

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Hong Kong
    Posts
    384

    Re: [2005] for loop problem

    Quote Originally Posted by stanav
    i/9 = 0 happens only when i = 0 where as i \ 9 = 0 happens when i < 9
    The difference? The / operator is for floating point division and the \ operator is for integer division.
    I see...After read yr atricle, now I think mod is equal to \

  13. #13
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [2005] for loop problem

    check the post 11

  14. #14
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2005] for loop problem

    Quote Originally Posted by newpat
    I see...After read yr atricle, now I think mod is equal to \
    Nope... Mod is modulus division. That is, it'll return the remainder of an integer division
    For example
    5 \ 9 = 0 where as 5 Mod 9 = 5
    9 \ 9 = 1 where as 9 Mod 9 = 0

  15. #15
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Re: [2005] for loop problem

    I see...After read yr atricle, now I think mod is equal to \
    No, Mod gives you the remainder.
    VB.Net 2008
    .Net Framework 2.0

    "Must you breathe? 'Cause I need heaven..."

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Hong Kong
    Posts
    384

    Re: [2005] for loop problem

    Quote Originally Posted by shakti5385
    vb Code:
    1. 'This code will not work when lable text is not label
    2.         For Each ctl As Control In Me.Controls
    3.             If TypeOf ctl Is Label Then
    4.                 If (Mid(ctl.Text.ToString, 6)) Mod 9 Then
    5.                     ctl.Text = "Hello"
    6.                 Else
    7.                     ctl.Text = "Hi"
    8.                 End If
    9.             End If
    10.         Next
    11.         'This code work on the more then 27 label also so you can change it in your hand
    Ha, I test yr code, but it doesn't work

  17. #17
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [2005] for loop problem

    Quote Originally Posted by newpat
    Ha, I test yr code, but it doesn't work
    What problems you get

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Hong Kong
    Posts
    384

    Re: [2005] for loop problem

    The label still "Label1", "Label2", "Label3"....

  19. #19
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [2005] for loop problem

    Put the code on a button click it is working properly for me.

  20. #20

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Hong Kong
    Posts
    384

    Re: [2005] for loop problem

    Quote Originally Posted by shakti5385
    Put the code on a button click it is working properly for me.
    oh! I put it in Form_Load...I would like it when running the program, the labels text are changed

  21. #21
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [2005] for loop problem

    check the file, working properly for me
    Attached Files Attached Files

  22. #22

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Hong Kong
    Posts
    384

    Re: [2005] for loop problem

    ya, yr program is my expectation!
    Is that my amount of labels is the problem?
    I have 90 of labels

  23. #23

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Hong Kong
    Posts
    384

    Re: [2005] for loop problem

    What is the function of this code?
    (Mid(ctl.Text.ToString, 6)) Mod 9

  24. #24
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Re: [2005] for loop problem

    If you set a breakpoint on that line, you could then copy it, go to the Command Window (this is all in the debugger), type "?" and then paste it, hit enter, and it would display what that code evaluates to at that moment.

    You could paste any part of it that you want, for instance:
    "?(Mid(ctl.Text.ToString, 6)) Mod 9"
    or
    "?Mid(ctl.Text.ToString, 6)"

    If you learn how to use breakpoints and other features, you can solve many of your problems.
    VB.Net 2008
    .Net Framework 2.0

    "Must you breathe? 'Cause I need heaven..."

  25. #25

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Hong Kong
    Posts
    384

    Re: [2005] for loop problem

    how can I make a break point?
    re shakti5385:
    I know what is the problem...because I put the labels in the groupbox, so vb may not detect it.
    Last edited by newpat; May 1st, 2007 at 10:10 AM.

  26. #26
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Re: [2005] for loop problem

    You click to the left of the code. I have attached a pic. Now, when the program reaches that line of code, execution will come to a halt. Then, you can look at values of variables and such.

    Use F11 to step through the code one line at a time, and F5 to resume execution. You can set as many breakpoints as you would like.
    Last edited by 18experience; Mar 23rd, 2010 at 09:37 AM.
    VB.Net 2008
    .Net Framework 2.0

    "Must you breathe? 'Cause I need heaven..."

  27. #27
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Re: [2005] for loop problem

    This is a picture of the Command Window, commonly found in the bottom right corner of the IDE. You just type "?" and then whatever else afterwards and it will attempt to evaluate it.

    Like:
    "?5 + 3" will return 8
    "?Me.Text" will return the title of your app
    and so on and so on...

    You can also use the Command Window when your app crashes in debug mode. It usually shows you the line that it errored on, and you can look at what could have done that (possibly an out of bounds exception on an array, thats fairly common).
    Last edited by 18experience; Mar 23rd, 2010 at 09:37 AM.
    VB.Net 2008
    .Net Framework 2.0

    "Must you breathe? 'Cause I need heaven..."

  28. #28

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Hong Kong
    Posts
    384

    Re: [2005] for loop problem

    thx for 18experience!

    now I meet a new troble by shakti5385's code



    Code:
    Dim myArr As String() = {"◕", "▲", "●", "◆", "★", "☆", "*", "♀", "↗", "→"}
    
            Dim i As Integer = 0
            Dim j As Integer = 9
            Dim myRnd As New Random
            Dim myVal As Integer = myRnd.Next(0, 9)
    
            For Each ctl As Control In Me.Controls
                Dim myVal1 As Integer = myRnd.Next(0, 9)
                Console.WriteLine("value1: " & myVal)
                Console.WriteLine("value: " & myVal1)
                If TypeOf ctl Is Label Then
                    If (Mid(ctl.Text.ToString, 6)) Mod 9 Then
                        i += 1
                        ctl.Text = i & ": " & myArr(myVal1)
                    Else
                        ctl.Text = j & ": " & myArr(myVal)
                        j += 9
                        i += 1
                    End If
                End If

  29. #29
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [2005] for loop problem

    You need to trace the code then you will get the solution, I think this problem is occurring due to the value of the I and J because , if IF part you are putting the value I on the label and In Else part you are putting the value of J on the Label

  30. #30

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Hong Kong
    Posts
    384

    Re: [2005] for loop problem

    Code:
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim myArr As String() = {"◕", "▲", "●", "◆", "★", "☆", "*", "♀", "↗", "→"}
    
            Dim i As Integer = 0
            Dim myRnd As New Random
            Dim myVal As Integer = myRnd.Next(0, 9)
    
            For Each ctl As Control In Me.Controls
                Dim myVal1 As Integer = myRnd.Next(0, 9)
                Console.WriteLine("value1: " & myVal)
                Console.WriteLine("value: " & myVal1)
                If TypeOf ctl Is Label Then
                    If (Mid(ctl.Text.ToString, 6)) Mod 9 Then
                        i += 1
                        ctl.Text = i & ": " & myArr(myVal1)
                    Else
                        ctl.Text = i & ": " & myArr(myVal)
                        i += 1
                    End If
                End If
            Next
        End Sub
    If i dun use i and j, the num will not be repeated , but it is really confusing

  31. #31
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [2005] for loop problem

    vb Code:
    1. If (Mid(ctl.Text.ToString, 6)) Mod 9 Then
    2.             ctl.Text = Mid(ctl.Text.ToString, 6) & ": " & myArr(myVal1)
    3.         Else
    4.             ctl.Text = Mid(ctl.Text.ToString, 6) & ": " & myArr(myVal)
    5.       End If

    Use above code

  32. #32
    New Member
    Join Date
    Apr 2007
    Posts
    4

    Re: [2005] for loop problem

    You are doing the old divide by 9 trick (think of a number, add the digits, subract it from the original).
    What you need to do is re-sort the string order at the very beginning. Then loop through and take the mod 9. of all the numbers. If the remainder is 0 use the first char, 1 use the second char, etc...
    I haven't looked at your code, but knowing what you want to do and why will help you create the program. Break it down into the steps and have fun. This is a interesting project to do.

  33. #33

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Hong Kong
    Posts
    384

    Re: [2005] for loop problem

    Quote Originally Posted by dwc_work
    You are doing the old divide by 9 trick (think of a number, add the digits, subract it from the original).
    What you need to do is re-sort the string order at the very beginning. Then loop through and take the mod 9. of all the numbers. If the remainder is 0 use the first char, 1 use the second char, etc...
    I haven't looked at your code, but knowing what you want to do and why will help you create the program. Break it down into the steps and have fun. This is a interesting project to do.
    U know what I am doing?
    That's a really interesting program, when I finish it, I will post it to share with yours

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width