Factorial Function

J

jtnpham

Okay so I have a label with the ID = lblResult. I want to display
each factorial of 1 through five. But when I run my program it only
displays the value of 5!. Can someone help me put the values of 1! -
4!? I would like it to display as follows:

1
2
6
24
120

or

1, 2, 6, 24, 120

Here is my code:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim n, sum As Integer
getFactorial(n, sum)

End Sub
Private Function getFactorial(ByVal n As Integer, ByRef sum As
Integer) As Integer
Dim i As Integer
n = 0
sum = 1
For i = 1 To 5
n = n + 1
sum = n * sum
lblResult.Text = sum
Next i
End Function

Thanks! =)
 
J

John Coleman

The line "lblResult.Text = sum" keeps overwriting the old text.
Try something like lblResult.Text = lbl.Text & vbCrLf & sum

hope that helps

-John Coleman
 
J

jtnpham

Thanks! So to put the answer on each individual line? There is a
command similar to vbcrlf right?

Okay, this might sound really dumb.. but can you explain the
statement:

lblResult.Text & vbCrLf & sum

Like, how does that say to put it all in the same line without
overwriting like I had previously?
 
G

Guest

There is a
command similar to vbcrlf right?


Are you using VB.Net

Environment.NewLine
or
ControlChars.CrLf
or
ControlChars.NewLine

whether it is intepreted as intended by your textbox, I can't say.
 
J

John Coleman

Thanks! So to put the answer on each individual line? There is a
command similar to vbcrlf right?

I'm not sure what you mean here. vbCrLf is a Windows-style new line.
It's inclusion in the string introduces line breaks.

By the way, in Excel VBA labels in userforms don't seem to have a Text
property - they have a caption property instead. What sort of label
control is this? It doesn't quite look like Excel VBA. Is this VB6?
Okay, this might sound really dumb.. but can you explain the
statement:

lblResult.Text & vbCrLf & sum

Like, how does that say to put it all in the same line without
overwriting like I had previously?

& is the concatenation operator. It tacks a newline and the new sum on
the *end* of the old string - thus not overwriting the old string. It
is sort of like the difference between

x = 5
x = 7 'the 5 is lost!

vs
x = 5
x = x + 7 'the 5 is still part of the sum

A drawback to the code fragment I gave in my first response is that it
would *start* with a newline - hence a blank line would be printed.
There are 2 solutions (if it bothers you)
1) delete it after the loop: lblResult.Text = Mid(lblResult.Text,3)
should do it (it keeps the string from the 3rd character on since the
first 2 are cr and lf control characters).

2) initialize lblResult.Text before the loop and pass through the loop
1 fewer times:

Private Function getFactorial(ByVal n As Integer, ByRef sum As
Integer) As Integer
Dim i As Integer
Dim fact as integer
n = 0 '? what does this actually do? - it seems to be a
redundant counter
'also - what does "sum" do - it is acting like a local
variable
'instead of a passed parameter, so why not delete it?
fact = 1 ' "sum" doesn't quite make sense - factorials are
products, not sums
lblResult.Text = "1"
For i = 2 To 5
fact = i * fact 'see - i can do the job that you had n
doing
lblResult.Text = lblResult & vbCrLf & fact
Next i
End Function

note that I made a few comments. the parameters you had for the
function are just local variables, so it makes more sense to declare
them inside the function than as part of a parameter list.
Furthermore, it isn't returning a value - so why not have it a sub?
Thus, maybe just:

Private Sub getFactorial()
Dim i As Integer
Dim fact as integer
fact = 1
lblResult.Text = "1"
For i = 2 To 5
fact = i * fact
lblResult.Text = lblResult & vbCrLf & fact
Next i
End Sub


Then in your other sub you could replace

Dim n, sum As Integer
getFactorial(n, sum)


by

getFactorial

Hope that helps

-John Coleman
 
J

John Coleman

After I saw Tom's post I looked at your code in more detail (focusing
on the first part and not just the second) and it seems clear that you
are using VB.Net (and not VB6, which is essentially the same as VBA as
far as the core language goes). The phrase System.EventArgs gives it
away. & is still a concatenation operator in VB.Net and (judging from
how often it appears in a google search of the .Net newsgroups) vbcrlf
is still recognized - but don't quote me on that, so the basic
solution should be the same. If you are still having trouble, maybe
you can post on one of the following newsgroups:

microsoft.public.dotnet.general
microsoft.public.dotnet.languages.vb
microsoft.public.vsnet.general


(especially the second) since someone there will probably be more
aware of the syntactical fine points.

Hope that helps

-John Coleman
 

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

Similar Threads


Top