943,515 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Mar 29th, 2009
0

ReDim error!

Expand Post »
Hey guys thanks for viewing this post.

I have created an array to store usernames and passwords. Im tryning to get the array ReDim-ed mid program.

Here is the array:
Public Sub Usernames_Passwords()
    
'Assigns names to array space 1
        arrNames(0, 0) = "bnsjon"
        arrNames(1, 0) = "bnsjack"
        arrNames(2, 0) = "bnsjane"
        arrNames(3, 0) = "bnsjill"
        
'Assigns Passwords to array space 2
        arrNames(0, 1) = "jon"
        arrNames(1, 1) = "jack"
        arrNames(2, 1) = "jane"
        arrNames(3, 1) = "jill"
End Sub

This is the code I tried using to get the array ReDim-ed :
Private Sub Form_Load()
    Dim u As Integer
'Redims the array arrNames with new holding amount
'while preserving previous values
    u = 3
    u = u + 1
    ReDim Preserve arrNames(u, 1) As String
End Sub

Private Sub btnAdd_Click()
'Assigns new name and password to array
    arrNames(u, 0) = txtName.Text
    arrNames(u, 1) = txtPassword.Text
End Sub

But I get this error.
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Compile Error:
  2. Array already defined

And it highlights this line
Private Sub Form_Load()
    Dim u As Integer
'Redims the array arrNames with new holding amount
'while preserving previous values
    u = 3
    u = u + 1
    ReDim Preserve arrNames(u, 1) As StringEnd Sub

Any takers?
Thanks in advance

-Russell aka Rabbit
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
rabbithaveit is offline Offline
32 posts
since Mar 2009
Mar 29th, 2009
0

Re: ReDim error!

Unfortunately in 2D arrays redim is not working..
I recommend you to use two (one-dimensional) arrays one for usernames and the other for passwords.The arrays will be parraller.

You can use the redim - function on 1D array without error...
For exampe..
----------------------------------------------
dim usernames() as string
dim passwords() as string
dim rows as integer

rows=5

redim usernames(rows)
redim passwords(rows)

''Values assignment....
rows = rows +1

redim preserve usernames(rows)
redim preserve passwords(rows)

----------------------------------------------
I hope that i helped you...
KSS
Reputation Points: 11
Solved Threads: 3
Newbie Poster
KSS is offline Offline
21 posts
since Mar 2009
Mar 29th, 2009
0

Re: ReDim error!

Thanks for replying KSS

Thats a pitty though. Is it that it CANT be done? Or neither of us know how to? lol

And thanks for the advice. I was hoping I wouldnt have had to do so, but I'll have to impliment that.

Thanks again

-Russell aka Rabbit
Reputation Points: 10
Solved Threads: 0
Light Poster
rabbithaveit is offline Offline
32 posts
since Mar 2009
Mar 29th, 2009
0

Re: ReDim error!

Well.... I put them in two different arrays. Debugged the code so that the login screen works fine (Y)

And im still getting the same error.
This is what I have now :
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub Form_Load()
  2. Dim u As Integer
  3. 'Redims the array arrNames with new holding amount
  4. 'while preserving previous values
  5. u = 3
  6. u = u + 1
  7. ReDim Preserve arrNames(u)
  8. ReDim Preserve arrNamesP(u)
  9. End Sub

So im guessing its not that it cant be done. But we or I am just doing it wrong.
Any takers?

thanks in advance

-Russell aka Rabbit
Reputation Points: 10
Solved Threads: 0
Light Poster
rabbithaveit is offline Offline
32 posts
since Mar 2009
Mar 29th, 2009
0

Re: ReDim error!

Try the attachment file...
I just write it and it works fine...
Attached Files
File Type: zip Redim.zip (1.5 KB, 39 views)
KSS
Reputation Points: 11
Solved Threads: 3
Newbie Poster
KSS is offline Offline
21 posts
since Mar 2009
Mar 29th, 2009
0

Re: ReDim error!

Is it working?
KSS
Reputation Points: 11
Solved Threads: 3
Newbie Poster
KSS is offline Offline
21 posts
since Mar 2009
Mar 29th, 2009
0

Re: ReDim error!

Yes it is. Your solution worked perfectly

-Russell aka Rabbit
Reputation Points: 10
Solved Threads: 0
Light Poster
rabbithaveit is offline Offline
32 posts
since Mar 2009
Mar 29th, 2009
0

Re: ReDim error!

Oh woah woah. Wrong thread.
I'll try your ReDim zip now

-Russell aka Rabbit
Reputation Points: 10
Solved Threads: 0
Light Poster
rabbithaveit is offline Offline
32 posts
since Mar 2009
Mar 29th, 2009
0

Re: ReDim error!

KSS again. Thank you. I didnt know about list box till now O_O

Now that i've rummaged through your code, I understand how to use it. Thank you. I do beleive I can consider this solved as well.
2for you man lol. Your on a role

-Russell aka Rabbit
Reputation Points: 10
Solved Threads: 0
Light Poster
rabbithaveit is offline Offline
32 posts
since Mar 2009
Mar 29th, 2009
0

Re: ReDim error!

Hi Russel

Since the thread is already solved, I feel you need to also know why this happens... This is just for your FYI...

When using Preserve keyword, you can resize only the last array dimension. For example, if your array has only one dimension, you can resize that dimension because it is the last and only dimension. However, if your array has two or more dimensions, you can change the size of only the last dimension and still preserve the contents of the array.

For Example

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub Command1_Click()
  2. Dim MyArray() As String
  3. ReDim Preserve MyArray(1, 1) '<~~ YOU CAN DO THIS
  4. MyArray(1, 1) = "1. Blah Blah"
  5. ReDim Preserve MyArray(1, 2) '<~~ YOU CAN DO THIS
  6. MyArray(1, 2) = "2. Blah Blah"
  7. ReDim Preserve MyArray(1, 3) '<~~ YOU CAN DO THIS
  8. MyArray(1, 3) = "3. Blah Blah"
  9.  
  10. u = 2
  11. ReDim Preserve MyArray(u, 3) '<~~ YOU CANNOT DO THIS
  12. End Sub

An Alternative is

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub Command1_Click()
  2. Dim MyArray() As String, MyTempArray() As String
  3. ReDim Preserve MyArray(1, 1) '<~~ YOU CAN DO THIS
  4. MyArray(1, 1) = "1. Blah Blah"
  5. ReDim Preserve MyArray(1, 2) '<~~ YOU CAN DO THIS
  6. MyArray(1, 2) = "2. Blah Blah"
  7. ReDim Preserve MyArray(1, 3) '<~~ YOU CAN DO THIS
  8. MyArray(1, 3) = "3. Blah Blah"
  9.  
  10. u = 2
  11. ReDim Preserve MyArray(u, 3) '<~~ YOU CANNOT DO THIS
  12.  
  13. ReDim MyTempArray(u, 1)
  14. 'Transfer data from MyArray to this array
  15. End Sub
Last edited by koolsid; Mar 29th, 2009 at 7:32 pm.
Reputation Points: 11
Solved Threads: 6
Light Poster
koolsid is offline Offline
35 posts
since Feb 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Text box Validation
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Connect to mysql in another computer error(pls help)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC