Looks to me like you want to build some kind of factory function that will create a singleton instance of your class (or return the instance if it already exists).
The code you currently have will create a new object every time you call the function Profile
, because IsEmpty(Profile)
interprets Profile
as a local variable, which is always empty, because at that point it's not yet initialized in the context of the function. It's a good thing too that VBScript interprets Profile
in that expression as a variable, because otherwise you'd have an infinite recursion since every function call would invoke itself again before it could get to the point where it returned something.
To be able to build a "singleton factory" you first need a global variable to hold the singleton object. You also need to use different names for variable and function.
Dim profile
Function GetProfile
If IsEmpty(profile) Then
Set profile = New objectProfile
End If
Set GetProfile = profile
End Function
Usually a "singleton factory" is rather pointless, though. If you know you need a singleton instance of something just create a global instance at the beginning of your script and use that instance throughout the rest of it.
Set Profile = New objectProfile
Set Test1 = Profile
Test1.FirstName = "MyFN"
MsgBox Test1.FirstName
Set Test2 = Profile
Test2.Lastname = "MyLast"
MsgBox Test2.Lastname
MsgBox Test2.FirstName
The above puts references to the original Profile
object into the variables Test1
and Test2
(as opposed to creating a copy of the object). Because of that every change you make to the properties of one variable will automatically be reflected in the other variables.
If you don't need a singleton instance just use a regular factory method:
Function CreateProfile(first, last)
Set profile = New objectProfile
If Not IsEmpty(first) Then profile.FirstName = first
If Not IsEmpty(last) Then profile.LastName = last
'...
'further initialization/customization
'...
Set CreateProfile = profile
End Function
Set Test1 = CreateProfile("Joe", Empty) 'first instance (only first name)
Set Test2 = CreateProfile(Empty, "Bar") 'second instance (only last name)
Set Test3 = CreateProfile("Joe", "Bar") 'third instance (both)
or simply create the objects inline:
Set Test1 = New objectProfile 'first instance
Test1.FirstName = "MyFN"
Set Test2 = New objectProfile 'second instance
Test2.Lastname = "MyLast"