Discussion:
Problem, sub class needing a private var of another, parent class
(too old to reply)
R.Wieser
2016-05-21 19:18:59 UTC
Permalink
Hello all,

I've created a two vbs classes that are supposed to be a parent and a child
(mimicing WinSock and a created socket). When instanciating a child it
needs a specific property from its parent to be able to function (a handle
to an outside OCX object to do the actual work).

The problem is that I do not see a way to have the child either
automatically inherit that property, or to make it only instanciable by its
parent.

For the first I can ofcourse instanciate the child in the parent, and than
call a "property set" to transfer a property from the parent to the child,
but it does not stop anyone from instanciating a child-object directly
(which than would be useless), or to call that "property set" themselves
(although a bit of a "set only once" logic could be applied, it would be
hackish)

I've also tried to embed the child class in its parent, but alas, that
caused a compile-time (syntax) error.

Any other ideas ?

Regards,
Rudy Wieser
JJ
2016-05-22 16:12:01 UTC
Permalink
Post by R.Wieser
Hello all,
I've created a two vbs classes that are supposed to be a parent and a child
(mimicing WinSock and a created socket). When instanciating a child it
needs a specific property from its parent to be able to function (a handle
to an outside OCX object to do the actual work).
The problem is that I do not see a way to have the child either
automatically inherit that property, or to make it only instanciable by its
parent.
For the first I can ofcourse instanciate the child in the parent, and than
call a "property set" to transfer a property from the parent to the child,
but it does not stop anyone from instanciating a child-object directly
(which than would be useless), or to call that "property set" themselves
(although a bit of a "set only once" logic could be applied, it would be
hackish)
I've also tried to embed the child class in its parent, but alas, that
caused a compile-time (syntax) error.
Any other ideas ?
VBScript class doesn't have a concept of inheritance and it can't be
subclassed. You can only superclass it. So, what you want to achieve can't
be done without some workaround.

That being said, the workaround won't be without flaw - which is that anyone
can instantiate the child class directly, as an unlinked or unassociated
object. The reasons are due to these missing VBScript features.

- Class declaration can not have an argument.
e.g.: class myclass(arg1, arg2)
...
end class
and : set obj = new myclass(arg1, arg2)

- Class initialization event can not have an argument.
e.g.: class myclass
private sub class_initialize(arg1, arg2)
...
end sub
...
end class

- Argument can't be passed to a class instantiation.
e.g.: set obj = new myclass(arg1, arg2)

However, you might be able to get better workaround by mixing VBScript and
JScript. Or... you can go all the way using VBScript.NET.
R.Wieser
2016-05-22 16:57:51 UTC
Permalink
JJ,
Post by JJ
You can only superclass it.
If you mean you can DIM one class inside another (don't know yet what
happens if the DIM-ed class contains functions ...) than I've read that.
Have not tried anything with it though.
Post by JJ
- Class declaration can not have an argument.
(and)
Post by JJ
- Class initialization event can not have an argument.
(thus the)
Post by JJ
- Argument can't be passed to a class instantiation.
Yeah, found that out too. :-\

Also found out that you cannot create a anonymous (nameless?) default
function for the object either (objectname = "foobar" is not possible).
Post by JJ
However, you might be able to get better workaround by mixing
VBScript and JScript.
My attempt was to make stuff *easier* to manage, not to make it even more
complex. :-)

Also, what I'm currently writing is supposed to be stand-alone (to be
executed by wscript.exe), not for a webpage. So, no VBS/JS mixing available
I'm afraid.

If creating classes that contain functions and need to have a certain
relationship to each other is not possible I think I will revert back to
just using functions (with the first argument always being the "object")

In cases I need to chug around more than a single property for an object
than I can always use a good-old array or a properties-only class.

Too bad though.

Regards,
Rudy Wieser
Post by JJ
Post by R.Wieser
Hello all,
I've created a two vbs classes that are supposed to be a parent and a child
(mimicing WinSock and a created socket). When instanciating a child it
needs a specific property from its parent to be able to function (a handle
to an outside OCX object to do the actual work).
The problem is that I do not see a way to have the child either
automatically inherit that property, or to make it only instanciable by its
parent.
For the first I can ofcourse instanciate the child in the parent, and than
call a "property set" to transfer a property from the parent to the child,
but it does not stop anyone from instanciating a child-object directly
(which than would be useless), or to call that "property set" themselves
(although a bit of a "set only once" logic could be applied, it would be
hackish)
I've also tried to embed the child class in its parent, but alas, that
caused a compile-time (syntax) error.
Any other ideas ?
VBScript class doesn't have a concept of inheritance and it can't be
subclassed. You can only superclass it. So, what you want to achieve can't
be done without some workaround.
That being said, the workaround won't be without flaw - which is that anyone
can instantiate the child class directly, as an unlinked or unassociated
object. The reasons are due to these missing VBScript features.
- Class declaration can not have an argument.
e.g.: class myclass(arg1, arg2)
...
end class
and : set obj = new myclass(arg1, arg2)
- Class initialization event can not have an argument.
e.g.: class myclass
private sub class_initialize(arg1, arg2)
...
end sub
...
end class
- Argument can't be passed to a class instantiation.
e.g.: set obj = new myclass(arg1, arg2)
However, you might be able to get better workaround by mixing VBScript and
JScript. Or... you can go all the way using VBScript.NET.
Loading...