TNO
-
Total Posts
:
2094
- Scores: 36
-
Reward points
:
0
- Joined: 12/18/2004
- Location: Earth
-
Status: offline
|
Initializing a Class with parameters
Thursday, September 03, 2009 5:27 AM
( permalink)
One of the major shortcomings of VBScript Classes is the fact that you can't do this:
Class Person
Private m_Age
Private m_Name
Public Sub Class_Initialize(Name, Age)
m_Name = Name
m_Age = Age
End Sub
Public Property Get Name
Name = m_Name
End Property
Public Property Let Name(v)
m_Name = v
End Property
Public Property Get Age
Age = m_Age
End Property
Public Property Let Age(v)
m_Age = v
End Property
End Class
Dim TheDude : Set TheDude = New Person("John",40)
Looks perfectly reasonable, but try it and you'll get the following error: Microsoft VBScript compilation error: Class initialize or terminate do not have arguments So what is a person to do if they want a little OOP in their VBS? After a bit of hackery, I've come up with a somewhat decent alternative I think:
Class Person
Private m_Age
Private m_Name
Public Default Function Init(Name, Age)
m_Name = Name
m_Age = Age
Set Init = Me
End Function
Public Property Get Name
Name = m_Name
End Property
Public Property Let Name(v)
m_Name = v
End Property
Public Property Get Age
Age = m_Age
End Property
Public Property Let Age(v)
m_Age = v
End Property
End Class
Dim TheDude : Set TheDude = (New Person)("John",40)
Have an opinion on this hackish pattern?
To iterate is human, to recurse divine. -- L. Peter Deutsch
|
|
|
|
ehvbs
-
Total Posts
:
3320
- Scores: 110
-
Reward points
:
0
- Joined: 6/22/2005
- Location: Germany
-
Status: offline
|
Re:Initializing a Class with parameters
Tuesday, September 15, 2009 5:36 AM
( permalink)
I learned something like this from a contribution of Justin Piper to the microsoft.public.scripting.vbscript newsgroup. It's a way to get something like constructors with parameters in VBScript; but instead of using the Default feature of VBScript, I'd write plain init* functions (as much as I need) and call them like Dim TheDude : Set TheDude = New Person.init("John",40)
|
|
|
|
59cobalt
-
Total Posts
:
975
- Scores: 91
-
Reward points
:
0
- Joined: 7/17/2011
-
Status: offline
|
Re:Initializing a Class with parameters
Sunday, August 07, 2011 11:41 AM
( permalink)
Sorry for the late response, but since you were asking for opinions ... When I have the choice between Set foo = (New Bar)(23, 42) and Set foo = New Bar.Init(23, 42) I'd prefer the former. If you need more than a single set of initialization parameters, you still have the option of using additional Init*() methods and falling back to the latter style. I wouldn't worry too much about hackish-ness, because without real c'tor/d'tor, polymorphism or inheritance, VBScript classes are hackish anyway.
|
|
|
|
ehvbs
-
Total Posts
:
3320
- Scores: 110
-
Reward points
:
0
- Joined: 6/22/2005
- Location: Germany
-
Status: offline
|
Re:Initializing a Class with parameters
Monday, August 08, 2011 9:03 AM
( permalink)
In my opinion, there are two cons against the 'use the default function' strategy: - a class with a default init method can't have a default property ("Microsoft VBScript compilation error: Cannot have multiple default property/method in a Class")
- If you (must) fall back to the explicit init*() method(s), your code mixes two different styles of object construction
and - as far as I can see - not a single pro.
|
|
|
|
59cobalt
-
Total Posts
:
975
- Scores: 91
-
Reward points
:
0
- Joined: 7/17/2011
-
Status: offline
|
Re:Initializing a Class with parameters
Monday, August 08, 2011 11:37 AM
( permalink)
ehvbs 1. a class with a default init method can't have a default property ("Microsoft VBScript compilation error: Cannot have multiple default property/method in a Class") True. But in a situation where you need something else to be the Default for the class, you'd make that the Default and fall back to using .Init() for initialization. ehvbs 2. If you (must) fall back to the explicit init*() method(s), your code mixes two different styles of object construction Of course you wouldn't mix them. If multiple .Init*() methods are required, one would use .Init*() for every instantiation. Everything else would indeed be bad style. ehvbs and - as far as I can see - not a single pro. Perhaps it's just me, but I find "(New Bar)(a,b)" better readable than "New Bar.Init(a,b)". Unless one of the two issues you outlined applies, I'd prefer the "short" version.
|
|
|
|