Link to home
Start Free TrialLog in
Avatar of sirbounty
sirbountyFlag for United States of America

asked on

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)
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

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
Avatar of sirbounty

ASKER

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... :^)
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial