Results 1 to 11 of 11

Thread: Constant or variable?

  1. #1

    Thread Starter
    Frenzied Member Blobby's Avatar
    Join Date
    Oct 2001
    Location
    England
    Posts
    1,512

    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:
    1. Private Sub Blur(intUpperBoundX, intUpperBoundY)
    2.     Dim Pixels(1 To intUpperBoundX, 1 To intUpperBoundY) As Long
    3.     Dim x, y As Integer
    4.     Dim bytRed, bytGreen, bytBlue As Byte
    5.    
    6.     For x = 1 To intUpperBoundX
    7.         For y = 1 To intUpperBoundY
    8.             Pixels(x, y) = Picture1.Point(x, y)
    9.         Next y
    10.     Next x
    11.    
    12.     For x = 1 To intUpperBoundX - 1
    13.         For y = 1 To intUpperBoundY
    14.             bytRed = Abs((Pixels(x + 1, y) And &HFF) + (Pixels(x, y) And &HFF)) / 2
    15.             bytGreen = Abs(((Pixels(x + 1, y) And &HFF00) / &H100) Mod &H100 + ((Pixels(x, y) And &HFF00) / &H100) Mod &H100) / 2
    16.             bytBlue = Abs(((Pixels(x + 1, y) And &HFF0000) / &H10000) Mod &H100 + ((Pixels(x, y) And &HFF0000) / &H10000) Mod &H100) / 2
    17.             Pixels(x, y) = RGB(bytRed, bytGreen, bytBlue)
    18.          Next y
    19.     Next x
    20.    
    21.    
    22.     For x = 1 To intUpperBoundX
    23.         For y = 1 To intUpperBoundY
    24.             Picture2.PSet (x, y), Pixels(x, y)
    25.         Next y
    26.     Next x
    27.    
    28. 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

  2. #2
    PowerPoster
    Join Date
    Aug 2000
    Location
    India
    Posts
    2,288
    You can't have constants on the fly.

  3. #3

    Thread Starter
    Frenzied Member Blobby's Avatar
    Join Date
    Oct 2001
    Location
    England
    Posts
    1,512
    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

  4. #4
    PowerPoster
    Join Date
    Aug 2000
    Location
    India
    Posts
    2,288
    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.

  5. #5

    Thread Starter
    Frenzied Member Blobby's Avatar
    Join Date
    Oct 2001
    Location
    England
    Posts
    1,512
    bugger
    There are 3 types of people in this world.........those that can count, and those that can't.

    Blobby

  6. #6
    PowerPoster
    Join Date
    Aug 2000
    Location
    India
    Posts
    2,288
    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

  7. #7
    New Member
    Join Date
    May 2001
    Location
    The Rock
    Posts
    13
    You need to use a dynamic array.
    Initialise the array with no bounds, and then ReDim it using the variables.

    VB Code:
    1. Dim Pixels() As Long
    2. 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!

  8. #8

    Thread Starter
    Frenzied Member Blobby's Avatar
    Join Date
    Oct 2001
    Location
    England
    Posts
    1,512
    Thanks for replying....my code now reads:

    VB Code:
    1. Dim Pixels()
    2.    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

  9. #9
    PowerPoster
    Join Date
    Aug 2000
    Location
    India
    Posts
    2,288
    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:
    1. Dim Pixels() As Long
    2. 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

  10. #10

    Thread Starter
    Frenzied Member Blobby's Avatar
    Join Date
    Oct 2001
    Location
    England
    Posts
    1,512
    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

  11. #11
    New Member
    Join Date
    May 2001
    Location
    The Rock
    Posts
    13
    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
  •  



Click Here to Expand Forum to Full Width