Simple Arrays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Roy Riddex

    Simple Arrays

    2nd post of the day! I'm just learning about Arrays at College and have met
    a problem. I have 5 text boxes for number input, a command button to add the
    numbers to the array, and a command button which displays the array contents
    in 5 labels. My program works fine. The problem is that I use a simple If
    statement to check for any empty boxes. I've to cut the IF statement and use
    a For Each...Next statement to check for empty text boxes but I'm struggling
    to apply this method. The book also tells me to declare a variable 'Dim
    MyTextBox As TextBox', I've never came across this variable type before.
    I'll post my working version of the program and hopefully someone can help
    me out.

    Option Explicit

    Dim Numbers(1 To 5) As Integer

    Private Sub cmdAddToArray_C lick()
    Dim Index As Integer
    If (txtNumbers(1). Text = "") Or (txtNumbers(2). Text = "") Or
    (txtNumbers(3). Text = "") Or (txtNumbers(4). Text = "") Or
    (txtNumbers(5). Text = "") Then
    MsgBox "You have not entered 5 numbers"
    Else
    For Index = 1 To 5
    Numbers(Index) = txtNumbers(Inde x).Text
    Next Index
    End If
    End Sub

    Private Sub cmdDisplayArray _Click()
    Dim Index As Integer
    For Index = 1 To 5
    lblNumbers(Inde x).Caption = Numbers(Index)
    Next Index
    End Sub


  • Randy Birch

    #2
    Re: Simple Arrays

    off the top of my head ...

    dim ctl as textbox

    for each ctl in me

    if typeof ctl is textbox then

    if len(ctl.text) = 0 then
    msgbox "what part of 'enter 5 numbers' don't you understand?"
    ctl.setfocus
    exit for
    end if
    end if
    next ctl

    --

    Randy Birch
    MVP Visual Basic

    Please respond only to the newsgroups so all can benefit.


    "Roy Riddex" <roy_riddexNOSP AM@blueyonder.c o.uk> wrote in message
    news:f68Cb.2994 7$kh4.13863@new s-binary.blueyond er.co.uk...
    : 2nd post of the day! I'm just learning about Arrays at College and have
    met
    : a problem. I have 5 text boxes for number input, a command button to add
    the
    : numbers to the array, and a command button which displays the array
    contents
    : in 5 labels. My program works fine. The problem is that I use a simple If
    : statement to check for any empty boxes. I've to cut the IF statement and
    use
    : a For Each...Next statement to check for empty text boxes but I'm
    struggling
    : to apply this method. The book also tells me to declare a variable 'Dim
    : MyTextBox As TextBox', I've never came across this variable type before.
    : I'll post my working version of the program and hopefully someone can help
    : me out.
    :
    : Option Explicit
    :
    : Dim Numbers(1 To 5) As Integer
    :
    : Private Sub cmdAddToArray_C lick()
    : Dim Index As Integer
    : If (txtNumbers(1). Text = "") Or (txtNumbers(2). Text = "") Or
    : (txtNumbers(3). Text = "") Or (txtNumbers(4). Text = "") Or
    : (txtNumbers(5). Text = "") Then
    : MsgBox "You have not entered 5 numbers"
    : Else
    : For Index = 1 To 5
    : Numbers(Index) = txtNumbers(Inde x).Text
    : Next Index
    : End If
    : End Sub
    :
    : Private Sub cmdDisplayArray _Click()
    : Dim Index As Integer
    : For Index = 1 To 5
    : lblNumbers(Inde x).Caption = Numbers(Index)
    : Next Index
    : End Sub
    :
    :


    Comment

    • Mauro

      #3
      Re: Simple Arrays

      Of course, since ctl was declared as TextBox, no other object really should
      be enumerated in the loop, so the line 'if typeof ctl is textbox then' could
      really be dropped. However, if you were enumerating all objects and
      declared ctl as object, then it would be necessary.


      Mauro


      "Randy Birch" <rgb_removethis @mvps.org> wrote in message
      news:qv9Cb.3030 7$VEd1.7852@new s02.bloor.is.ne t.cable.rogers. com...[color=blue]
      > off the top of my head ...
      >
      > dim ctl as textbox
      >
      > for each ctl in me
      >
      > if typeof ctl is textbox then
      >
      > if len(ctl.text) = 0 then
      > msgbox "what part of 'enter 5 numbers' don't you understand?"
      > ctl.setfocus
      > exit for
      > end if
      > end if
      > next ctl
      >
      > --
      >
      > Randy Birch
      > MVP Visual Basic
      > http://www.mvps.org/vbnet/
      > Please respond only to the newsgroups so all can benefit.
      >
      >
      > "Roy Riddex" <roy_riddexNOSP AM@blueyonder.c o.uk> wrote in message
      > news:f68Cb.2994 7$kh4.13863@new s-binary.blueyond er.co.uk...
      > : 2nd post of the day! I'm just learning about Arrays at College and have
      > met
      > : a problem. I have 5 text boxes for number input, a command button to add
      > the
      > : numbers to the array, and a command button which displays the array
      > contents
      > : in 5 labels. My program works fine. The problem is that I use a simple[/color]
      If[color=blue]
      > : statement to check for any empty boxes. I've to cut the IF statement and
      > use
      > : a For Each...Next statement to check for empty text boxes but I'm
      > struggling
      > : to apply this method. The book also tells me to declare a variable 'Dim
      > : MyTextBox As TextBox', I've never came across this variable type before.
      > : I'll post my working version of the program and hopefully someone can[/color]
      help[color=blue]
      > : me out.
      > :
      > : Option Explicit
      > :
      > : Dim Numbers(1 To 5) As Integer
      > :
      > : Private Sub cmdAddToArray_C lick()
      > : Dim Index As Integer
      > : If (txtNumbers(1). Text = "") Or (txtNumbers(2). Text = "") Or
      > : (txtNumbers(3). Text = "") Or (txtNumbers(4). Text = "") Or
      > : (txtNumbers(5). Text = "") Then
      > : MsgBox "You have not entered 5 numbers"
      > : Else
      > : For Index = 1 To 5
      > : Numbers(Index) = txtNumbers(Inde x).Text
      > : Next Index
      > : End If
      > : End Sub
      > :
      > : Private Sub cmdDisplayArray _Click()
      > : Dim Index As Integer
      > : For Index = 1 To 5
      > : lblNumbers(Inde x).Caption = Numbers(Index)
      > : Next Index
      > : End Sub
      > :
      > :
      >
      >[/color]


      Comment

      • Bob Butler

        #4
        Re: Simple Arrays

        "Mauro" <mbiefeni@hotma il.com> wrote in message news:<_8rCb.681 270$pl3.655010@ pd7tw3no>...[color=blue]
        > Of course, since ctl was declared as TextBox, no other object really should
        > be enumerated in the loop, so the line 'if typeof ctl is textbox then' could
        > really be dropped. However, if you were enumerating all objects and
        > declared ctl as object, then it would be necessary.[/color]

        try it with at least 1 label on the form... the for each construct
        enumerates all controls regardless of the declared type of the loop
        variable in VB

        The only thing I'd change in Randy's code is explicitly stating teh
        collection name instead of relying on the default:
        For Each ctl In Me.Controls

        Comment

        • Randy Birch

          #5
          Re: Simple Arrays

          yep .. that's why it was air code. <g>

          --

          Randy Birch
          MVP Visual Basic

          Please respond only to the newsgroups so all can benefit.


          "Bob Butler" <butlerbob@eart hlink.net> wrote in message
          news:fa10fb0.03 12130828.306539 b1@posting.goog le.com...
          : "Mauro" <mbiefeni@hotma il.com> wrote in message
          news:<_8rCb.681 270$pl3.655010@ pd7tw3no>...
          : > Of course, since ctl was declared as TextBox, no other object really
          should
          : > be enumerated in the loop, so the line 'if typeof ctl is textbox then'
          could
          : > really be dropped. However, if you were enumerating all objects and
          : > declared ctl as object, then it would be necessary.
          :
          : try it with at least 1 label on the form... the for each construct
          : enumerates all controls regardless of the declared type of the loop
          : variable in VB
          :
          : The only thing I'd change in Randy's code is explicitly stating teh
          : collection name instead of relying on the default:
          : For Each ctl In Me.Controls


          Comment

          • Steve Gerrard

            #6
            Re: Simple Arrays


            "Randy Birch" <rgb_removethis @mvps.org> wrote in message
            news:pmHCb.1065 6$%TO.9694@twis ter01.bloor.is. net.cable.roger s.com...[color=blue]
            > yep .. that's why it was air code. <g>
            >[color=green]
            >> off the top of my head ...
            >>
            >> dim ctl as textbox
            >>
            >> for each ctl in me
            >>[/color][/color]

            If you declare ctl as TextBox, not Control, shouldn't this code break if
            encounters a control that is not a TextBox?


            Comment

            • Rick Rothstein

              #7
              Re: Simple Arrays

              > > yep .. that's why it was air code. <g>[color=blue][color=green]
              > >[color=darkred]
              > >> off the top of my head ...
              > >>
              > >> dim ctl as textbox
              > >>
              > >> for each ctl in me
              > >>[/color][/color]
              >
              > If you declare ctl as TextBox, not Control, shouldn't this code break if
              > encounters a control that is not a TextBox?[/color]

              Correct... for the code Randy posted, ctl needs to be declared as a Control.
              However, the OP showed that he was using a control array of TextBoxes. That
              means, the For Each can be made to enumerate only the control array
              elements. Here is Randy's code modified to do that (txtNumbers was the name
              of the control array the OP posted)...


              Rick - MVP

              Private Sub Command1_Click( )

              Dim ctl As TextBox

              For Each ctl In txtNumbers

              If Len(ctl.Text) = 0 Then
              MsgBox "What part of 'Enter 5 numbers' don't you understand?"
              ctl.SetFocus
              Exit For
              End If

              Next ctl

              End Sub


              Comment

              • Roy Riddex

                #8
                Re: Simple Arrays

                Thanks guys, I'll give it a try.


                Comment

                • Bob Butler

                  #9
                  Re: Simple Arrays

                  "Rick Rothstein" <rickNOSPAMnews @NOSPAMcomcast. net> wrote in message news:<IsednW_rb 4cRdEaiRVn-vg@comcast.com> ...[color=blue][color=green][color=darkred]
                  > > > yep .. that's why it was air code. <g>
                  > > >
                  > > >> off the top of my head ...
                  > > >>
                  > > >> dim ctl as textbox
                  > > >>
                  > > >> for each ctl in me
                  > > >>[/color]
                  > >
                  > > If you declare ctl as TextBox, not Control, shouldn't this code break if
                  > > encounters a control that is not a TextBox?[/color]
                  >
                  > Correct... for the code Randy posted, ctl needs to be declared as a Control.[/color]

                  dang, can't believe I missed that!

                  Comment

                  • Steve Gerrard

                    #10
                    Re: Simple Arrays


                    "Rick Rothstein" <rickNOSPAMnews @NOSPAMcomcast. net> wrote in message
                    news:IsednW_rb4 cRdEaiRVn-vg@comcast.com. ..[color=blue][color=green][color=darkred]
                    > > > yep .. that's why it was air code. <g>[/color][/color]
                    >
                    > Correct... for the code Randy posted, ctl needs to be declared as a[/color]
                    Control.[color=blue]
                    > However, the OP showed that he was using a control array of TextBoxes.[/color]
                    That[color=blue]
                    > means, the For Each can be made to enumerate only the control array
                    > elements. Here is Randy's code modified to do that (txtNumbers was the[/color]
                    name[color=blue]
                    > of the control array the OP posted)...
                    >
                    > Private Sub Command1_Click( )
                    >
                    > Dim ctl As TextBox
                    >
                    > For Each ctl In txtNumbers
                    >
                    > ...[/color]

                    You know, I am pretty sure I would have tried some sort of For n = 0 to
                    UBound(txtNumbe rs), but VB won't recognize a control array as an array
                    in this context. And if I hard code the upper limit, I am still assuming
                    that the indexes are sequential, which does not have to be the case.
                    I would not have thought to try a For Each on a control array, which I
                    like better. Doesn't this all mean that actually what you have is a
                    collection, not an array?


                    Comment

                    • Larry Serflaten

                      #11
                      Re: Simple Arrays

                      "Steve Gerrard" <notstevegerrar d@comcast.net> wrote
                      [color=blue][color=green]
                      > > Dim ctl As TextBox
                      > >
                      > > For Each ctl In txtNumbers[/color]
                      >
                      > You know, I am pretty sure I would have tried some sort of For n = 0 to
                      > UBound(txtNumbe rs), but VB won't recognize a control array as an array
                      > in this context. And if I hard code the upper limit, I am still assuming
                      > that the indexes are sequential, which does not have to be the case.
                      > I would not have thought to try a For Each on a control array, which I
                      > like better. Doesn't this all mean that actually what you have is a
                      > collection, not an array?[/color]


                      No, you have some unknown hybrid type. Intellisense will list LBound
                      and UBound as well as Count and Item. LBound and UBound are usually
                      associated with arrays, but Count and Item are associated with collections.

                      For the example above, type; txtNumbers.
                      (Where the list will show up in the code window after you type the dot.)


                      LFS




                      -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
                      http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
                      -----== Over 100,000 Newsgroups - 19 Different Servers! =-----

                      Comment

                      • Randy Birch

                        #12
                        Re: Simple Arrays

                        sure it will. But since the controls - array or not - are all part of the
                        Controls collection, For Each can be easier.

                        --

                        Randy Birch
                        MVP Visual Basic

                        Please respond only to the newsgroups so all can benefit.


                        "Steve Gerrard" <notstevegerrar d@comcast.net> wrote in message
                        news:EKKdnWUaKq N9K0GiRVn-hQ@comcast.com. ..
                        :
                        : "Rick Rothstein" <rickNOSPAMnews @NOSPAMcomcast. net> wrote in message
                        : news:IsednW_rb4 cRdEaiRVn-vg@comcast.com. ..
                        : > > > yep .. that's why it was air code. <g>
                        : >
                        : > Correct... for the code Randy posted, ctl needs to be declared as a
                        : Control.
                        : > However, the OP showed that he was using a control array of TextBoxes.
                        : That
                        : > means, the For Each can be made to enumerate only the control array
                        : > elements. Here is Randy's code modified to do that (txtNumbers was the
                        : name
                        : > of the control array the OP posted)...
                        : >
                        : > Private Sub Command1_Click( )
                        : >
                        : > Dim ctl As TextBox
                        : >
                        : > For Each ctl In txtNumbers
                        : >
                        : > ...
                        :
                        : You know, I am pretty sure I would have tried some sort of For n = 0 to
                        : UBound(txtNumbe rs), but VB won't recognize a control array as an array
                        : in this context. And if I hard code the upper limit, I am still assuming
                        : that the indexes are sequential, which does not have to be the case.
                        : I would not have thought to try a For Each on a control array, which I
                        : like better. Doesn't this all mean that actually what you have is a
                        : collection, not an array?
                        :
                        :


                        Comment

                        • Steve Gerrard

                          #13
                          Re: Simple Arrays


                          "Larry Serflaten" <Abuse@SpamBust ers.com> wrote in message
                          news:3fdcc276_7 @corp.newsgroup s.com...[color=blue]
                          > "Steve Gerrard" <notstevegerrar d@comcast.net> wrote[color=green]
                          > >Doesn't this all mean that actually what you have is a[/color]
                          > collection, not an array?[/color]
                          [color=blue]
                          > No, you have some unknown hybrid type. Intellisense will list LBound
                          > and UBound as well as Count and Item. LBound and UBound are usually
                          > associated with arrays, but Count and Item are associated with[/color]
                          collections.[color=blue]
                          >
                          > For the example above, type; txtNumbers.
                          > (Where the list will show up in the code window after you type the[/color]
                          dot.)[color=blue]
                          >
                          >[/color]

                          I didn't know that. Interesting that for a control array, UBound is a
                          method - I think this is the only case where that is true. It still
                          seems more like a collection to me now, with some array features tacked
                          on.


                          Comment

                          • Roy Riddex

                            #14
                            Re: Simple Arrays

                            Thanks for your help. You all started to get me a wee bit confused, lol. Had
                            a bit of trouble with the Exit For statement at first, it should have been
                            Exit Sub.
                            It works fine now. Below is a code listing. Thanks again.

                            Dim Numbers(1 To 5) As Integer

                            Private Sub cmdCheckForBlan ks_Click()
                            Dim ctl As TextBox
                            Dim Index As Integer
                            For Each ctl In txtNumbers
                            If Len(ctl.Text) = 0 Then
                            MsgBox "What part of 'enter 5 numbers' don't you understand?"
                            ctl.SetFocus
                            Exit Sub
                            End If
                            Next ctl
                            For Index = 1 To 5
                            Numbers(Index) = txtNumbers(Inde x).Text
                            Next Index
                            End Sub

                            Private Sub cmdDisplayArray _Click()
                            Dim Index As Integer
                            For Index = 1 To 5
                            lblNumbers(Inde x).Caption = Numbers(Index)
                            Next Index
                            End Sub


                            Comment

                            Working...