 |
| ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

September 12th, 2003, 03:13 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
Working with an Array
I'm trying to dynamically add numbers to an array:
Dim Ary() As Integer
Dim i
For i = 1 To 52
ReDim Preserve Ary(i)
Next
Dim testVar = Int((Ary(i) * Rnd()) + 1)
Label1.Text = testVar
I'm getting "Index was outside the bounds of the array"
I'm still learning asp.net and I'm creating a 'lottery picker' page.
|
|

September 12th, 2003, 04:26 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
If you're goal is a random number from 1 to 52...
Use this standard "random integer" formula:
(Max - Min + 1) * Rnd + Min
If Min = 1 use
Max * Rnd + Min
Code:
Label1.Text = Int(52 * Rnd + 1).ToString
As far as working with the array goes...
If you know what the bounds of your array is going to be, you can declare it explicitly. Only when one array dimension may change at runtime will you need to use ReDim.
Code:
Dim Ary(51) As Integer 'Dim 52 element array (base-0 index)
Dim i
For i = 0 To 51
'Populate array with numbers 1-52
Ary(i) = i + 1
Next
The error you are getting is due to the fact that "Next" in your for loop advanced i to 53 which was then outside the bounds of the array.
Peter
|
|

September 15th, 2003, 09:52 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
Is there any way to randomize an array that's a string?
Dim ary() AS String = {"Chilis", "Gumbeauxs", "Hops", "Logans", "Long Horn"}
'randomly pick one to display to label
'Labe1.text = ary()
|
|

September 15th, 2003, 10:01 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
An array's an array. It doesn't matter what's in the array. I think maybe you are getting confused/thrown off because your first array actually has numbers in it so in my response I just cut out the middleman of the array to generate a random number.
Here's a way to get a random element of an array with a dynamic size (that is, you don't know how long it will be until run-time):
Label1.Text = ary(Int((ary.GetUpperBound(0) + 1) * Rnd))
Give that a try.
Peter
|
|

September 15th, 2003, 10:09 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
Thanks for your help. Both of your examples worked.
I've always had difficulties with arrays. Most of the time I just use a table in MS Access to hold values instead of an array.
|
|

September 15th, 2003, 10:18 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
Wowsers!! That's like killing a mouse with a wrecking ball.
Learn arrays, they are very handy and flexible. Especially with the new stuff in .Net. Arrays (or collections, which are just complex abstractions of arrays) are at the heart of many of the controls you will use in .Net. Array, ArrayList and HashTable are objects that can be extremely useful when dealing with various sets of data when you are not actually working with DB based datasets.
Peter
|
|

September 16th, 2003, 09:10 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
Does anybody know a good site with examples using arrays?
|
|

September 16th, 2003, 09:58 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
This is what I have so far.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
aryList.Visible = True
Dim ary() = {"Chilis", "Gumbeauxs", "Hops", "Logans", "Long Horn", "O Charleys", "Olive Garden", _
"Out Back", "Pavinos", "Red Lobster", "Joe's Crab Shack", "Tokyo Steakhouse", "Golden Buddha", "Taco Mac"}
Dim i
i = ary(Int((ary.GetUpperBound(0) + 1) * Rnd()))
aryList.DataSource = ary
aryList.DataBind()
Label1.Text = i
End Sub
Sub myList_OnChange(ByVal s As System.Object, ByVal e As System.EventArgs)
If aryList.SelectedIndex > -1 Then
Label2.Text = "You selected<b> " & aryList.SelectedItem.Text & "</b>"
End If
End Sub
It works and I've used event handlers!!! But is there a way once an item has been selected from the array to remove it?
|
|

September 16th, 2003, 02:50 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
Mr. Paperclip says, "It looks like you are trying to create a pick list that removes each item as you pick it.":D
When the selector onchange event fires, you can do this at the end (after setting the label):
aryList.Items.Remove(aryList.SelectedItem)
This will remove the selected item. As long as you aren't building the list and binding the selector every time you will eliminate each selected item in the list as you select them.
Only problem with this is that you'll never be able to get rid of the default selected item (whether it's the first or not) because you'll be able to get "onchange" to fire. Otherwise it works.
Post again if you would like a little more detailed explanation as to why/how this works.
Peter
|
|

September 17th, 2003, 08:48 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
" Mr. Paperclip says, "It looks like you are trying to create a pick list that removes each item as you pick it.""
Yep :). Mr. Paperclip reminded me of a windows parody: http://www.deanliou.com/WinRG/WinRG.htm
It's pretty funny.
Thanks for your help.
" Only problem with this is that you'll never be able to get rid of the default selected item (whether it's the first or not) because you'll be able to get "onchange" to fire. Otherwise it works.
Post again if you would like a little more detailed explanation as to why/how this works."
My guess is that the selected item is stored in memory and returned back to the user.
|
|
 |