Results 1 to 3 of 3

Thread: vb

  1. #1
    SG Stud ColdFusion's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, BC
    Posts
    3,542

    vb

    hey guys,

    looking for a command that has properties to halt the script in vb. For example:

    Code:
        If cmd2.Caption = blank Then
            If (gblturn = "Player1") Then
            cmd2.Caption = "X"
            txt_status.Text = "Player2 it is now your turn"
            gblturn = "Player2"
            HALT
            End If
            If (gblturn = "Player2") Then
            cmd2.Caption = "O"
            gblturn = "Player1"
            HALT
            End If
         Else
            txt_status.Text = "Error! The box you have selected is already taken"
        End If
    End Sub
    Where 'halt' is a need a command to stop the script right there and not procede any further.

    Hope that makes sense!
    -matt

  2. #2
    Greenish
    Guest
    can you use
    Code:
    exit(1);
    
    or maybe
    
    break;
    not sure about vb script, but those work for most other languages.

  3. #3
    *giggle* vinnie's Avatar
    Join Date
    Dec 2002
    Location
    Sydney, Oz
    Posts
    1,620
    Quote Originally Posted by ColdFusion
    hey guys,

    looking for a command that has properties to halt the script in vb. For example:

    Code:
        If cmd2.Caption = blank Then
            If (gblturn = "Player1") Then
            cmd2.Caption = "X"
            txt_status.Text = "Player2 it is now your turn"
            gblturn = "Player2"
            HALT
            End If
            If (gblturn = "Player2") Then
            cmd2.Caption = "O"
            gblturn = "Player1"
            HALT
            End If
         Else
            txt_status.Text = "Error! The box you have selected is already taken"
        End If
    End Sub
    Where 'halt' is a need a command to stop the script right there and not procede any further.

    Hope that makes sense!
    -matt
    *shudder*

    Doing noughts and crosses the *very* long way are we?

    Code:
    Private Sub cmd2_Click()
        
        With cmd2
        
          If .Caption = "-" Then
            Select Case gblturn
            
                Case "player1"
                    .Caption = "X"
                    gblturn = "player2"
                    txt_status.Text = "Player2 it is now your turn"
    
                Case "player2"
                    .Caption = "O"
                    gblturn = "player1"
                    txt_status.Text = "Player1 it is now your turn"
    
            End Select
    
          Else
    
            txt_status.Text = "Error! The box you have selected is already taken"
    
          End If
    
        End With
             
    End Sub
    That's what I would do personally.

    The first thing is using a 'with'. The with just means that in a 'with... end with' block you can refer to the 'with' control by starting a statement with a period. It saves typing it out again and again plus improves readability as you know that section is about one particular control.

    The next thing is that instead of fiddling with lots of ifs, we have one if then a select statement. The select just avoids nesting ifs here, but it improves readability and is a useful construct in other similar situations.

    Now... why would you need to break anyhow? It's not like switch statements in C/C++ where it'll cascade down the options if you don't break, it's VB, it wants to hold your hand and play games in the park with you. Any speed savings on not doing your extra if comparison are lost by the very nature of using VB anyhow, if that kind of speed is an issue then VB is not the language of choice

    Anyhow... if you are doing noughts and crosses, this isn't the most effecient way to be doing it. Look into control arrays. It's overly complicated and cumbersome to do it the way it looks like you're trying- control arrays are extremely simple to make and operate.

    Make a control, hit copy, hit paste, click yes and paste as many as you need. They are reffered to in their event procedure by their index, ie the following works awesomely well with a control array of 9 buttons

    Code:
    Private Sub Command1_Click(Index As Integer)
    
    With Command1(Index)
    If .Caption = "-" Then
            Select Case gblturn
            
                Case "player1"
                    .Caption = "X"
                    gblturn = "player2"
                    txt_status.Text = "Player2 it is now your turn"
    
                Case "player2"
                    .Caption = "O"
                    gblturn = "player1"
                    txt_status.Text = "Player1 it is now your turn"
            
            End Select
          Else
            txt_status.Text = "Error! The box you have selected is already taken"
          End If
    
        End With
    
    
    End Sub

    Wasn't that simple? Beats the dang out of 9 procedures doesn't it?

    All you need is slip in the call to your win/draw condition procedure in the right places and you're done
    Australian Regular Army and proud of it.

Similar Threads

  1. VB Gurus
    By deleted_acct in forum General Discussion Board
    Replies: 0
    Last Post: 04-23-04, 08:46 PM
  2. Ultimate project in VB 6.0
    By Y-R'D in forum Programming Forum
    Replies: 0
    Last Post: 04-10-04, 11:15 PM
  3. VB Data Environment
    By parse27 in forum Programming Forum
    Replies: 5
    Last Post: 11-15-02, 02:28 PM
  4. vB code other than vB?
    By Qwijib0 in forum General Discussion Board
    Replies: 1
    Last Post: 05-07-02, 12:43 PM
  5. Get Ready, vB is comin ;)
    By Brent in forum General Discussion Board
    Replies: 25
    Last Post: 05-21-01, 11:48 PM

Bookmarks

Posting Permissions

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