ArrayList Function In Business Tier?

L

Leon

I have a webform in which when the user press generate button the form
generate six unique ramdon numbers, the user can also type these six numbers
in manually in any order. however, the user can also press a post button
that post these six numbers into the database. My problem is that these six
numbers need to be posted to the database from less to greatest, and all of
my code is within the business tier class not the code behind class.

After looking at my code below...
How do I create an ArrayList that can get the values from the Generate
function ("which include a stack") and pass to the form, and get the values
from the form and pass them to the database? **Please note where you see is
my problem. Thanks


Private Function Generate(ByVal Value As Integer) As Integer

Dim arrl(Value) As Integer

Dim x As Integer

Dim j As Integer

Dim s As Stack = New Stack

Dim blnexists As Boolean = True

For j = 0 To Value

arr1(j) = GetRandomNumber()

blnexists = s.Contains(arr1(j))

While blnexists = True

arr1(j) = GetRandomNumber()

blnexists = s.Contains(arr1(j))

End While

s.Push(arr1(j))

Next



For x = 0 To Value

arr1(x) = s.Pop()

Next


'Just Tried something did not work

'Dim I As Integer

'Dim pickNums As ArrayList = New ArrayList



'For I = 0 To PickCount

'pickNums.Add(arrl(I))

'Next

'GeneratePick = SortPick(pickNums)


End Function



'Just Tried something did not work

'Public Function SortPick(ByVal UserPick As ArrayList)

'UserPick.Sort()

'End Function
 
M

mortb

Not sure what you want to do but here is a function that returns a sorted
stack of numberOfNumbers random numbers
cheers,
mortb

Private Function Generate(ByVal numberOfNumbers As Integer) As Stack
Dim al as ArrayList = new ArrayList(numberOfNumbers)
Dim rand as random = new Random()
for i as integer = 0 to numberOfNumbers
Dim rnd as integer = rand.Next()
while(al.Contains(rnd))
rnd = rand.Next()
end while
al.Add(rnd)
next
al.Sort()
Generate = new Stack(al)
End Function

pseudo code:
page_onload

stack = generate
textbox1 = (stack.pop)ToString()
textbox2 = (stack.pop)ToString()
..

end function
 
M

mortb

oops,
when the elements are added to the stack they are reversed to avoid this
insert
al.Reverse()
before
Generate = new Stack(al)

cheers,
mortb
 
L

Leon

Thanks for your reply, but what I want to do is pass the randomly generated
numbers from the Generate function which is in the business tier to my
webform.
Do I need to declare the Generate function as a ArrayList or build a
separate ArrayList function or sub and pass the values from there to my
page?
OR....
How do I get the five generated number from this class which is in the
business tier to my webform?
See Code...
Private Function Generate(ByVal Value As Integer) As Integer
Dim arrl(Value) As Integer
Dim x As Integer
Dim j As Integer
Dim s As Stack = New Stack
Dim blnexists As Boolean = True
For j = 0 To Value
arr1(j) = GetRandomNumber()
blnexists = s.Contains(arr1(j))
While blnexists = True
arr1(j) = GetRandomNumber()
blnexists = s.Contains(arr1(j))
End While
s.Push(arr1(j))
Next
For x = 0 To Value
arr1(x) = s.Pop()
Next
End Function
 
M

mortb

Like this (pseudo code not compiled)?
Page code behind:

submit onclick:

Dim userRandomNumbers as ArrayList = new ArrayList()
if userRnd1.Text.Trim() != ""
userRandomNumbers.Add(int.Parse(userRnd1.Text.Trim())) ' Assuming there
are text fields for the random numbers
end if
if userRnd2.Text.Trim() != ""
userRandomNumbers.Add(int.Parse(userRnd2.Text.Trim()))
end if
....

' if the generate would return an arraylist of x random numbers:
int x = 6 - userRandomNumbers.Count
userRandomNumbers.AddRange(BusinessObject.Generate(x))
userRandomNumbers.Sort()

'if you want to display the resulting numbers on your webform:
userRnd1.Text.= userRandomNumbers[0]
userRnd2.Text.= userRandomNumbers[1]
....

' code that post data to database goes here...
 
L

Leon

Thanks For all the help, but this is my code and it runs but it only return
one value to the textbox and I know the other values exist because I step
inside of my code. what do you see that I'm doing wrong. thanks again!!!

Private Function Generate(ByVal Value As Integer) As ArrayList
Dim arrl(Value) As Integer
Dim x As Integer
Dim j As Integer
Dim s As Stack = New Stack
Dim blnexists As Boolean = True
For j = 0 To Value
arr1(j) = GetRandomNumber()
blnexists = s.Contains(arr1(j))
While blnexists = True
arr1(j) = GetRandomNumber()
blnexists = s.Contains(arr1(j))
End While
s.Push(arr1(j))
Next
For x = 0 To Value
arr1(x) = s.Pop()
Next
Dim I As Integer
Dim UserNum As ArrayList = New ArrayList
For I = 0 To Value
UserNum.Add(arr1(I))
Next
Generate= New ArrayList(UserNum)
End Function

And then this code is in my code behind class...............

Dim test As ArrayList
test = newPick.GeneratePick(5)

Me.TextBox1.Text = test.Item(0)
Me.TextBox1.Text = test.Item(1)
Me.TextBox1.Text = test.Item(2)
Me.TextBox1.Text = test.Item(3)
Me.TextBox1.Text = test.Item(4)
Me.TextBox1.Text = test.Item(5)

mortb said:
Like this (pseudo code not compiled)?
Page code behind:

submit onclick:

Dim userRandomNumbers as ArrayList = new ArrayList()
if userRnd1.Text.Trim() != ""
userRandomNumbers.Add(int.Parse(userRnd1.Text.Trim())) ' Assuming there
are text fields for the random numbers
end if
if userRnd2.Text.Trim() != ""
userRandomNumbers.Add(int.Parse(userRnd2.Text.Trim()))
end if
...

' if the generate would return an arraylist of x random numbers:
int x = 6 - userRandomNumbers.Count
userRandomNumbers.AddRange(BusinessObject.Generate(x))
userRandomNumbers.Sort()

'if you want to display the resulting numbers on your webform:
userRnd1.Text.= userRandomNumbers[0]
userRnd2.Text.= userRandomNumbers[1]
...

' code that post data to database goes here...

Leon said:
Thanks for your reply, but what I want to do is pass the randomly
generated numbers from the Generate function which is in the business
tier to my webform.
Do I need to declare the Generate function as a ArrayList or build a
separate ArrayList function or sub and pass the values from there to my
page?
OR....
How do I get the five generated number from this class which is in the
business tier to my webform?
 
M

mortb

If the numbers are there when you step into the code then the problem has to
be somwhere else -- somewhere in the code that you haven't posted.

By the way, why do you create a stack? and an array? It seems you only need
the array list which you return...

cheers,
mortb

Private Function Generate(ByVal Value As Integer) As ArrayList
Dim arrl As ArrayList(Value)
For j = 0 To Value
Dim rnd as integer = GetRandomNumber()
While arrl.Contains(rnd)
rnd = GetRandomNumber()
End While
arrl .Add(rnd)
Next
Generate = arrl
End Function


Leon said:
Thanks For all the help, but this is my code and it runs but it only
return one value to the textbox and I know the other values exist because
I step inside of my code. what do you see that I'm doing wrong. thanks
again!!!

Private Function Generate(ByVal Value As Integer) As ArrayList
Dim arrl(Value) As Integer
Dim x As Integer
Dim j As Integer
Dim s As Stack = New Stack
Dim blnexists As Boolean = True
For j = 0 To Value
arr1(j) = GetRandomNumber()
blnexists = s.Contains(arr1(j))
While blnexists = True
arr1(j) = GetRandomNumber()
blnexists = s.Contains(arr1(j))
End While
s.Push(arr1(j))
Next
For x = 0 To Value
arr1(x) = s.Pop()
Next
Dim I As Integer
Dim UserNum As ArrayList = New ArrayList
For I = 0 To Value
UserNum.Add(arr1(I))
Next
Generate= New ArrayList(UserNum)
End Function

And then this code is in my code behind class...............

Dim test As ArrayList
test = newPick.GeneratePick(5)

Me.TextBox1.Text = test.Item(0)
Me.TextBox1.Text = test.Item(1)
Me.TextBox1.Text = test.Item(2)
Me.TextBox1.Text = test.Item(3)
Me.TextBox1.Text = test.Item(4)
Me.TextBox1.Text = test.Item(5)

mortb said:
Like this (pseudo code not compiled)?
Page code behind:

submit onclick:

Dim userRandomNumbers as ArrayList = new ArrayList()
if userRnd1.Text.Trim() != ""
userRandomNumbers.Add(int.Parse(userRnd1.Text.Trim())) ' Assuming
there are text fields for the random numbers
end if
if userRnd2.Text.Trim() != ""
userRandomNumbers.Add(int.Parse(userRnd2.Text.Trim()))
end if
...

' if the generate would return an arraylist of x random numbers:
int x = 6 - userRandomNumbers.Count
userRandomNumbers.AddRange(BusinessObject.Generate(x))
userRandomNumbers.Sort()

'if you want to display the resulting numbers on your webform:
userRnd1.Text.= userRandomNumbers[0]
userRnd2.Text.= userRandomNumbers[1]
...

' code that post data to database goes here...

Leon said:
Thanks for your reply, but what I want to do is pass the randomly
generated numbers from the Generate function which is in the business
tier to my webform.
Do I need to declare the Generate function as a ArrayList or build a
separate ArrayList function or sub and pass the values from there to my
page?
OR....
How do I get the five generated number from this class which is in the
business tier to my webform?
 
L

Leon

I use the stack just to make sure I get 5 unique random number back.
however, what I sent was all the code and what I meant by say "the number
are there when I step into my project is the numbers are in the stack, but
it only sends one value back to the calling function ArrayList. maybe I can
change the stack to ArrayList but my biggest problem is getting from the
Generate Function ArrayList to my textboxes in my webform on button click.

mortb said:
If the numbers are there when you step into the code then the problem has
to be somwhere else -- somewhere in the code that you haven't posted.

By the way, why do you create a stack? and an array? It seems you only
need the array list which you return...

cheers,
mortb

Private Function Generate(ByVal Value As Integer) As ArrayList
Dim arrl As ArrayList(Value)
For j = 0 To Value
Dim rnd as integer = GetRandomNumber()
While arrl.Contains(rnd)
rnd = GetRandomNumber()
End While
arrl .Add(rnd)
Next
Generate = arrl
End Function


Leon said:
Thanks For all the help, but this is my code and it runs but it only
return one value to the textbox and I know the other values exist because
I step inside of my code. what do you see that I'm doing wrong. thanks
again!!!

Private Function Generate(ByVal Value As Integer) As ArrayList
Dim arrl(Value) As Integer
Dim x As Integer
Dim j As Integer
Dim s As Stack = New Stack
Dim blnexists As Boolean = True
For j = 0 To Value
arr1(j) = GetRandomNumber()
blnexists = s.Contains(arr1(j))
While blnexists = True
arr1(j) = GetRandomNumber()
blnexists = s.Contains(arr1(j))
End While
s.Push(arr1(j))
Next
For x = 0 To Value
arr1(x) = s.Pop()
Next
Dim I As Integer
Dim UserNum As ArrayList = New ArrayList
For I = 0 To Value
UserNum.Add(arr1(I))
Next
Generate= New ArrayList(UserNum)
End Function

And then this code is in my code behind class...............

Dim test As ArrayList
test = newPick.GeneratePick(5)

Me.TextBox1.Text = test.Item(0)
Me.TextBox1.Text = test.Item(1)
Me.TextBox1.Text = test.Item(2)
Me.TextBox1.Text = test.Item(3)
Me.TextBox1.Text = test.Item(4)
Me.TextBox1.Text = test.Item(5)

mortb said:
Like this (pseudo code not compiled)?
Page code behind:

submit onclick:

Dim userRandomNumbers as ArrayList = new ArrayList()
if userRnd1.Text.Trim() != ""
userRandomNumbers.Add(int.Parse(userRnd1.Text.Trim())) ' Assuming
there are text fields for the random numbers
end if
if userRnd2.Text.Trim() != ""
userRandomNumbers.Add(int.Parse(userRnd2.Text.Trim()))
end if
...

' if the generate would return an arraylist of x random numbers:
int x = 6 - userRandomNumbers.Count
userRandomNumbers.AddRange(BusinessObject.Generate(x))
userRandomNumbers.Sort()

'if you want to display the resulting numbers on your webform:
userRnd1.Text.= userRandomNumbers[0]
userRnd2.Text.= userRandomNumbers[1]
...

' code that post data to database goes here...

Thanks for your reply, but what I want to do is pass the randomly
generated numbers from the Generate function which is in the business
tier to my webform.
Do I need to declare the Generate function as a ArrayList or build a
separate ArrayList function or sub and pass the values from there to my
page?
OR....
How do I get the five generated number from this class which is in the
business tier to my webform?
 
L

Leon

Thanks for all the help I got it. LOOK! in the code I posted all the
textboxes was the same name so all the values went to that textbox.

Me.TextBox1.Text = test.Item(0)
Me.TextBox1.Text = test.Item(1)
Me.TextBox1.Text = test.Item(2)
Me.TextBox1.Text = test.Item(3)
Me.TextBox1.Text = test.Item(4)
Me.TextBox1.Text = test.Item(5)

Leon said:
I use the stack just to make sure I get 5 unique random number back.
however, what I sent was all the code and what I meant by say "the number
are there when I step into my project is the numbers are in the stack, but
it only sends one value back to the calling function ArrayList. maybe I can
change the stack to ArrayList but my biggest problem is getting from the
Generate Function ArrayList to my textboxes in my webform on button click.

mortb said:
If the numbers are there when you step into the code then the problem has
to be somwhere else -- somewhere in the code that you haven't posted.

By the way, why do you create a stack? and an array? It seems you only
need the array list which you return...

cheers,
mortb

Private Function Generate(ByVal Value As Integer) As ArrayList
Dim arrl As ArrayList(Value)
For j = 0 To Value
Dim rnd as integer = GetRandomNumber()
While arrl.Contains(rnd)
rnd = GetRandomNumber()
End While
arrl .Add(rnd)
Next
Generate = arrl
End Function


Leon said:
Thanks For all the help, but this is my code and it runs but it only
return one value to the textbox and I know the other values exist
because I step inside of my code. what do you see that I'm doing wrong.
thanks again!!!

Private Function Generate(ByVal Value As Integer) As ArrayList
Dim arrl(Value) As Integer
Dim x As Integer
Dim j As Integer
Dim s As Stack = New Stack
Dim blnexists As Boolean = True
For j = 0 To Value
arr1(j) = GetRandomNumber()
blnexists = s.Contains(arr1(j))
While blnexists = True
arr1(j) = GetRandomNumber()
blnexists = s.Contains(arr1(j))
End While
s.Push(arr1(j))
Next
For x = 0 To Value
arr1(x) = s.Pop()
Next
Dim I As Integer
Dim UserNum As ArrayList = New ArrayList
For I = 0 To Value
UserNum.Add(arr1(I))
Next
Generate= New ArrayList(UserNum)
End Function

And then this code is in my code behind class...............

Dim test As ArrayList
test = newPick.GeneratePick(5)

Me.TextBox1.Text = test.Item(0)
Me.TextBox1.Text = test.Item(1)
Me.TextBox1.Text = test.Item(2)
Me.TextBox1.Text = test.Item(3)
Me.TextBox1.Text = test.Item(4)
Me.TextBox1.Text = test.Item(5)

Like this (pseudo code not compiled)?
Page code behind:

submit onclick:

Dim userRandomNumbers as ArrayList = new ArrayList()
if userRnd1.Text.Trim() != ""
userRandomNumbers.Add(int.Parse(userRnd1.Text.Trim())) ' Assuming
there are text fields for the random numbers
end if
if userRnd2.Text.Trim() != ""
userRandomNumbers.Add(int.Parse(userRnd2.Text.Trim()))
end if
...

' if the generate would return an arraylist of x random numbers:
int x = 6 - userRandomNumbers.Count
userRandomNumbers.AddRange(BusinessObject.Generate(x))
userRandomNumbers.Sort()

'if you want to display the resulting numbers on your webform:
userRnd1.Text.= userRandomNumbers[0]
userRnd2.Text.= userRandomNumbers[1]
...

' code that post data to database goes here...

Thanks for your reply, but what I want to do is pass the randomly
generated numbers from the Generate function which is in the business
tier to my webform.
Do I need to declare the Generate function as a ArrayList or build a
separate ArrayList function or sub and pass the values from there to
my page?
OR....
How do I get the five generated number from this class which is in the
business tier to my webform?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top