Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Factorial Function

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! =)

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 274
Default Factorial Function

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

hope that helps

-John Coleman



On Feb 19, 2:49 pm, wrote:
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! =)



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Factorial Function

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?

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Factorial Function

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.

--
Regards,
Tom Ogilvy


" wrote:

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?


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 274
Default Factorial Function

On Feb 19, 3:06 pm, wrote:
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



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 274
Default Factorial Function

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

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Factorial question M.M Excel Worksheet Functions 6 June 8th 09 04:33 PM
Factorial Listing Branson Excel Discussion (Misc queries) 3 June 6th 08 04:18 PM
Factorial (like =FACT) function? mr tom Excel Worksheet Functions 8 June 13th 06 01:45 AM
Range of Factorial Function Rushi Excel Discussion (Misc queries) 7 September 17th 05 12:28 AM
Help? Inverse factorial? 43fan Excel Programming 5 December 30th 03 06:25 PM


All times are GMT +1. The time now is 08:53 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"