Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 208
  • Last Modified:

Objects as Parameters

Hi.  I have a question about passing object as parameters in Visual Basic 6.  Suppose that I have a class module named CPerson that consists of the following code:

    Private m_strName As String
    Public Property Get Name() As String
        Name = m_strName
    End Property
    Public Property Let Name(ByVal strValue As String)
        m_strName = strValue
    End Property

Furthermore, suppose that I have a class module named CGroup that consists of the follow code:

    Private m_objLeader As CPerson
    Public Property Get Leader() As CPerson
        Set Leader = m_objLeader
    End Property
    Public Property Let Leader(ByRef objValue As CPerson)
        Set m_objLeader = objValue
    End Property

When I run the following code:

    Dim objGroup As New CGroup
    Dim objPerson As New CPerson
    objPerson.Name = "Chris"
    Set objGroup.Leader = objPerson

I get the following error message:

    Run-time error '91':
    Object variable or With block variable not set

Could you please tell me why that I am getting this error message?  How do you pass objects as parameters in Visual Basic 6?  Thank you.
0
deatc
Asked:
deatc
  • 3
  • 2
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
the code looks fine so far (except that I would not name a property NAME, as that is a reserved word).

the first step to help us would be to tell us on which line the error occurs.
0
 
Harisha M GCommented:
Hi, try changing Let to Set

---
Harish
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
good catch. to make it clear (points for mgh_mgharish:

Public Property Set Leader(ByRef objValue As CPerson)
        Set m_objLeader = objValue
    End Property
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Carl TawnSystems and Integration DeveloperCommented:
As mgh_mgharish has mentioned, you need to change:

    Public Property Let Leader(ByRef objValue As CPerson)
        Set m_objLeader = objValue
    End Property

To:

    Public Property Set Leader(ByRef objValue As CPerson)
        Set m_objLeader = objValue
    End Property

Let is used for properties of Value type, whereas Set is used for properties that are objects.
0
 
Harisha M GCommented:
deatc, In the class CPerson, "Name" is a "String", which is a basic data type. So, you should use "Let"

But in the case of CGroup, "Leader" is an "CPerson Object" (it may be built-in object or user defined object), which is not a basic data type. So, you must use "Set" in this case.
0
 
Harisha M GCommented:
deatc, also a very important point to keep in mind...

When assigning values to basic data types, you shouldn't use "Set". So,
objPerson.Name = "Chris"
is correct and
Set objPerson.Name = "Chris"
would be wrong.

Similarly, while assigning to an object, you must use "Set"

So,
Set objGroup.Leader = objPerson
is correct, and
objGroup.Leader = objPerson
would be wrong.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now