[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Copy Class to Variable But...

Posted on 2005-05-13
8
Medium Priority
?
234 Views
Last Modified: 2010-04-17
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
0
Comment
Question by:ezekial316
  • 4
  • 3
8 Comments
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 14000545
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 14000650
>> Is this possible in VB6

Pretty sure that is NOT possible in VB6.

It will work in VB.Net however...
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 14000721
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
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.

 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 14000724
I also had to declare currentLevel as a Variant.
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 14000807
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
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 14000816
btw I think he actually meant is it possible in VB.NET (he starts off saying it is possible in VB6 :) )

Greg
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 14001089
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
 

Author Comment

by:ezekial316
ID: 14001415
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

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

867 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