variable array

How do I declare (and use) a variable array in vb.net?

It seems I have problems with code that worked in vb6:

Dim myStringArray() as string

ReDim myStringArray(whateverObject.Count)

Or, in a more 'real-world' case...
Dim strAccounts as string
[...]
ReDim strAccounts(myAdoRS.RecordCount)
LVL 67
sirbountyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi sirbounty;

ReDim requires an array which the following statement is not

        Dim strAccounts As String

so then this statement will fail

        ReDim strAccounts(myAdoRS.RecordCount)

On the other hand this is fine as long as you do not need the data that was already in the array if any.

        Dim myStringArray() as string

        ReDim myStringArray(whateverObject.Count)

If you don't want to lose the data then use this form:

        Dim myStringArray() as string

        ReDim Preserve myStringArray(whateverObject.Count)

Also this should return an integer.

Fernando
sirbountyAuthor Commented:
Well, like I'm using this in a module: (this is 'upgraded' via the wizard code from vb6):

Public Function FindWindowLike(ByRef hWndArray() As Integer, ByVal hWndStart As Integer, ByRef WindowText As String, ByRef Classname As String) As Integer

and in my form...
        Dim hWnds() As Integer
        Dim cnt As Integer
        cnt = FindWindowLike(hWnds, 0, "1.3*", "*")

and I get a green squiggley under hWnds:
Variable hWnds is passed by reference before it has been assigned a value.  A null reference exception could result at run time.

Do I care?   I'm just trying to find my window... :^)
Fernando SotoRetiredCommented:
Hi sirbounty;

Well passing it a null reference will most likely throw an error. I would initialize it with the maximum number of elements that you would expect.

        Dim hWnds() As Integer = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

Fernando

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.