Results 1 to 6 of 6

Thread: If one does not = ten then one = +1

  1. #1

    Thread Starter
    Addicted Member Reapism's Avatar
    Join Date
    Sep 2012
    Posts
    170

    Angry If one does not = ten then one = +1

    As introduced above, I am trying to recreate a similar concept where its like a loop. I am looking for a very compact way for basis to add one if not equal to countTo.
    You may include a loop and other things. Our goal is to make it very compact.

    As shown, I tried to use Lines and GoTo's to go back like a loop but it doesnt seem to work.

    Code:
    Public Class Form1
        Dim basis As Double = 0
        Dim countTo As Double = 100
    
        Private Sub btnStart_Click(sender As System.Object, e As System.EventArgs) Handles btnStart.Click
    Start:
            basis = +1
            lblCount.Text = "Count: " & basis.ToString
            If basis < countTo Then GoTo Start Else GoTo Finish
    Finish: MsgBox("done")
    
        End Sub
    End Class

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: If one does not = ten then one = +1

    Instead of using GOTOs, I would recommend using proper loops. See this link

    Topic: Loop Structures (Visual Basic)
    Link: http://msdn.microsoft.com/en-us/library/ezk76t25.aspx

    I can give you the code but I would like that you give it a try and if you are stuck then post the code you tried and we will take it from there?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: If one does not = ten then one = +1

    Hi,

    (Just saw koolsid's post, which I agree with, but I will add this extra bit anyway)

    To start with, the reason why your code does not work is because you are assigning the value of 1 to your "basis" variable every time your code executes within that loop. The proper shortcut statement to add 1 to the EXISTING value of a variable is:-

    Code:
    basis +=1
    Presumably you are wanting to do some work within a loop while the value of "basis" is less than your "countTo" variable. If so, then either use a FOR Loop or a WHILE loop and get rid of the antiquated GoTo statements.

    Hope that helps.

    Cheers,

    Ian

  4. #4

    Thread Starter
    Addicted Member Reapism's Avatar
    Join Date
    Sep 2012
    Posts
    170

    Re: If one does not = ten then one = +1

    Quote Originally Posted by koolsid View Post
    Instead of using GOTOs, I would recommend using proper loops. See this link

    Topic: Loop Structures (Visual Basic)
    Link: http://msdn.microsoft.com/en-us/library/ezk76t25.aspx

    I can give you the code but I would like that you give it a try and if you are stuck then post the code you tried and we will take it from there?
    Yes, I would like the code, I apologize, I am just having a so called "brain-fart" today. It is such a simple task yet I cannot do it. However I did get it to work but it freezes my PC. So I would like to see the code you came up with.

  5. #5

    Thread Starter
    Addicted Member Reapism's Avatar
    Join Date
    Sep 2012
    Posts
    170

    Re: If one does not = ten then one = +1

    Okay I got it but when countTo is over 10000, it freezes the application. I dont know why but I have a pretty good computer, "processor i7 3820 @ 3.6GHz"

    Code:
            For counter As Double = basis To countTo Step 1
                basis += 1
                lblCount.Text = "Count: " & basis
            Next counter

  6. #6
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: If one does not = ten then one = +1

    Hi,

    A couple of points here for you:-

    1) Your use of the For Loop is slightly incorrect in that you should use the For Loop to do the counting of a variable for you rather than using another variable to count the number of iterations the Loop has completed. In addition to this you only need to add the Step keyword if you need to deviate from the default step increment of 1. Therefore your For Loop could be better by saying:-

    Code:
    For basis As Integer = 1 To countTo
      lblCount.Text = "Count: " & basis
    Next basis
    2) I cannot imagine that a simple routine such as this would cause your computer to freeze but what I think you may possibly mean is that the Label does not update to show each count of the "basis" variable while this code is running?

    If so, then you need to know that windows messages are suspended while code in the likes of a button is being executed and therefore calls to repaint controls, for instance, are not captured until the code within that button has completed its execution. You can get round this by forcing a call to repaint a control by calling its Refresh method. i.e:-

    Code:
    lblCount.Refresh()
    Hope that helps.

    Cheers,

    Ian

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