
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