Results 1 to 4 of 4

Thread: I just dont get it....please help

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2005
    Posts
    18

    I just dont get it....please help

    why doesnt this code work ??

    VB Code:
    1. Private Sub Command2_Click()
    2. Dim aa As Integer
    3. aa = 1
    4. Dim ad(aa) As String
    5. End Sub

    iam in a situation where i have to declare an array that has a size based on a variable....so if the variable value is 1 then the array declared will be of size 1 if the variable value is 2 then the array declared will be of size 2 and so on.....how would i go about achieving this task....any help will be greatly appreciated

  2. #2
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    Re: I just dont get it....please help

    No you can't.

    On second thought...

    VB Code:
    1. Private Sub Command1_Click()
    2. Dim aa As Integer
    3. aa = 1
    4. Dim ad() As String
    5.  
    6. ReDim ad(aa)
    7.  
    8. End Sub
    Last edited by zynder; Feb 16th, 2007 at 02:16 AM.

  3. #3
    New Member
    Join Date
    Feb 2007
    Posts
    13

    Re: I just dont get it....please help

    If you want to begin index from 1
    you could do as follows
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim aa As Integer
    3. aa = 1
    4. Dim ad() As String
    5.  
    6. ReDim ad(1 to aa)
    7.  
    8. End Sub

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: I just dont get it....please help

    The reason you can't do it your way is that Dim'ing a fixed-size array requires a constant for the array size (the number has to be known at compile time, so the compiler can allocate enough memory for the array). Since the value of a variable isn't known at compile time, even if it's only set once and never changed, it can't be used to set a fixed-size array.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

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