|
|
| VBScript :: Declare A Class As Static |
|
bamccaig
Member #7,536
July 2006
|
I've been working with VBScript for a number of months. For this reason I don't expect to find a solution to this (:-/), but I was wondering if anybody knew of a way to declare static classes/methods/properties in VBScript, or simulate it in a human-friendly way. For example:
Class Convert ' [Static] Class Convert
Public Function ToInteger(intInteger)
If IsNumeric(intInteger) Then
ToInteger = CInt(intInteger)
Else
ToInteger = CInt(0)
End If
End Function
End Class
So that using it would require no instantiation, rather you can simply: Dim intInteger: intInteger = Convert.ToInteger(234.23) When I realized that this directly doesn't work I attempted something like the following:
Class Conversion ' [Static] Class Conversion
Public Function ToInteger(intInteger)
If IsNumeric(intInteger) Then
ToInteger = CInt(intInteger)
Else
ToInteger = CInt(0)
End If
End Function
End Class
Dim Convert: Set Convert = New Conversion
Which would allow... Dim intInteger: intInteger = Convert.ToInteger(234.23) However, the problem is cleanup afterward. I've always been told that explicitly cleaning up the objects will speed up execution: Set Convert = Nothing However, in this model both the class and instantiation would be <!--#include -->'d into the script, leaving the cleanup of Convert to either the programmer to know and remember at the bottom, or another <!--#include --> which does the cleanup (that the programmer would still have to know about and remember). Otherwise, I could leave instantiation to the programmer (who would then know to cleanup the object), however, it would be a lot nicer if that was taken care of for him automatically. Suggestions? I've got cookies... ** EDIT ** The reason I'm creating this class in the first place is to handle the Null checks for all of the conversion functions, which for whatever stupid reason crash when passed a Null value. -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
|
Billybob
Member #3,136
January 2003
|
This should work in Visual Basic .NET, dunno about VBscript: Class Convert
Shared Function ToInteger(intInteger)
If IsNumeric(intInteger) Then
ToInteger = CInt(intInteger)
Else
ToInteger = CInt(0)
End If
End Function
End Class
The keyword you were looking for is "Shared", that's Visual Basic's equivalent to C++'s static.
|
|
CGamesPlay
Member #2,559
July 2002
|
Quote: The keyword you were looking for is "Shared", that's Visual Basic's equivalent to C++'s static. Okay, so VB's "Static" keyword does something different? Shared vs. Static. The only difference I can see is that Shared applies to methods, and Static applies to local variables. Am I right? -- Ryan Patterson - <http://cgamesplay.com/> |
|
Billybob
Member #3,136
January 2003
|
Quote: The only difference I can see is that Shared applies to methods, and Static applies to local variables. Am I right? static in C++ is used to describe two things. void some_func() { static int x = 8; } In that context it's being used like VB's Static. class SomeClass { public: static void some_func(); }; In that context, it's being used like VB's Shared. class SomeClass { public: static int x; }; And in that context, it's being used like VB's Shared.
|
|
bamccaig
Member #7,536
July 2006
|
Billybob said: This should work in Visual Basic .NET However, VB.NET shouldn't require a class like this because the "static" Convert class's methods can, IIRC, handle Null. VBScript is really stupid in that if you did something like the following... <%
Dim varImNull: varImNull = Null
Dim strImNull
strImNull = CStr(varImNull) ' BOOM!
%>...the script will throw an exception and blow up. For whatever reason, conversion functions in VBScript are not capable of handling the value Null. Billybob said: The keyword you were looking for is "Shared", that's Visual Basic's equivalent to C++'s static.
I appreciate the keywords, but I doubt that they are built into VBScript. I'll double-check on Monday when I get back to work. I guess there's really no way to do it in VBScript... In the meantime, can anybody think of a fancy way to simulate a static method? -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
|
Archon
Member #4,195
January 2004
|
Module? |
|
bamccaig
Member #7,536
July 2006
|
Archon said: Module?
Functions and Modules said: In VBScript itself and other older varieties there is no real module concept. - Source
-- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
|
Archon
Member #4,195
January 2004
|
Can't you use Module s in VBScript? They're like static classes (in VB6,VB.NET) - no need to instantiate them or anything. |
|
bamccaig
Member #7,536
July 2006
|
Archon said: Can't you use Module s in VBScript? They're like static classes (in VB6,VB.NET) - no need to instantiate them or anything.
I don't think so. You tell me... What's the syntax, at least...? VBScript is missing a lot of VB keywords, etc. In fact, a lot of what it has now was added over a long period of time as developers complained of it's lack of capability... In the beginning it was V[ERY]BScript. For example, the whole reason I want this functionality is because VBScript crashes when you try to convert Null to another data type. When you're pulling data from a database it requires constant !null checks to avoid errors. -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
|
|