473,407 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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_Click()
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(Index).Text
Next Index
End If
End Sub

Private Sub cmdDisplayArray_Click()
Dim Index As Integer
For Index = 1 To 5
lblNumbers(Index).Caption = Numbers(Index)
Next Index
End Sub
Jul 17 '05 #1
13 4670
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" <ro**************@blueyonder.co.uk> wrote in message
news:f6*******************@news-binary.blueyonder.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_Click()
: 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(Index).Text
: Next Index
: End If
: End Sub
:
: Private Sub cmdDisplayArray_Click()
: Dim Index As Integer
: For Index = 1 To 5
: lblNumbers(Index).Caption = Numbers(Index)
: Next Index
: End Sub
:
:
Jul 17 '05 #2
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" <rg************@mvps.org> wrote in message
news:qv*******************@news02.bloor.is.net.cab le.rogers.com...
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" <ro**************@blueyonder.co.uk> wrote in message
news:f6*******************@news-binary.blueyonder.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_Click()
: 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(Index).Text
: Next Index
: End If
: End Sub
:
: Private Sub cmdDisplayArray_Click()
: Dim Index As Integer
: For Index = 1 To 5
: lblNumbers(Index).Caption = Numbers(Index)
: Next Index
: End Sub
:
:

Jul 17 '05 #3
"Mauro" <mb******@hotmail.com> wrote in message news:<_8rCb.681270$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
Jul 17 '05 #4
yep .. that's why it was air code. <g>

--

Randy Birch
MVP Visual Basic
http://www.mvps.org/vbnet/
Please respond only to the newsgroups so all can benefit.
"Bob Butler" <bu*******@earthlink.net> wrote in message
news:fa*************************@posting.google.co m...
: "Mauro" <mb******@hotmail.com> wrote in message
news:<_8rCb.681270$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
Jul 17 '05 #5

"Randy Birch" <rg************@mvps.org> wrote in message
news:pm******************@twister01.bloor.is.net.c able.rogers.com...
yep .. that's why it was air code. <g>
off the top of my head ...

dim ctl as textbox

for each ctl in me


If you declare ctl as TextBox, not Control, shouldn't this code break if
encounters a control that is not a TextBox?
Jul 17 '05 #6
> > yep .. that's why it was air code. <g>
off the top of my head ...

dim ctl as textbox

for each ctl in me


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


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
Jul 17 '05 #7
Thanks guys, I'll give it a try.
Jul 17 '05 #8
"Rick Rothstein" <ri************@NOSPAMcomcast.net> wrote in message news:<Is********************@comcast.com>...
yep .. that's why it was air code. <g>

> off the top of my head ...
>
> dim ctl as textbox
>
> for each ctl in me
>


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


Correct... for the code Randy posted, ctl needs to be declared as a Control.


dang, can't believe I missed that!
Jul 17 '05 #9

"Rick Rothstein" <ri************@NOSPAMcomcast.net> wrote in message
news:Is********************@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(txtNumbers), 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?
Jul 17 '05 #10
"Steve Gerrard" <no*************@comcast.net> wrote
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(txtNumbers), 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?

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! =-----
Jul 17 '05 #11
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
http://www.mvps.org/vbnet/
Please respond only to the newsgroups so all can benefit.
"Steve Gerrard" <no*************@comcast.net> wrote in message
news:EK********************@comcast.com...
:
: "Rick Rothstein" <ri************@NOSPAMcomcast.net> wrote in message
: news:Is********************@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(txtNumbers), 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?
:
:
Jul 17 '05 #12

"Larry Serflaten" <Ab***@SpamBusters.com> wrote in message
news:3f********@corp.newsgroups.com...
"Steve Gerrard" <no*************@comcast.net> wrote
Doesn't this all mean that actually what you have is a collection, not an array?

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.)


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.
Jul 17 '05 #13
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 cmdCheckForBlanks_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(Index).Text
Next Index
End Sub

Private Sub cmdDisplayArray_Click()
Dim Index As Integer
For Index = 1 To 5
lblNumbers(Index).Caption = Numbers(Index)
Next Index
End Sub
Jul 17 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jedispy | last post by:
Hello, I'm pretty new to PHP, and for starters I want to create a simple script for creating dynamic web pages. Here is how I want it to work: index.html has 4 thumbnail images on it. When users...
2
by: Westcoast Sheri | last post by:
Any way to do a simple delete from array? In other words, what would be the *easiest* (and fastest php runtime) way to delete "banana" from the following array: $my_array = array( "apple",...
31
by: da Vinci | last post by:
OK, this has got to be a simple one and yet I cannot find the answer in my textbook. How can I get a simple pause after an output line, that simply waits for any key to be pressed to move on? ...
7
by: jmac | last post by:
Greetings fellow programmers, I have created a C program that has a few bugs and would like to get some help with working them out. Here is a list of the problems that I am experiencing: -...
51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
9
by: niclane | last post by:
Hi, I was reading section 5.5 of Ritchie and Kernighan and saw the following: " ..... char amessage = "now is the time"; char *pmessage = "now is the time";
9
by: NewInTheGame | last post by:
I'm a beginner in VB.Net. I'm actually taking a class & so far I understand everything, but I suck when it comes to arrays. I'm sure that this is simple (I'm ashamed of asking :oops: )... I...
4
by: learnfpga | last post by:
Here is a little code I wrote to add the numbers input by the user.....I was wondering if its possible to have the same functionality without using dynamic arrays.....just curious..... ...
24
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.