Solved

Dynamic Array in VB 2008

Posted on 2010-11-18
5
779 Views
Last Modified: 2012-05-10
This works in VB 6:

Partner() As String

Sub ReDimArray()
    ReDim Partner(0) As String
    Partner(0) = "Jaes"
    MsgBox Partner(0)  '<-- returns "Jaes"

    ReDim Preserve Partner(1) As String
    Partner(1) = "Mike"   '<-- returns "Jaes"

'etc
End Sub

How is this done in VB 2008

Thank you.
0
Comment
Question by:Mike Eghtebas
5 Comments
 
LVL 7

Expert Comment

by:EYoung
Comment Utility
0
 
LVL 17

Expert Comment

by:Zhaolai
Comment Utility
This works in vb 2008:
Dim Partner() As String



    Sub ReDimArray()

        ReDim Partner(0)

        Partner(0) = "Jaes"

        MsgBox(Partner(0))  '<-- returns "Jaes"



        ReDim Preserve Partner(1)

        Partner(1) = "Mike"   '<-- returns "Jaes"



        'etc

    End Sub

Open in new window

0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
Comment Utility
You could use essentially the same code...but why would you want to?

Use a List(Of String) instead:

    Dim Partner As New List(Of String)

    ...
        Partner.Add("Jaes")
        Partner.Add("Mike")

You can access it like a traditional array:

    MessageBox.Show(Partner(0)) ' <-- "Jaes"

...and it GROWS automatically to accommodate the entries you add.

You can even remove from the middle and everything shifts down to fill the "hole".

See:
http://msdn.microsoft.com/en-us/library/6sh2ey19(VS.80).aspx
0
 
LVL 33

Author Comment

by:Mike Eghtebas
Comment Utility
Hi Idle_Mind,

Your solution is the best practice and very much appreciated.

Hi Zhaolai,

Thank you for your post. I was looking for 2008-specific solution like what Idle_Mind has posted.

Thank you
0
 
LVL 33

Author Comment

by:Mike Eghtebas
Comment Utility
EYoung,

My apology for not paying a good attention of your post. I will request to reopen the question and give your fair share.

Thank you,

Mike
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now