ReDim error!

Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2009
Posts: 31
Reputation: rabbithaveit is an unknown quantity at this point 
Solved Threads: 0
rabbithaveit's Avatar
rabbithaveit rabbithaveit is offline Offline
Light Poster

ReDim error!

 
0
  #1
Mar 29th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 10
Reputation: KSS is an unknown quantity at this point 
Solved Threads: 1
KSS's Avatar
KSS KSS is offline Offline
Newbie Poster

Re: ReDim error!

 
0
  #2
Mar 29th, 2009
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...
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 31
Reputation: rabbithaveit is an unknown quantity at this point 
Solved Threads: 0
rabbithaveit's Avatar
rabbithaveit rabbithaveit is offline Offline
Light Poster

Re: ReDim error!

 
0
  #3
Mar 29th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 31
Reputation: rabbithaveit is an unknown quantity at this point 
Solved Threads: 0
rabbithaveit's Avatar
rabbithaveit rabbithaveit is offline Offline
Light Poster

Re: ReDim error!

 
0
  #4
Mar 29th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 10
Reputation: KSS is an unknown quantity at this point 
Solved Threads: 1
KSS's Avatar
KSS KSS is offline Offline
Newbie Poster

Re: ReDim error!

 
0
  #5
Mar 29th, 2009
Try the attachment file...
I just write it and it works fine...
Attached Files
File Type: zip Redim.zip (1.5 KB, 3 views)
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 10
Reputation: KSS is an unknown quantity at this point 
Solved Threads: 1
KSS's Avatar
KSS KSS is offline Offline
Newbie Poster

Re: ReDim error!

 
0
  #6
Mar 29th, 2009
Is it working?
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 31
Reputation: rabbithaveit is an unknown quantity at this point 
Solved Threads: 0
rabbithaveit's Avatar
rabbithaveit rabbithaveit is offline Offline
Light Poster

Re: ReDim error!

 
0
  #7
Mar 29th, 2009
Yes it is. Your solution worked perfectly

-Russell aka Rabbit
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 31
Reputation: rabbithaveit is an unknown quantity at this point 
Solved Threads: 0
rabbithaveit's Avatar
rabbithaveit rabbithaveit is offline Offline
Light Poster

Re: ReDim error!

 
0
  #8
Mar 29th, 2009
Oh woah woah. Wrong thread.
I'll try your ReDim zip now

-Russell aka Rabbit
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 31
Reputation: rabbithaveit is an unknown quantity at this point 
Solved Threads: 0
rabbithaveit's Avatar
rabbithaveit rabbithaveit is offline Offline
Light Poster

Re: ReDim error!

 
0
  #9
Mar 29th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 35
Reputation: koolsid is an unknown quantity at this point 
Solved Threads: 6
koolsid's Avatar
koolsid koolsid is offline Offline
Light Poster

Re: ReDim error!

 
0
  #10
Mar 29th, 2009
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.
A good excercise for the Heart is to bend down and help another up...

Please Mark your Thread "Solved", if the query is solved...

==>If a post has helped you then Please Rate it!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum


Views: 1279 | Replies: 20
Thread Tools Search this Thread



Tag cloud for Visual Basic 4 / 5 / 6
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC