Determine Obj Name on Class Initialisation 
Author Message
 Determine Obj Name on Class Initialisation

Hi,

I'm trying to determine the name of an object during the
initialise event when a new class is instantiated.

The code looks something like this:

'From the general module I execute this line of code....
Dim ClassTest as New MyClass

'...which branches to the Class module
Private Sub Class_Initialize()
'...my problem is here, how do I obtain the class name to
'store in the Name property.

MsgBox Me.Name

'Should display: ClassTest

Thanks in advance

.



Sun, 11 Dec 2005 09:40:12 GMT  
 Determine Obj Name on Class Initialisation


Quote:
> I'm trying to determine the name of an object during the
> initialise event when a new class is instantiated.

The neat thing about VB is that it doesn't know the names of its own
variables: which is just as well since they are of no interest to anybody
but the programmer. In any case, the same object might be (probably will
be) referred to by several variables, so it could not possibly know what
you want back.

Quote:
> 'From the general module I execute this line of code....
> Dim ClassTest as New MyClass

> '...which branches to the Class module
> Private Sub Class_Initialize()
> '...my problem is here, how do I obtain the class name to
> 'store in the Name property.

Well, the class name is "MyClass" -- you should be able to get this from
TypeName("Me"). If, however, you want the name of the variable "ClassTest",
then you can't have it.

Quote:
> MsgBox Me.Name

Well, you have actually given your own solution here. You need a Class
Member called Name and set it from the calling code.

  [Class MyClass code]

  Dim m_txtName as String

  Public Property Let Name (NameText As String)
    m_txtName = NameText
  End Property
  Public Property Get Name () As String
    Name = m_txtName
  End Property

  Public Sub ShowName
    MsgBox Me.Name ' MsgBox m_txtName is functionally the same
  End Sub

  [some other module code]

  Set mcClassTest = new MyClass
  mcClassTest.Name = "Eric"

  mcClassTest.ShowName

Unfortunately, VBA does not offer parameters to the builder function, so
you can't do what you can in C++

  Set mcClassTest = New MyClass("Eric")

so you have to call the properties manually.

Hope that helps

Tim F



Mon, 12 Dec 2005 01:55:47 GMT  
 Determine Obj Name on Class Initialisation
Thanks Tim,

I've pretty much tried what you've suggested, I had also
tried passing a name parameter to the class:

Set mcClassTest = New MyClass("Eric")

but as you have said, it's not going to work.

You've at least confirmed my thinking.

Regards,
Peter L.

Quote:
>-----Original Message-----


>> I'm trying to determine the name of an object during
the
>> initialise event when a new class is instantiated.

>The neat thing about VB is that it doesn't know the names
of its own
>variables: which is just as well since they are of no

interest to anybody
Quote:
>but the programmer. In any case, the same object might be
(probably will
>be) referred to by several variables, so it could not
possibly know what
>you want back.

>> 'From the general module I execute this line of code....
>> Dim ClassTest as New MyClass

>> '...which branches to the Class module
>> Private Sub Class_Initialize()
>> '...my problem is here, how do I obtain the class name
to
>> 'store in the Name property.

>Well, the class name is "MyClass" -- you should be able
to get this from
>TypeName("Me"). If, however, you want the name of the

variable "ClassTest",

- Show quoted text -

Quote:
>then you can't have it.

>> MsgBox Me.Name

>Well, you have actually given your own solution here. You
need a Class
>Member called Name and set it from the calling code.

>  [Class MyClass code]

>  Dim m_txtName as String

>  Public Property Let Name (NameText As String)
>    m_txtName = NameText
>  End Property
>  Public Property Get Name () As String
>    Name = m_txtName
>  End Property

>  Public Sub ShowName
>    MsgBox Me.Name ' MsgBox m_txtName is functionally the
same
>  End Sub

>  [some other module code]

>  Set mcClassTest = new MyClass
>  mcClassTest.Name = "Eric"

>  mcClassTest.ShowName

>Unfortunately, VBA does not offer parameters to the

builder function, so

- Show quoted text -

Quote:
>you can't do what you can in C++

>  Set mcClassTest = New MyClass("Eric")

>so you have to call the properties manually.

>Hope that helps

>Tim F

>.



Mon, 12 Dec 2005 06:15:43 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Dim obj As New Class crt Dim Obj As Class = New Class

2. Delayed initialisation in derived class, bug?

3. Class Initialisation problem

4. Class Initialisation problem

5. Programmatically determine class name

6. cant create obj error when calling a function in same class

7. Obj Ref not set to an instance of an Obj

8. CR Engine Obj Lib vs CR Designer Component Obj Model

9. COM+ obj calling another COM+ obj

10. instance a class from its class name

11. File name or class name not found during Automation operation

12. Project-Name, dll-Name & Classes

 

 
Powered by phpBB® Forum Software