![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Sep 2004
Posts: 7
Rep Power: 0
![]() |
Hello,
I'm a beginner in VB and I'm having trouble with my code. It's a prime number calculator. Here is the function: Public Function prime(x As Integer)
Dim primes(0) As Integer
primes(0) = 2
For y = 2 To x
bound= UBound(primes)
For Index = 0 To bound
If (y Mod primes(Index)) = 0 Then
Exit For
End If
Next Index
If Index = bound Then
ReDim Preserve primes(boun + 1)
primes(bound + 1) = y
End If
Next y
End FunctionWhen I run the code I get "Array already dimentioned." |
|
|
|
|
|
#2 |
|
Programming Guru
![]() ![]() ![]() |
Some of your variables are misspelled, others are undefined... I don't have VB on hand right now, but try this:
Public Function prime(ByVal x As Integer)
Dim primes(0) As Integer
Dim bound, y, Index As Integer
primes(0) = 2
For y = 2 To x
bound = UBound(primes)
For Index = 0 To bound
If (y Mod primes(Index)) = 0 Then
Exit For
End If
Next Index
If Index = bound Then
ReDim Preserve primes(bound + 1)
primes(bound + 1) = y
End If
Next y
End Function
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#3 |
|
Programming Guru
![]() |
edit: ignore what i wrote if you read it.
__________________
"Put your hand on a hot stove for a minute, and it seems like an hour. Sit with a pretty girl for an hour, and it seems like a minute. THAT'S relativity." - Albert Einstein |
|
|
|
|
|
#4 |
|
Programming Guru
![]() ![]() ![]() |
it was pretty good advice too Berto.
The code above compiles and runs fine in VB .NET
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#5 |
|
Programming Guru
![]() |
nah i said something that was wrong, by a mile so thought i should get rid of it.
__________________
"Put your hand on a hot stove for a minute, and it seems like an hour. Sit with a pretty girl for an hour, and it seems like a minute. THAT'S relativity." - Albert Einstein |
|
|
|
|
|
#6 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
In VB (not sure about VB .NET, but as you're not using that, it's irrelevant), if you want to redimension an array, you have to define it without any dimensions:
Dim primes() as Integer Then redimension it at the start. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|