Results 1 to 4 of 4

Thread: nubie: constants

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2000
    Location
    Leeds
    Posts
    30

    Post

    i would like to set the limit for an array that i'm creating to a value that is not a const,

    Dim Ordered(sqlcol) As Integer

    but it won't compile due to the fact that sqlcol is not a constant value, have do i get this to work, any help is much appreciated.

    Piers

  2. #2
    New Member
    Join Date
    Mar 2000
    Location
    Hull, PQ, Canada
    Posts
    7

    Post It won't work the way you have it set up...

    Unfortunately, I do not believe it is possible to delare an array using a variable to set the size. It's just something VB (or any other programming language to my knowledge) dosen't allow. You either use constants, which cannot be changed, or you use actual numbers. From what I see, you seem to be trying to create a dynamic array - one that will take on a different size according to the situation. If this is the case, I strongly suggest you take a look at collections. You say you are a newbie (which is fine - we were all there once ) so I don't know if you feel ready to work with objects yet, but that would definitely be the way I would do it. A collection is a dynamic array of objects, and is much more powerful (IMHO) than a standard Dimmed array.

    Hope this helps!

    JFDman


  3. #3
    Lively Member
    Join Date
    Mar 2000
    Posts
    82

    Post

    dim sqlcol as long
    reDim Ordered(sqlcol) As Integer


  4. #4
    Guest

    Post

    vb absolutely allows the use of a variable to declare the size. try this, start a new standard exe, put one command button on Form1 named Command1 and cut and paste the following code into Form1's code module. Run the project and put in a number for whatever size array you want and it will create an array that size, fill it with random numbers and spit the contents out to the immediate window:

    Code:
    
    Option Explicit
    Dim aryNumbers() As Single
    
    Private Sub LoadDynamicArray()
        Dim strUbound As String
        Dim intUbound As Integer
        Dim intCount As Integer
        
        strUbound = InputBox("Enter upper bound of your array", "UPPER BOUND", 10)
        
        If Trim$(strUbound) = "" Or IsNumeric(Trim$(strUbound)) = False Then Exit Sub
                
        intUbound = CInt(strUbound)
        ReDim aryNumbers(intUbound)
        Debug.Print "Upper bound of the array is " & UBound(aryNumbers)
        Randomize
        For intCount = 0 To intUbound - 1
            aryNumbers(intCount) = (Round((intUbound * Rnd)) + 1) * (Round((intUbound * Rnd)) + 1)
            Debug.Print "Array Element " & intCount & " contains " & aryNumbers(intCount)
        Next
    End Sub
    
    Private Sub Command1_Click()
        LoadDynamicArray
    End Sub
    Edited by pvb on 03-10-2000 at 12:39 PM

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