Help?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jjeakle
    New Member
    • Jan 2007
    • 1

    #1

    Help?

    First off - I'm new at this so I'm sorry up front if I ask - dumb Q's

    I'm trying to learn VB 6
    Trying to put together a hangman game.
    I'm working with a textbox, command button. and some labels as well as a few variables.

    Private Sub Form_Load()
    Dim Correct As Boolean
    Dim x% 'use as counter
    Dim SecretWord(8, 2)
    'load secret word as an array
    SecretWord(1, 1) = "L"
    SecretWord(2, 1) = "E"
    SecretWord(3, 1) = "A"
    SecretWord(4, 1) = "R"
    SecretWord(5, 1) = "N"
    SecretWord(6, 1) = "K"
    SecretWord(7, 1) = "E"
    SecretWord(8, 1) = "Y"


    For x% = 1 To 8
    SecretWord(x, 2) = "*"
    Label1.Caption = Label1.Caption & SecretWord(x, 2)
    Next
    End Sub

    Private Sub Command1_Click( )
    Dim x%
    Static Guesses As Integer
    Dim SecretWord(8, 2)
    Dim Correct As Boolean
    Correct = False

    Text1.SetFocus
    Text1.SelLength = Len(Text1.Text)

    For x = 1 To 8
    If Text1.Text = SecretWord(x, 1) Then
    SecretWord(x, 2) = SecretWord(x, 1)
    Correct = True
    End If
    Next
    If Correct = False Then
    Guesses = Guesses + 1
    Label2.Caption = Guesses
    End If
    End Sub
    I can't get The correct guesses in the TextBox to appear in the (x,2) of my array
    and I can't set focus to my Text1.Text
    I'm trying to use a multdemensional array
    Thanks, Jim
  • willakawill
    Top Contributor
    • Oct 2006
    • 1646

    #2
    Originally posted by jjeakle
    First off - I'm new at this so I'm sorry up front if I ask - dumb Q's

    I'm trying to learn VB 6
    Trying to put together a hangman game.
    I'm working with a textbox, command button. and some labels as well as a few variables.

    Private Sub Form_Load()
    Dim Correct As Boolean
    Dim x% 'use as counter
    Dim SecretWord(8, 2)
    'load secret word as an array
    SecretWord(1, 1) = "L"
    SecretWord(2, 1) = "E"
    SecretWord(3, 1) = "A"
    SecretWord(4, 1) = "R"
    SecretWord(5, 1) = "N"
    SecretWord(6, 1) = "K"
    SecretWord(7, 1) = "E"
    SecretWord(8, 1) = "Y"


    For x% = 1 To 8
    SecretWord(x, 2) = "*"
    Label1.Caption = Label1.Caption & SecretWord(x, 2)
    Next
    End Sub

    Private Sub Command1_Click( )
    Dim x%
    Static Guesses As Integer
    Dim SecretWord(8, 2)
    Dim Correct As Boolean
    Correct = False

    Text1.SetFocus
    Text1.SelLength = Len(Text1.Text)

    For x = 1 To 8
    If Text1.Text = SecretWord(x, 1) Then
    SecretWord(x, 2) = SecretWord(x, 1)
    Correct = True
    End If
    Next
    If Correct = False Then
    Guesses = Guesses + 1
    Label2.Caption = Guesses
    End If
    End Sub
    I can't get The correct guesses in the TextBox to appear in the (x,2) of my array
    and I can't set focus to my Text1.Text
    I'm trying to use a multdemensional array
    Thanks, Jim
    Hi Jim
    First of all the secretword array in form load only has scope in that sub. Once you reach the end of the sub it disappears. If you are going to use it throughout the game you will need to declare it outside a sub at the top of your code like this:

    Code:
    Public SecretWord(7)(1) As String
    In vb declarations of arrays the number represents the upper bound or the highest index number that you can use. The first index is 0 so putting 7 gives you 8 elements. you can now reference this array anywhere in your code.

    Good luck

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Just a couple of things to keep in mind.
      • I think willakawill has got the VB syntax mixed up with C or Java. :) If the (7)(1) syntax won't compile try using a comma, as you've done elsewhere.
      • You can't create a public variable (or array) like this in a form. If you want it available to everything in the project, use the Public declaration but put it in a module. If you just want it available within the form, declare it at the top of the form and use Private rather than Public.
      • As for the "0/1 to 7/8" thing, be aware that this is largely a matter of personal preference. It defaults to 0 as the base, but if you prefer you can use the Option Base statement to say that arrays should begin at 1. Personally, I prefer to specify both upper and lower bounds. For example Public SecretWord(1 To 8, 1 To 2) As String

      Comment

      Working...