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.
Question (2)
How do i pass an array to a function and let this function returns as an array.
Printable View
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.
Question (2)
How do i pass an array to a function and let this function returns as an array.
Open a new project. Remove the default form and add a Module (Module1) and two Class Modules(Class1 and Class2). In Class1 put this codeQuote:
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.
In Class2, put thisCode: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
Now in the Module(Module1) create a main function and initialize a two objects for Class1 and Class2, like thisCode: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
The TakeClass function takes an argument of type Class1 and when you call the function it changes the property of Object aClass.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
Here is how to pass an array and return an array from a function.Quote:
Question (2)
How do i pass an array to a function and let this function returns as an array.
Hope this explains some stuff.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
Edit--
Remember Inheritence is not supported in VB 6.0
@Shuja: ???
@jason:
Solution 1
in class b:
private mClassA as classA
public propery get cClassA() as ClassA
set cClassA = mClassA
end property
Public property set cClassA(byval vdata as classA) ' maybe byref
set mClassA = vdata
end property
in code:
dim myclassB as ClassB
dim myclass A as ClassA
'... set etc ...
set myclassb.cClassA = myclassA
solution 2: Read about implements