Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 150,085 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,786 people online right now. Registration is fast and FREE... Join Now!




Help outputting list of calculated factorials in label

 
Reply to this topicStart new topic

Help outputting list of calculated factorials in label

SilentCodingOne
5 Jun, 2008 - 07:46 PM
Post #1

D.I.C Head
**

Joined: 8 Nov, 2007
Posts: 63


My Contributions
I'm working on a school project where I'm suppose determine the factorial for each number from 1 to 20. I have been able to calculate the factorial for each number and output in a top to bottom list within the console window. However when I try to do it in a label as per requirements the only output I get is for 20. any ideas as to what I'm doing wrong?

CODE

Public Class frmMain

    Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim intInputNbr As Integer = 20
        Dim intLoopCtr As Integer
        Dim numCtr As Integer = 1
        Dim lngFactorial As Long = 1

        For intLoopCtr = 1 To intInputNbr

            lngFactorial = lngFactorial * intLoopCtr
            lblNumberSeries.Text = numCtr & ControlChars.CrLf
            lblNumberFactorial.Text = lngFactorial & ControlChars.CrLf
            numCtr += 1
            Console.WriteLine(lngFactorial)

        Next




    End Sub

End Class


Thanks
User is offlineProfile CardPM
+Quote Post

robertelder
RE: Help Outputting List Of Calculated Factorials In Label
6 Jun, 2008 - 05:20 AM
Post #2

New D.I.C Head
*

Joined: 5 Jun, 2008
Posts: 30



Thanked: 3 times
My Contributions
SilentCodingOne,

Try reading the code in your For Loop again.
CODE

  For intLoopCtr = 1 To intInputNbr

            lngFactorial = lngFactorial * intLoopCtr
            lblNumberSeries.Text = numCtr & ControlChars.CrLf
            lblNumberFactorial.Text = lngFactorial & ControlChars.CrLf
            numCtr += 1
            Console.WriteLine(lngFactorial)
  Next


At the end of the For Loop you tell the app to write "lngFactorial" to the Console. When you look at the Console, it appears to have worked perfectly. The problem is that the Console.WriteLine automatically saves what was previously written and acts kind of like a log. As it stands right now, each time the For Loop loops around, it is simply changing the text of the labels, not adding to them.

You can solve this by telling the labels that their text equals what it currenly is plus this next value. This can be written out the long way or by using the shortcut "+=" like you did when you incremented numCtr. Both methods are shown in the following:
CODE

  For intLoopCtr = 1 To intInputNbr
            ...
            lblNumberSeries.Text = lblNumberSeries.Text & numCtr & ControlChars.CrLf
            lblNumberFactorial.Text += lngFactorial & ControlChars.CrLf
            ...
  Next


I hope this helps you out!

-Rob

User is offlineProfile CardPM
+Quote Post

SilentCodingOne
RE: Help Outputting List Of Calculated Factorials In Label
6 Jun, 2008 - 06:23 AM
Post #3

D.I.C Head
**

Joined: 8 Nov, 2007
Posts: 63


My Contributions
QUOTE(robertelder @ 6 Jun, 2008 - 06:20 AM) *

SilentCodingOne,

Try reading the code in your For Loop again.
CODE

  For intLoopCtr = 1 To intInputNbr

            lngFactorial = lngFactorial * intLoopCtr
            lblNumberSeries.Text = numCtr & ControlChars.CrLf
            lblNumberFactorial.Text = lngFactorial & ControlChars.CrLf
            numCtr += 1
            Console.WriteLine(lngFactorial)
  Next


At the end of the For Loop you tell the app to write "lngFactorial" to the Console. When you look at the Console, it appears to have worked perfectly. The problem is that the Console.WriteLine automatically saves what was previously written and acts kind of like a log. As it stands right now, each time the For Loop loops around, it is simply changing the text of the labels, not adding to them.

You can solve this by telling the labels that their text equals what it currenly is plus this next value. This can be written out the long way or by using the shortcut "+=" like you did when you incremented numCtr. Both methods are shown in the following:
CODE

  For intLoopCtr = 1 To intInputNbr
            ...
            lblNumberSeries.Text = lblNumberSeries.Text & numCtr & ControlChars.CrLf
            lblNumberFactorial.Text += lngFactorial & ControlChars.CrLf
            ...
  Next


I hope this helps you out!

-Rob


Thanks Rob

User is offlineProfile CardPM
+Quote Post

robertelder
RE: Help Outputting List Of Calculated Factorials In Label
6 Jun, 2008 - 07:49 AM
Post #4

New D.I.C Head
*

Joined: 5 Jun, 2008
Posts: 30



Thanked: 3 times
My Contributions
Glad to be of help. Good luck with your class.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 11:44PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month