Solved

Objects as Parameters

Posted on 2006-06-30
6
206 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 143

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 143

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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month4 days, 8 hours left to enroll

635 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