Solved

Objects as Parameters

Posted on 2006-06-30
6
200 Views
Last Modified: 2013-11-23
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
Comment
Question by:deatc
  • 3
  • 2
6 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17020680
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
 
LVL 37

Accepted Solution

by:
Harisha M G earned 125 total points
ID: 17020694
Hi, try changing Let to Set

---
Harish
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17020710
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17020722
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
 
LVL 37

Expert Comment

by:Harisha M G
ID: 17020757
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
 
LVL 37

Expert Comment

by:Harisha M G
ID: 17020775
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

778 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