473,507 Members | 6,295 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

trying to create a simple control

Hi
I'm really new to programming and i'm reading through a book, teach yourself
asp.net by SAMS.
I'm having some difficulty with one of the vb.net lessons.
What i am supposed to be doing is create simple function to return the
factorial amount of the numbers 1 to 5.
Factorial is where you take a number and multiply it by all the digits that
exist below that number, e.g 3 would be 3 * 2 * 1 which would give you a
total of 6.

I've been really stuck with this for hours on end. It doesnt help that there
are several very annoying mistakes in the book, ( a bad worker blames his
tools)
So i just wanted to post the code here,
Please somone show me where i am wrong.

Function Factorial (n as integer) as integer
Dim i as integer
for i = 1 to 5
i = i * (i-1)
next i
Return Factorial

End function

Sub Page_load (sender as object, e as EventArgs)
response.write(factorial)
end sub

Please help me.

Raphael
Nov 20 '05 #1
7 2206
Your always getting zero aren't you?

try

Dim i As Integer

Dim fact As Double

fact = 5

For i = fact To 1 Step -1

fact = fact * i

Next

MsgBox(fact)

you use 'i' too many times, just changng the variable all over the place...
this should help you out.

-peace,

cJ

"Raphael Gluck" <iwish i could tell you @alas blame the spammers> wrote in
message news:O7**************@TK2MSFTNGP12.phx.gbl...
Hi
I'm really new to programming and i'm reading through a book, teach yourself asp.net by SAMS.
I'm having some difficulty with one of the vb.net lessons.
What i am supposed to be doing is create simple function to return the
factorial amount of the numbers 1 to 5.
Factorial is where you take a number and multiply it by all the digits that exist below that number, e.g 3 would be 3 * 2 * 1 which would give you a
total of 6.

I've been really stuck with this for hours on end. It doesnt help that there are several very annoying mistakes in the book, ( a bad worker blames his
tools)
So i just wanted to post the code here,
Please somone show me where i am wrong.

Function Factorial (n as integer) as integer
Dim i as integer
for i = 1 to 5
i = i * (i-1)
next i
Return Factorial

End function

Sub Page_load (sender as object, e as EventArgs)
response.write(factorial)
end sub

Please help me.

Raphael

Nov 20 '05 #2
Peace to you too, (we could do with some of that around here).

I am actually getting an error
BC30455: Argument not specified for parameter 'n' of 'Public Function
Factorial(n As Integer) As Integer'.
This is my original code

Function Factorial (n as integer) as integer
Dim i as integer
for i = 1 to 5 i = i * (i-1)
next i
Return Factorial
End function
Sub Page_load (sender as object, e as EventArgs) response.write(factorial)
end sub
Nov 20 '05 #3
You had:
Function Factorial (n as integer) as integer
Dim i as integer
for i = 1 to 5
i = i * (i-1)
next i
Return Factorial

This would recieve an integer and return a result i think:

Function Factorial (n as integer) as integer
Dim intResult as integer = 1
for n= n to 1 step -1 'done it his way to help understanding
intResult = intResult * (n)
next n
Return intResult

dim myinput as integer =Val(textnum.text)
dim myValue as integer
myValue = factorial(myinput)
"Herfried K. Wagner [MVP]" <hi*******@m.activevb.de> wrote in message
news:uM**************@TK2MSFTNGP12.phx.gbl...
Hello,

"Raphael Gluck" <iwish i could tell you @alas blame the spammers> schrieb:
I'm really new to programming and i'm reading through a book, teach

yourself
asp.net by SAMS.
I'm having some difficulty with one of the vb.net lessons.
What i am supposed to be doing is create simple function to return the
factorial amount of the numbers 1 to 5.
Factorial is where you take a number and multiply it by all the digits

that
exist below that number, e.g 3 would be 3 * 2 * 1 which would give you a
total of 6.


Quick and Dirty:

\\\
Private Function FacIterative(ByVal i As Integer) As Integer
Dim j As Integer, k As Integer = 1
For j = 1 To i
k = k * j
Next j
Return k
End Function
///

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
http://www.mvps.org/dotnet

Nov 20 '05 #4
Cor

"Raphael Gluck".
I shall try to explain you
Factorial is where you take a number and multiply it by all the digits that exist below that number, e.g 3 would be 3 * 2 * 1 which would give you a
total of 6.
I take your code first and start with the mainroutine
-------------------------Sub Page_load (sender as object, e as EventArgs)
response.write(factorial) Prints the after that the function factorial is done, the return integer on
a webpage, nothing wrong but only 1 time.end sub -------------------------Function Factorial (n as integer) as integer Because you had not called it with an integer, the n as integer is a fault,
that gives an errorDim i as integer
for i = 1 to 5
i = i * (i-1)
next i
Return Factorial When you do this, you would return nothingEnd function

-------------------------
I changed it something
\\\\\\
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim i As Integer
For i = 1 To 5
Response.Write(i.ToString & "=" & Factorial(i) & "<BR>")
Next
'Prints 5 rows from 1 to 5 and start 5 times the function Factorial and send
the value of i to that function.
'The response.write is not so nice in a VB.net webproject but lets not
bother about that, the <br> is a html line feed
End Sub

Function Factorial(ByVal a As Integer) As Integer
'Does the function with the value you suported, now in this funtion it is
called a
Dim i As Integer
For i = 1 To a - 1
If Factorial = 0 Then Factorial = 1
Factorial += Factorial * i
Next
'Figure above out yourself
Return Factorial
'This was your bigest mistake, the Return gives the As integer retour
End Function
////
I don't know if you will read this, so after the rest, but I am curious if
this works ?
I found it funny to do.
Cor
Nov 20 '05 #5
Cor
Raphael,
Don't look at my typing and correcting errors please.
Some of them
I shall try to explain you I shall try to explain this too you Prints the after that the function factorial is done, the return integer

on
This prints after that the function "factorial" is done, the integer that is
returned etc etc.
:-)
Cor
Nov 20 '05 #6
Hello,

"Jeff Brown" <no******@thistime.com> schrieb:
for n= n to 1 step -1 'done it his way to help understanding
intResult = intResult * (n)


Why '(n)'?

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #7
sorry should have been just n, but i just took out some things and left the
() that was already there.

Good catch!!!

"Herfried K. Wagner [MVP]" <hi*******@m.activevb.de> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hello,

"Jeff Brown" <no******@thistime.com> schrieb:
for n= n to 1 step -1 'done it his way to help understanding
intResult = intResult * (n)


Why '(n)'?

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
http://www.mvps.org/dotnet

Nov 20 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1432
by: Kelly | last post by:
Hi. I am trying to do something that seems very simple. I want to create a class that inherits from the ListViewItem Control so that I can add a few custom properties to it. I then want to make...
3
3504
by: Bill | last post by:
I have a seating chart web form that has over 50 entry field controls (tables/booths) where I use a DropDownList box to select a single company name from a single large list of organizations (200...
12
2912
by: Phil Certain | last post by:
Hi, I'm trying to do something very simple...or at least it should be. I have created a host page (gen.aspx) and a very simple user control (us.ascx). The corresponding code-behind files are...
4
2040
by: JimC | last post by:
On my main form in a C# program, I create an instance of another form that contains a ListView control, in the usual way, that is: public class frmMain : System.Windows.Forms.Form { // ...
14
5875
by: dba_222 | last post by:
Dear experts, Again, sorry to bother you again with such a seemingly dumb question, but I'm having some really mysterious results here. ie. Create procedure the_test As
6
3319
by: AppleBag | last post by:
I'm having the worst time trying to login to myspace through code. Can someone tell me how to do this? Please try it yourself before replying, only because I have asked this a couple of times in...
8
3749
by: William Foster | last post by:
Good evening all, I am creating a program with the ability to select an option from a ComboBox which will then create a secondary list of options in a CheckedListBox. Previously when I have...
3
12240
by: John Kotuby | last post by:
Hi all... I am trying to do a simple thing and maybe am missing something elementary. I have created a Javascript function at the top of a page which is meant to enable editing of an HTML input...
2
3927
by: chandan | last post by:
Hi, How to create an instance of user control in a class(simple) file not in Page class file? As I tried I can do it in page class file but not in simple class file. Let me know the solution!! ...
15
5238
by: lxyone | last post by:
Using a flat file containing table names, fields, values whats the best way of creating html pages? I want control over the html pages ie 1. layout 2. what data to show 3. what controls to...
0
7308
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7371
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7479
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5617
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3188
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1534
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.