Solved

Objects as Parameters

Posted on 2006-06-30
6
198 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now