CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2005
    Posts
    25

    Question pass a class object into a class

    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.

  2. #2
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: pass a class object into a class

    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
    Last edited by Shuja Ali; October 25th, 2005 at 06:04 AM.

  3. #3
    Join Date
    May 2001
    Posts
    91

    Re: pass a class object into a class

    @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
    have a nice day

    Patzer
    _____________________________
    Philo will never be forgotten

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured