Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Redim of Public Array Within Class Results in Loss of Dimensions

Posted on 2013-06-20
4
Medium Priority
?
460 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 1500 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

972 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