sentinel controlled loop, string comparison


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: sentinel controlled loop, string comparison

  1. #1
    David Guest

    sentinel controlled loop, string comparison


    As part of a project I would like to be able to control when inputting names
    is completed by using a
    sentinel controlled loop. The control for the loop is to continue entering
    names until a "-1" is encountered.

    First time through the loop

    Private Sub cmdName_Click()
    Dim LName(20) As String, FName(20) As String
    Dim Number As Integer
    Number = 0
    ' Use sentinel-controlled program to control entering names
    lstStudents.Clear
    lstStudents.AddItem "Last Name" & vbTab & "First Name"
    LName(Number) = InputBox("Enter Last Name; -1 to end", "VBHTP")
    FName(Number) = InputBox("Enter First Name;", "VBHTP")
    Do Until LName(Number) = -1
    Number = Number + 1
    LName(Number) = InputBox("Enter Last Name; -1 to end", "VBHTP")
    FName(Number) = InputBox("Enter First Name;", "VBHTP")
    Loop
    End Sub

    At the Statement with the comparison to -1, I get a type mismatch error on
    execution.

    Does VB allow a comparion of a string variable (in an array) to an integer
    or does either the string variable
    or integer have to be changed to make the comparison?

    any help or comments or suggestions would be appreciated. Also, this method
    seems cumbersome,
    any suggestions to something a little more elegant

    Thanks ahead of time.

    David

  2. #2
    Jim Edgar Guest

    Re: sentinel controlled loop, string comparison

    David --

    Instead of
    > Do Until LName(Number) = -1


    try

    Do Until LName(Number) = "-1"

    Hope it helps

    Jim Edgar




  3. #3
    Rick Rothstein Guest

    Re: sentinel controlled loop, string comparison

    Consider these changes to your code:

    Private Sub cmdName_Click()
    Dim LName(20) As String, FName(20) As String
    Dim Number As Integer
    Number = -1
    lstStudents.Clear
    lstStudents.AddItem "Last Name" & vbTab & "First Name"
    Do
    Number = Number + 1
    LName(Number) = InputBox("Enter Last Name; -1 to end", "VBHTP")
    If LName(Number) <> "-1" Then
    FName(Number) = InputBox("Enter First Name;", "VBHTP")
    End If
    Loop While LName(Number) <> "-1"
    End Sub

    See if you can figure out what I did and why I did it. Also, you might want
    to consider a test statement to make sure your user doesn't try and enter a
    22nd (element #21) line. You might also want to consider adding the items to
    the ListBox as they are typed in so the user will know that his entry wasn't
    ignored.

    Rick


    "David" <shefchem@swbell.net> wrote in message
    news:38f2586c$1@news.devx.com...
    >
    > As part of a project I would like to be able to control when inputting

    names
    > is completed by using a
    > sentinel controlled loop. The control for the loop is to continue

    entering
    > names until a "-1" is encountered.
    >
    > First time through the loop
    >
    > Private Sub cmdName_Click()
    > Dim LName(20) As String, FName(20) As String
    > Dim Number As Integer
    > Number = 0
    > ' Use sentinel-controlled program to control entering names
    > lstStudents.Clear
    > lstStudents.AddItem "Last Name" & vbTab & "First Name"
    > LName(Number) = InputBox("Enter Last Name; -1 to end", "VBHTP")
    > FName(Number) = InputBox("Enter First Name;", "VBHTP")
    > Do Until LName(Number) = -1
    > Number = Number + 1
    > LName(Number) = InputBox("Enter Last Name; -1 to end", "VBHTP")
    > FName(Number) = InputBox("Enter First Name;", "VBHTP")
    > Loop
    > End Sub
    >
    > At the Statement with the comparison to -1, I get a type mismatch error on
    > execution.
    >
    > Does VB allow a comparion of a string variable (in an array) to an integer
    > or does either the string variable
    > or integer have to be changed to make the comparison?
    >
    > any help or comments or suggestions would be appreciated. Also, this

    method
    > seems cumbersome,
    > any suggestions to something a little more elegant
    >
    > Thanks ahead of time.
    >
    > David




  4. #4
    Bill McCarthy Guest

    Re: sentinel controlled loop, string comparison

    David,

    Rick's code should work, but I can't help wondering if something other
    than -1 is more suitable to end the loop. Say if you changed the title to
    "Enter in New Last Name" or similar, you could detect if the hit "Esc" or
    Cancel by checking the strptr of the return string.
    eg:
    If strPtr(FName(Number)) Then 'did not press cancel
    If Len(FName(Number)) Then 'they typed something





    "Rick Rothstein" <rick_newsgroup@email.com> wrote in message
    news:38f2b85f$1@news.devx.com...
    > Consider these changes to your code:
    >
    > Private Sub cmdName_Click()
    > Dim LName(20) As String, FName(20) As String
    > Dim Number As Integer
    > Number = -1
    > lstStudents.Clear
    > lstStudents.AddItem "Last Name" & vbTab & "First Name"
    > Do
    > Number = Number + 1
    > LName(Number) = InputBox("Enter Last Name; -1 to end", "VBHTP")
    > If LName(Number) <> "-1" Then
    > FName(Number) = InputBox("Enter First Name;", "VBHTP")
    > End If
    > Loop While LName(Number) <> "-1"
    > End Sub
    >
    > See if you can figure out what I did and why I did it. Also, you might

    want
    > to consider a test statement to make sure your user doesn't try and enter

    a
    > 22nd (element #21) line. You might also want to consider adding the items

    to
    > the ListBox as they are typed in so the user will know that his entry

    wasn't
    > ignored.
    >
    > Rick
    >
    >
    > "David" <shefchem@swbell.net> wrote in message
    > news:38f2586c$1@news.devx.com...
    > >
    > > As part of a project I would like to be able to control when inputting

    > names
    > > is completed by using a
    > > sentinel controlled loop. The control for the loop is to continue

    > entering
    > > names until a "-1" is encountered.
    > >
    > > First time through the loop
    > >
    > > Private Sub cmdName_Click()
    > > Dim LName(20) As String, FName(20) As String
    > > Dim Number As Integer
    > > Number = 0
    > > ' Use sentinel-controlled program to control entering names
    > > lstStudents.Clear
    > > lstStudents.AddItem "Last Name" & vbTab & "First Name"
    > > LName(Number) = InputBox("Enter Last Name; -1 to end",

    "VBHTP")
    > > FName(Number) = InputBox("Enter First Name;", "VBHTP")
    > > Do Until LName(Number) = -1
    > > Number = Number + 1
    > > LName(Number) = InputBox("Enter Last Name; -1 to end",

    "VBHTP")
    > > FName(Number) = InputBox("Enter First Name;", "VBHTP")
    > > Loop
    > > End Sub
    > >
    > > At the Statement with the comparison to -1, I get a type mismatch error

    on
    > > execution.
    > >
    > > Does VB allow a comparion of a string variable (in an array) to an

    integer
    > > or does either the string variable
    > > or integer have to be changed to make the comparison?
    > >
    > > any help or comments or suggestions would be appreciated. Also, this

    > method
    > > seems cumbersome,
    > > any suggestions to something a little more elegant
    > >
    > > Thanks ahead of time.
    > >
    > > David

    >
    >




  5. #5
    Rick Rothstein Guest

    Re: sentinel controlled loop, string comparison

    I wasn't sure how far to take my answer. Personally, I don't like popping
    InputBox's in my user's face, one after the other, like that. Because this
    is a "getting started" newsgroup, I figured David will "grow" through the
    various GUI presentation stages that we all(?) did. Hence, I thought I would
    restrict my post to an improvement, code-wise, for David to consider (within
    the GUI he had created).

    Your suggestion is, of course, valid. I hope the undocumented "strPtr"
    function doesn't throw him though. In addition, we both forgot to suggest to
    David that he should consider what he wants to do if his user simply hits
    the Enter key without typing in any text at all. For the interface method
    David is using, he could probably omit the strPtr function that you
    suggested altogether and simply end the loop when a (Trim'ed) blank entry is
    inputted.

    Rick


    "Bill McCarthy" <billmcc@gsat.edu.au> wrote in message
    news:38f2f7d1@news.devx.com...
    > David,
    >
    > Rick's code should work, but I can't help wondering if something other
    > than -1 is more suitable to end the loop. Say if you changed the title to
    > "Enter in New Last Name" or similar, you could detect if the hit "Esc" or
    > Cancel by checking the strptr of the return string.
    > eg:
    > If strPtr(FName(Number)) Then 'did not press cancel
    > If Len(FName(Number)) Then 'they typed something
    >
    >
    >
    >
    >
    > "Rick Rothstein" <rick_newsgroup@email.com> wrote in message
    > news:38f2b85f$1@news.devx.com...
    > > Consider these changes to your code:
    > >
    > > Private Sub cmdName_Click()
    > > Dim LName(20) As String, FName(20) As String
    > > Dim Number As Integer
    > > Number = -1
    > > lstStudents.Clear
    > > lstStudents.AddItem "Last Name" & vbTab & "First Name"
    > > Do
    > > Number = Number + 1
    > > LName(Number) = InputBox("Enter Last Name; -1 to end", "VBHTP")
    > > If LName(Number) <> "-1" Then
    > > FName(Number) = InputBox("Enter First Name;", "VBHTP")
    > > End If
    > > Loop While LName(Number) <> "-1"
    > > End Sub
    > >
    > > See if you can figure out what I did and why I did it. Also, you might

    > want
    > > to consider a test statement to make sure your user doesn't try and

    enter
    > a
    > > 22nd (element #21) line. You might also want to consider adding the

    items
    > to
    > > the ListBox as they are typed in so the user will know that his entry

    > wasn't
    > > ignored.
    > >
    > > Rick
    > >
    > >
    > > "David" <shefchem@swbell.net> wrote in message
    > > news:38f2586c$1@news.devx.com...
    > > >
    > > > As part of a project I would like to be able to control when inputting

    > > names
    > > > is completed by using a
    > > > sentinel controlled loop. The control for the loop is to continue

    > > entering
    > > > names until a "-1" is encountered.
    > > >
    > > > First time through the loop
    > > >
    > > > Private Sub cmdName_Click()
    > > > Dim LName(20) As String, FName(20) As String
    > > > Dim Number As Integer
    > > > Number = 0
    > > > ' Use sentinel-controlled program to control entering names
    > > > lstStudents.Clear
    > > > lstStudents.AddItem "Last Name" & vbTab & "First Name"
    > > > LName(Number) = InputBox("Enter Last Name; -1 to end",

    > "VBHTP")
    > > > FName(Number) = InputBox("Enter First Name;", "VBHTP")
    > > > Do Until LName(Number) = -1
    > > > Number = Number + 1
    > > > LName(Number) = InputBox("Enter Last Name; -1 to end",

    > "VBHTP")
    > > > FName(Number) = InputBox("Enter First Name;", "VBHTP")
    > > > Loop
    > > > End Sub
    > > >
    > > > At the Statement with the comparison to -1, I get a type mismatch

    error
    > on
    > > > execution.
    > > >
    > > > Does VB allow a comparion of a string variable (in an array) to an

    > integer
    > > > or does either the string variable
    > > > or integer have to be changed to make the comparison?
    > > >
    > > > any help or comments or suggestions would be appreciated. Also, this

    > > method
    > > > seems cumbersome,
    > > > any suggestions to something a little more elegant
    > > >
    > > > Thanks ahead of time.
    > > >
    > > > David

    > >
    > >

    >
    >




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center