views:

1165

answers:

3

The following code kills VB6 (sp6) with an 'unhandled exception fault in VB.exe' on two machines in the office on the line marked.

''# Form1.frm
Option Explicit
Private ArrayHolder As Class2

Private Sub Command1_Click()
    Set ArrayHolder = New Class2

    Dim arr(3) As Long
    arr(0) = 1
    arr(1) = 2
    arr(2) = 3

    ArrayHolder.Add arr

End Sub

''# -----------------------------------------------------------    
''# Class1.cls
Option Explicit

Private m_myArray() As Long

Public Property Get myArray() As Long()
    myArray = m_myArray
End Property

Friend Property Let myArray(ByRef anArray() As Long)
    m_myArray = anArray
End Property

''# -----------------------------------------------------------    
''# Class2.cls
Option Explicit

Friend Function Add(newArray() As Long) As Class1
    Dim oClass As Class1
    Set oClass = New Class1

    oClass.myArray = newArray  <- This kills VB6 dead
    MsgBox "passed"

End Function

From what I can tell on various websites I am passing an array correctly but am I actually doing it correctly, and why is it causing VB6 to die so horribly?

+3  A: 

I don't have an answer, and certainly is a curious question, but why don't just add a method and move on?

'Passed the test
Public Sub LetMyArray(anArray() As Long)
    m_myArray = anArray
End Sub


'oClass.MyArray = newArray ' <- This kills VB6 dead
oClass.LetMyArray newArray  ' <- This works
Eduardo Molteni
Curiously it does work.Well I was working on making it a collection, but updating all the places it is used was proving to be a pain.
graham.reeds
I think you just discovered a bug in the VB runtime. But we will never see SP7 :)
Eduardo Molteni
+1 for pragmatism! And, perhaps sadly, for being correct about SP7. The extended support for the runtime does not include service packs (I seem to remember)
MarkJ
@MarkJ, fancy seeing you here on a VB post, lol :)
Erx_VB.NExT.Coder
@Erx_VB I admit it is [arguable](http://stackoverflow.com/tags/vb6/topusers) that I should spend more time in the "real world"! :)
MarkJ
@markj Wow I am impressed I must say, nice stats! Apparently I am the second highest of those who "ask" lol and am lucky to have people like yourself to answer :)
Erx_VB.NExT.Coder
+1  A: 

I have no idea why this happens, but if you anyway want a copy of an array, use a Variant in class2:

Private m_myArray As Variant

Public Property Get myArray() As Variant
    myArray = m_myArray
End Property

Friend Property Let myArray(anArray As Variant)
    m_myArray = anArray
End Property

Fixes it, but still, I'm curious about the reason.

GSerg
When I tried a variant, it still crashed. Maybe I did something wrong?!
graham.reeds
Maybe you just did what I said and replaced contents of Class2 with this code. :) Of course, I meant to say Class1. Works fine for me.
GSerg
Still crashes in my machine
Eduardo Molteni
+2  A: 

This is a bug in the IDE (compiled is ok) that MS never fixed. I'm using a workaround with a temp array like this:

Friend Function Add(newArray() As Long) As Class1
    Dim oClass As Class1
    Dim tempArray() As Long
    Set oClass = New Class1

    tempArray = newArray
    oClass.myArray = tempArray <- Should work now
    MsgBox "passed"

End Function

FYI, it gets worse with Byte arrays (Long arrays are safe) when you accidentally hover with the mouse over the param or the array property. Better keep your mouse away from the code :-))

cheers,
</wqw>

wqw