Copy Class to Variable But...

In VB 6 is it possible to do the follow:

Dim myAIArray(100) as New AIDetails
Dim currentLevel as variant
...
Some other code
...

Private Sub Form_Load()

currentLevel = myAIArray

End Sub

Everything works fine except that nothing changes in myAIArray only in currentLevel.

I know (or i think) that in C++ when you do something like this its all to do with pointers thus what you do to currentLevel happens to myAIArray as they are using the same address space basically.

Is this possible in VB6, and if so how?

Thanks

Colin
ezekial316Asked:
Who is Participating?
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
Alrighty then...I was wrong.  Sorry Greg!

This worked for me.  I had to iterate thru and assign something to each of my classes in the array before it would work:

' --------------------------
' Class AIDetails
' --------------------------
Public s As String

' --------------------------
' Form1
' --------------------------
Option Explicit

Dim myAIArray(100) As New AIDetails
Dim currentLevel As Variant

Private Sub Form_Load()
    Dim i As Integer
   
    For i = LBound(myAIArray) To UBound(myAIArray)
        myAIArray(i).s = "Hello World"
    Next i
   
    currentLevel = myAIArray
    currentLevel(0).s = "Goodbye World"

    Debug.Print "currentLevel(0).s = " & currentLevel(0).s
    Debug.Print "myAIArray(0).s = " & myAIArray(0).s
    Debug.Print "currentLevel(1).s = " & currentLevel(1).s
    Debug.Print "myAIArray(1).s = " & myAIArray(1).s
End Sub

0
 
gregoryyoungCommented:
Dim myAIArray(100) as New AIDetails
Dim currentLevel() as AIDetails
...
Some other code
...

Private Sub Form_Load()

currentLevel = myAIArray

End Sub


its using a reference ... and it is strongly typed therefore not late binding as in your vb6 example.

currentLevel and myAIArray point to the same array in memory.

Cheers,

Greg

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
>> Is this possible in VB6

Pretty sure that is NOT possible in VB6.

It will work in VB.Net however...
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I also had to declare currentLevel as a Variant.
0
 
gregoryyoungCommented:
umm of course you had to initialize them ... when you do an array it only creates the REFERENCES to the objects (all of them are null aka nothing) you need to assign objects to the references.

Public Class fubar

    Public Name As String
    Public value As Int16

    Public Overrides Function ToString() As String
        Return Me.Name & " = " & Me.value
    End Function
End Class

Module Module1
    Sub CreateArray(ByVal arr As fubar())
        Dim i As Integer
        For i = 0 To arr.Length - 1
            arr(i) = New fubar
            arr(i).Name = "Fubar " + i.ToString()
            arr(i).value = i
        Next
    End Sub
    Sub PrintArray(ByVal arr As fubar())
        Dim i As Integer
        For i = 0 To arr.Length - 1
            Console.WriteLine(arr(i).ToString())
        Next
    End Sub
    Sub Main()
        Dim t(5) As fubar
        Dim t2() As fubar

        CreateArray(t)
        Console.WriteLine("t")
        PrintArray(t)
        t2 = t
        Console.WriteLine("Reverse t2")
        Array.Reverse(t2)
        Console.WriteLine("t")
        PrintArray(t)
    End Sub
End Module
0
 
gregoryyoungCommented:
btw I think he actually meant is it possible in VB.NET (he starts off saying it is possible in VB6 :) )

Greg
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
LOL....he didn't say it is possible in VB6.

He said:

    "In VB 6 is it possible to do the follow:"

NOT..

    In VB 6 it is possible to do the follow:

Notice the reversal of "it" and "is"...



ezekial316,

Please clarify what target language you need the solution in!

~IM
0
 
ezekial316Author Commented:
Thanks to you all

target language is VB6

i think i was falling over by not initialising the arrays.  its now working a treat due to IdleMinds code :-)

I love you all (but i think thats the hayfever medicine and drowsiness talking)

Colin

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.