Question (1)
I have declare a classA object in the main and want classB to use this object inside classB domain. may i know how to i go about doing it.
Open a new project. Remove the default form and add a Module (Module1) and two Class Modules(Class1 and Class2). In Class1 put this code
Code:
Option Explicit
'this class will take a reference of another class as an argument
Public Function TakeClass(ByRef anotherClass As Class2)
'here change the property of the argument
anotherClass.Name = "Changed in Class1"
End Function
In Class2, put this
Code:
Option Explicit
'property string
Private m_name As String
Public Property Get Name() As String
Name = m_name
End Property
Public Property Let Name(ByVal vNewValue As String)
m_name = vNewValue
End Property
Now in the Module(Module1) create a main function and initialize a two objects for Class1 and Class2, like this
Code:
Option Explicit
Sub main()
'initialize two classes
Dim aClass As New Class1
Dim bClass As New Class2
'set the property of another class
bClass.Name = "Original Name"
Debug.Print bClass.Name
'call the function of first class and pass the second class as argument
aClass.TakeClass bClass
'see the name was changed inside the function
Debug.Print bClass.Name
End Sub
The TakeClass function takes an argument of type Class1 and when you call the function it changes the property of Object aClass.
Question (2)
How do i pass an array to a function and let this function returns as an array.
Here is how to pass an array and return an array from a function.
Code:
Sub Main()
'pass array as arument
Dim sString(3) As String
sString(0) = "Hello"
sString(1) = "OneMoreHello"
sString(2) = "AnotherHello"
Dim sNewString() As String
'pass the array to a function and save the return value in an array
sNewString = takeArray(sString)
Debug.Print sNewString(0)
Debug.Print sNewString(1)
Debug.Print sNewString(2)
End Sub
'this function takes an array as argument and returns an array
'remember you cannot pass an array using ByVal
Public Function takeArray(ByRef arrArgument() As String) As String()
takeArray = arrArgument
End Function
Hope this explains some stuff.
Edit--
Remember Inheritence is not supported in VB 6.0