Re: Comparison Script Components with Script Classes

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



I think it's just a matter of preference. Both methods
provide a way to encapsulate code that's then treated
as an object.

WSCs have the advantage that they can be stored
soomewhere, registered, then treated like system
files so that you never need to look at the code again.

WSCs have the disadvantage that you have to deal
with

1) Unnecessarily picky XML in the file structure (The XML
is case-sensitive and format sensitive, to no purpose.
And the debugger is not very sharp. It's smart enough to
cause the WSC to fail if you write, say, <Public> instead of
<public>, but it's not smart enough to do an internal check
to see whether the error is case-related so that it can tell
you so.)

2) A hokey (as well as somewhat risky) use (or misuse)
of COM. To register text files as COM objects seems
like bad planning to me. If the file is moved you have a
Registry error. If it's edited you have a COM problem.

Personally, I like pasted classes. If I wanted the
convenience of WSCs I'd just set up a class storage
folder somewhere. Then instead of creating a WSC
object I would just use a small "CreateClass" sub that
opens the class file, reads it in via textstream, calls
ExecuteGlobal to include the class, then creates the class
object. A CreateClass sub of about 6 lines will do the
same basic thing as creating a WSC without the Registry
mess or the XML hassle. (And classes can also be used
via SCRIPT SRC in HTML, as you pointed out.)

WSCs appear to me to be the result of too many
MS programmers with too much time on their hands....
but I'm sure that there are a lot of people who love WSCs
and would disagree with me heartily. :)

Hello everybody,

the last days I started to make myself familiarised with VBScript. Now I
would like to have some advice from more experienced programmers. I do
not really figure out, how the concepts of Script Components and Script
Classes differ and for what use cases they are intended.

I) Both are quite similar:

(1) They give the opportunity to define public methods and properties to
encapsulate the modules.

(2) Each can produce an object by calling:

Class: "new objectClass = New classname"
Component: "new objectComponent = GetObject(ComponentName)

And one can use the objects functions over the same syntax:
object.function


II) But there are some differences:

(1) With Components all code around the defined functions and classes is
executed on the creation of a component-object. (Dirty for class
definitions - what is the sense of this?)

(2) With classes one can define constructors and destructors like in C++
(sub class_initialize and sub class_terminate). That doesn't seem to
work with components.

(3) It doesn't seem to work to define classes in components and make the
definition usable to the calling script.

(..) perhaps there are some more I didn't figure out yet

III) Ways to use them

(1) Classes seem to be one way to write reusable libraries. I could
define my classes each in a separate .vbs-file. Then including the
necessary class-definitions to my .wsf-s by a xml tag like:
<script language="VBScript" src="filename.vbs">

(2) An other way would be to create a component for each kind of object
I need. Then creating the objects with
Set objectComponent = GetObject (...)

IV) Best Practice

What would you advice to me. How do you build your strictly
object-oriented libraries in VBScript? How do you build mixed procedural
/ object-oriented libraries.

I would be very happy if someone could provide me with some information
about best practices on this field.

Thank you very much in advance,

Melanie Desaive




.



Relevant Pages

  • Re: Comparison Script Components with Script Classes
    ... And I am sure there must be situations, where using WSCs is the better solution. ... Unnecessarily picky XML in the file structure (The XML ... A CreateClass sub of about 6 lines will do the ... how the concepts of Script Components and Script ...
    (microsoft.public.scripting.vbscript)
  • Re: WScript.Sleep for HTA?
    ... It's my backup for WSCs, where the calling script fails ... > and not a basic problem with handling the WScript object. ... > Is it possible to start your HTA with a WSH-hosted script and have the ...
    (microsoft.public.scripting.vbscript)
  • Re: Automated WSF argument validation: demo script.
    ... The "right" kind of tool for this job is a COM-accessible XML stream reader ... The script is a demo of automating argument validation ... >> end sub ...
    (microsoft.public.scripting.wsh)
  • Re: Automated WSF argument validation: demo script.
    ... The "right" kind of tool for this job is a COM-accessible XML stream reader ... The script is a demo of automating argument validation ... >> end sub ...
    (microsoft.public.windows.server.scripting)