• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 405
  • Last Modified:

Object property's values disappear.

Sub main initializes the following

Sub Main ()
    Dim Mkt  As MarketData
    Set Mkt = New MarketData
    ...

The Class_Initailize() procedure in class MarketData populates a set of arrays from Excel ranges.  I placed a breakpoint in the Class_Initialize procedure to confirm that the arrays populate.  They do and I can see the values in the Watch Window.

If I place a break point within Sub Main() anywhere after the initalization of the MarketData object Mkt and run the program to the breakpoint, the values initialized in Mkt.Class_Initialize disappear.  This then generates a "Subscript out of range" error the first time the program references one of the Mkt object's arrays.

Bewildered.

0
AlphaSquared
Asked:
AlphaSquared
1 Solution
 
mladenoviczCommented:
Do you have Redim somewhere in code. It can change your array
0
 
Michael_DCommented:
post your code
0
 
jkorzCommented:
put a new watch on Mkt and select "Break when value changes" that will pinpoint your problem line
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mladenoviczCommented:
Here is a simple sample that works

Public Sub Main()
    Dim a As Class1
    Set a = New Class1
    MsgBox a.SomeItem(4)
End Sub


Class

Private m_Arr() As Integer

Private Sub Class_Initialize()
    ReDim m_Arr(5)
    m_Arr(1) = 8
    m_Arr(2) = 18
    m_Arr(3) = 28
    m_Arr(4) = 438
    m_Arr(5) = 558
End Sub

Public Property Get SomeItem(ind As Integer) As Integer
    SomeItem = m_Arr(ind)
End Property
0
 
AlphaSquaredAuthor Commented:
No ReDim of any array in the MarketData class anywhere in the code of any module.

I places a new watch on Mkt and selected "Break when value changes"  It does not break before the program the encounters the error statement.

Relevant code follows (breakpoints indicated as comments and numbered in sequence of their occurrence in the execution of the program):

class MarkeData code:
-----------------------------------------------------
Option Explicit
Private op() As Variant    
...

Public Property Let O(ByVal i As Integer, Ovalue As Variant)
    op(i) = Ovalue
End Property

Public Property Get O(ByVal i As Integer) As Variant
    O = op(i)
End Property
 ...

Private Sub Class_Initialize()
    ...
    op() = Range("openPrices")
    hi() = Range("highPrices")                             'breakpoint2
    ...
End Sub
-----------------------------------------------------
Sub Main code:

Sub Main ()
    Dim testMktOArray As Variant
    Dim Mkt  As MarketData
    Set Mkt = New MarketData                           'breakpoint1  next break in class module (above)
    ...
    testMktOArray = Mkt.O(2)                            'breakpoint3
    ...
-----------------------------------------------------

re: the breakpoints indicated above, at:

breakpoint1: as expected the watch on  arrays:
    op() = <Out of context>
    Mkt.O() = <Expression not defined in context>

breakpoint2: the Mkt object gets created and the Class_Initialize() procedure runs and as expected op() has a fully populated variant array of some 3619 values.  So far so good.

breakpoint3: at this point I get bewildered, the watch values show...
    op()           <Out of context>
    Mkt.O(2)    <Subscript out of range>
    Mkt.O()      <Expression not defined in context>
    Mkt            This object indeed has all of its expected values

Wierd to me.  Do I have a problem with:

Public Property Get O(ByVal i As Integer) As Variant
    O = op(i)
End Property

??????

Something else?

0
 
mladenoviczCommented:
Try to rewrite data from range object to an array
0
 
mladenoviczCommented:
Try sthg like this (pseudo code)

redim op(range_size)
for i = 1 to range_size
   op(i) = range_element(i)
next
0
 
AlphaSquaredAuthor Commented:
Solved!  Thanks.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now