0

I'm trying to write VBScript as follows:

Set Test1 = Profile
Test1.FirstName = "MyFN"
MsgBox Test1.FirstName
Set Test2 = Profile
Test2.Lastname = "MyLast"
MsgBox Test2.Lastname
MsgBox Test2.FristName

Public Function Profile()
    If IsEmpty(Profile) Then
        Set Profile = New objectProfile
    End If
End Function

Class objectProfile
    Private mFirstname, mLastname 
    Private Sub Class_Initialize()
        mFirstname = ""
    End Sub

    Public Property Let FirstName(strFirstName)
        mFirstname = strFirstName
    End Property

    Public Property Get FirstName()
        FirstName = mFirstname
    End Property

    Public Property Let Lastname(strLastname)
        mLastname = strLastname
    End Property

    Public Property Get Lastname()
        Lastname = mLastname 
    End Property
End Class

I set FirstName="MyFN". Why does Test2.FirstName return ""?

The Problem is that IsEmpty(Profile) is not working.

What's the proper way to check object does not exists in VBScript?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Bell Aimsaard
  • 483
  • 3
  • 5
  • 16
  • Possible duplicate of [Check if an Object exists in VBScript](http://stackoverflow.com/questions/4100506/check-if-an-object-exists-in-vbscript) – user692942 Dec 11 '16 at 08:39
  • I Fail to see what you are trying to accomplish with this code? At the moment checking `Profile` will always return `Nothing` as it's never set, you've created an inifinite loop of `Nothing` no object instances are ever set. If you want to check a particular instance is set then pass it as an argument into `Profile()` and test that. – user692942 Dec 11 '16 at 09:38
  • @Lankymart - 'Profile' will never return 'Nothing' and how an infinite loop could return anything is a puzzle. Why don't you test your assumptions before you publish them? – Ekkehard.Horner Dec 11 '16 at 12:25
  • @Ekkehard.Horner probably because I'm on my iPad. – user692942 Dec 11 '16 at 13:03
  • @Ekkehard.Horner simply assumed that `Profile` performing a check on `Profile` would be a infinite loop, but hadn't tested it. – user692942 Dec 11 '16 at 13:09

1 Answers1

2

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"
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328