Solved

Redim of Public Array Within Class Results in Loss of Dimensions

Posted on 2013-06-20
4
451 Views
Last Modified: 2013-06-29
I am using Redim in a class on a two-dimensional array that is defined as public in a "declarations" module.  However, depending on where the program flow is after the Redim, there can be a loss of dimensions.  The pattern is that when I instantiate another object (class) within this class, then run the small process in that new object and come back the dimensions of the public array are lower.  Thus if the new object was never instantiated to go out and do work, the results are confirmed that loss of elements doesn't occur.

For my example, the Redim is A(20,1) but after using the instantiated class to do a small outside job (outside of this class we are in -->with the Redim) the dimensions are A(19,0).

There are rules for Redim based on where it is used, so my question is how can I retain declaring a public array in a declaration module, and then use Redim on those arrays within instantiated objects (classes) without losing dimensions?
0
Comment
Question by:lep1
4 Comments
 
LVL 25

Expert Comment

by:Luis Pérez
ID: 39262298
Can you post some piece of code?
0
 

Author Comment

by:lep1
ID: 39262373
Sure:

Module mymod()
    Public A(,) As single
End Module

In button 1:
Dim obj as New Test

Class Test
Sub New
     Call test()
End Sub

Sub test()
   Redim A(20,1)
   'Here is where A will have dimensions (20,1)
    Dim myobj as As New ClassB
   'Here is where A will have dimensions (19,0)
End Sub
End Class

Class ClassB
Sub New()
Msgbox("hello")
End Sub
End Class
0
 
LVL 40

Accepted Solution

by:
Vadim Rapp earned 500 total points
ID: 39287320
I recreated the code you posted, in VS2008, with some adjustments (below), but upper bound did not change.

Form:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim obj As New Test
    End Sub

    Class Test
        Sub New()
            Call test()
        End Sub

        Sub test()
            ReDim A(20, 1)
            'Here is where A will have dimensions (20,1)
            MsgBox(A.GetUpperBound(0))
            Dim myobj As New ClassB
            MsgBox(A.GetUpperBound(0))
            'Here is where A will have dimensions (19,0)
        End Sub
    End Class

    Class ClassB
        Sub New()
            MsgBox("hello")
        End Sub
    End Class

End Class

Open in new window


Module:
Module mymod
    Public A(,) As Single
End Module

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

809 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