|
-
Aug 30th, 2002, 08:35 AM
#1
Thread Starter
Frenzied Member
Constant or variable?
The following piece of code blurs a picture box but the compiler wont let me use a variable for the picture width or height, it only wants a constant. Can anyone tell me Why i cant use a variable (intUpperBoundY) in the DIM PIXELS line (1st line of code)? This value may change depending on the size of the picture I am blurring and so cant use a constant. Thanks for any pointers
VB Code:
Private Sub Blur(intUpperBoundX, intUpperBoundY)
Dim Pixels(1 To intUpperBoundX, 1 To intUpperBoundY) As Long
Dim x, y As Integer
Dim bytRed, bytGreen, bytBlue As Byte
For x = 1 To intUpperBoundX
For y = 1 To intUpperBoundY
Pixels(x, y) = Picture1.Point(x, y)
Next y
Next x
For x = 1 To intUpperBoundX - 1
For y = 1 To intUpperBoundY
bytRed = Abs((Pixels(x + 1, y) And &HFF) + (Pixels(x, y) And &HFF)) / 2
bytGreen = Abs(((Pixels(x + 1, y) And &HFF00) / &H100) Mod &H100 + ((Pixels(x, y) And &HFF00) / &H100) Mod &H100) / 2
bytBlue = Abs(((Pixels(x + 1, y) And &HFF0000) / &H10000) Mod &H100 + ((Pixels(x, y) And &HFF0000) / &H10000) Mod &H100) / 2
Pixels(x, y) = RGB(bytRed, bytGreen, bytBlue)
Next y
Next x
For x = 1 To intUpperBoundX
For y = 1 To intUpperBoundY
Picture2.PSet (x, y), Pixels(x, y)
Next y
Next x
End Sub
Is it possible to create a constant on the fly based on a variable?
There are 3 types of people in this world.........those that can count, and those that can't.
Blobby
-
Aug 30th, 2002, 08:40 AM
#2
PowerPoster
You can't have constants on the fly.
-
Aug 30th, 2002, 08:42 AM
#3
Thread Starter
Frenzied Member
I didnt think so.........so how do you create an array based on a variable number of elements??
There are 3 types of people in this world.........those that can count, and those that can't.
Blobby
-
Aug 30th, 2002, 08:45 AM
#4
PowerPoster
There is a Redim statement, but it might not not work in your case as it can only be used to modify single dimension of a multi-dimensional array.
-
Aug 30th, 2002, 08:46 AM
#5
Thread Starter
Frenzied Member
There are 3 types of people in this world.........those that can count, and those that can't.
Blobby
-
Aug 30th, 2002, 08:47 AM
#6
PowerPoster
from MSDN:
Visual Basic Reference Error Messages 'ReDim' can only change the right-most dimension A ReDim statement attempts to use the Preserve keyword to change a dimension of an array that is not the last dimension. When using Preserve, you can resize only the last dimension of an array, and for all
See this link
-
Aug 30th, 2002, 08:49 AM
#7
New Member
You need to use a dynamic array.
Initialise the array with no bounds, and then ReDim it using the variables.
VB Code:
Dim Pixels() As Long
ReDim Pixels(1 To intUpperBoundX, 1 To intUpperBoundY)
The problem is that when you Dim an array with bounds (eg Dim arr(1,2)), vb uses fixed size array. It knows the size of the array won't change, so it can allocate some memory for it.
A dynamic array (Dim arr()) looks similar to the programmer, but is managed differently and so can be resized at a later time using redim.
Hope this helps!
-
Aug 30th, 2002, 08:52 AM
#8
Thread Starter
Frenzied Member
Thanks for replying....my code now reads:
VB Code:
Dim Pixels()
ReDim Pixels(1 To PicWidth, 1 To picHeight) As Long
but the compiler says "Cant change datatypes of array elements"
any ideas?
There are 3 types of people in this world.........those that can count, and those that can't.
Blobby
-
Aug 30th, 2002, 08:53 AM
#9
PowerPoster
Originally posted by smokinjoe
You need to use a dynamic array.
Initialise the array with no bounds, and then ReDim it using the variables.
VB Code:
Dim Pixels() As Long
ReDim Pixels(1 To intUpperBoundX, 1 To intUpperBoundY)
The problem is that when you Dim an array with bounds (eg Dim arr(1,2)), vb uses fixed size array. It knows the size of the array won't change, so it can allocate some memory for it.
A dynamic array (Dim arr()) looks similar to the programmer, but is managed differently and so can be resized at a later time using redim.
Hope this helps!
smokinjoe I don't think this will work.
Take a look at thislink
-
Aug 30th, 2002, 08:54 AM
#10
Thread Starter
Frenzied Member
OOOPS...i forgot to put the AS LONG on the end of the first DIM!
Thanks Smokin.....
There are 3 types of people in this world.........those that can count, and those that can't.
Blobby
-
Aug 30th, 2002, 08:54 AM
#11
New Member
No probs Blobby.
amitabh - This link is for when you are using the "preserve" keyword. If you don't use it, then you can change any of the dimensions of the array.
Cheers.
Last edited by smokinjoe; Aug 30th, 2002 at 08:59 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|