can't use VB Interface in both VB and ASP 
Author Message
 can't use VB Interface in both VB and ASP

Try to do some OOP in VB, just not easy.

I created
one DLL, interface class, IAnimal
one DLL, Dog class, implements IAnimal
one VB client,
one ASP page.

I want to be able to use Dog class in both VB client or through ASP.
In VB client, it works fine, but it doesn't work in ASP, since the
implemented function is private and I can't declare type in ASP
beforehand.

DLL #1 animal_interface.vbp, contains IAnimal.class
code:   Public Function Speak(times As Integer) As String
        End Function

DLL #2 Animals.vbp, implement DLL #1 to create a dog class. Dog.class
code:   Implements Animal_Interface.IAnimal

        Private Function IAnimal_Speak(times As Integer) As String
          Dim I As Integer
          Dim Temp As String

          For I = 1 To times
            Temp = Temp + "wuff "
          Next I

          IAnimal_Speak = Temp
        End Function

VB client reference Animal_Interface.IAnimal, and it works.
returns "wuff wuff wuff"
code:   Private Sub Command1_Click()
          Dim D As Animal_Interface.IAnimal

          Set D = CreateObject("Animals.Dog")
          Text1.Text = D.Speak(3)
        End Sub

ASP Page, doesn't work since IAnimal_Speak is private.
        <%
        Dim D
        SET D = Server.CreateObject("Animals.Dog")
        Response.Write D.IAnimal_Speak(3)
        %>

The only 2 solutions I can think of is

1. add another public function in my dog class for ASP COM calling:

        Public Function Speak(times As Integer) As String
          Speak = IAnimal_Speak(times)
        End Function

   and in ASP, call "Response.Write D.Speak(3) "

or

2. Or make Dog.IAnimal_Speak function public

either way, it goes against the idea of OOP,
Is any other better ways to deal with this?

Dorren

thanks

ps: the reason I used CreateObject function because I may implement more
animals later, so I can create them by pass animal name string into
createObject function without redo vb client.

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Sat, 23 Nov 2002 03:00:00 GMT  
 can't use VB Interface in both VB and ASP
Hopefully some of your pains will be solved in VB7.  VB currently lacks some
key OO features.  However, your problem lies mostly in the architecture of
the scripting language, not really VB.  The scripting clients do not bind to
the vtbl of interfaces you design - they can only bind to IDispatch.
However, all of your interfaces you design in VB implement IDispatch, so how
does the scripting client know which one it will need to bind to?  It binds
to the one marked as [default] in IDL.  Open up your VB DLL in OLEView and
view the IDL for your DLL - only one interface can be marked as [default].
Since scripting clients can only bind to the [default] IDispatch interface,
it only has access to the methods/properties available through of that one
interface.  This means any non-default interfaces (i.e. your IAnimal
interface) are not accessible.  So how do you make those same interfaces
available through the [default] interface?  You've already devised two ways.
They are definitely ugly.  Another way requires a lot more knowledge about
the internals of COM.  You can create your own class factory that will
handle these types of situations.  You can't write the class factory in VB,
however.  Perhaps you can look into ATL.

Matt


Quote:
> Try to do some OOP in VB, just not easy.

> I created
> one DLL, interface class, IAnimal
> one DLL, Dog class, implements IAnimal
> one VB client,
> one ASP page.

> I want to be able to use Dog class in both VB client or through ASP.
> In VB client, it works fine, but it doesn't work in ASP, since the
> implemented function is private and I can't declare type in ASP
> beforehand.

> DLL #1 animal_interface.vbp, contains IAnimal.class
> code:   Public Function Speak(times As Integer) As String
>         End Function

> DLL #2 Animals.vbp, implement DLL #1 to create a dog class. Dog.class
> code:   Implements Animal_Interface.IAnimal

>         Private Function IAnimal_Speak(times As Integer) As String
>           Dim I As Integer
>           Dim Temp As String

>           For I = 1 To times
>             Temp = Temp + "wuff "
>           Next I

>           IAnimal_Speak = Temp
>         End Function

> VB client reference Animal_Interface.IAnimal, and it works.
> returns "wuff wuff wuff"
> code:   Private Sub Command1_Click()
>           Dim D As Animal_Interface.IAnimal

>           Set D = CreateObject("Animals.Dog")
>           Text1.Text = D.Speak(3)
>         End Sub

> ASP Page, doesn't work since IAnimal_Speak is private.
>         <%
> Dim D
> SET D = Server.CreateObject("Animals.Dog")
> Response.Write D.IAnimal_Speak(3)
>         %>

> The only 2 solutions I can think of is

> 1. add another public function in my dog class for ASP COM calling:

>         Public Function Speak(times As Integer) As String
>           Speak = IAnimal_Speak(times)
>         End Function

>    and in ASP, call "Response.Write D.Speak(3) "

> or

> 2. Or make Dog.IAnimal_Speak function public

> either way, it goes against the idea of OOP,
> Is any other better ways to deal with this?

> Dorren

> thanks

> ps: the reason I used CreateObject function because I may implement more
> animals later, so I can create them by pass animal name string into
> createObject function without redo vb client.

> Sent via Deja.com http://www.deja.com/
> Before you buy.



Sun, 24 Nov 2002 03:00:00 GMT  
 can't use VB Interface in both VB and ASP
Why blame VB for a limitation of a scripting language?  Write IAnimal and
Dog in ATL/C++, it still will not work with ASP.

As another suggestion for how to work around this, consider that scripts are
not OO:  So leave your IAnimal interface alone for the real OO clients.  Now
add a separate, "script" interface using the public methods of Dog.  A
little creative change in the interface may produce a more "service-like"
interface, more suitable to scripts than OO clients.

Also, Matt, who says you cannot build a class factory in VB?  It may not be
quite as sophisticated as some, but a simple class factory is well within VB
capabilities:

  Public Function CreateDog( ) As IAnimal
    Dim oDog As IAnimal
    Set oDog = New Dog
    Set CreateDog = oDog
  End Function

Put that in a GlobalMultiUse Class, and off you go.  (If the creation of Dog
is even more complicated, you can change Dog to Private and force the VB
clients to use the factory.)

Finally, there is always the simple interface cast routine.  Not sure how
this will work with ASP, but it might be worth a try.  Got this idea from
{*filter*} Visual Basic:

  Public Function IAnimal(obj As IAnimal) As IAnimal
    Set IAnimal = obj
  End Function

Write a routine like that, in the component, for each interface exposed by
the component.  (I put mine in a GlobalMultiUse Class for convenience, but
it does not really matter where it goes, as long as the client can get to
it.)  With a class like this, all the client needs is any object that it
knows is an IAnimal:

  Dim oDog As Dog
  Set oDog = New Dog
  IAnimal(oDog).Speak

Steven



Sun, 24 Nov 2002 03:00:00 GMT  
 can't use VB Interface in both VB and ASP
Do not do this in your factory:

Set oDog = New Dog

Dog will not be created by the COM runtime.  You will get weird problems
eventually.  Use CreateObject("...")



Quote:
> Why blame VB for a limitation of a scripting language?  Write IAnimal and
> Dog in ATL/C++, it still will not work with ASP.

> As another suggestion for how to work around this, consider that scripts
are
> not OO:  So leave your IAnimal interface alone for the real OO clients.
Now
> add a separate, "script" interface using the public methods of Dog.  A
> little creative change in the interface may produce a more "service-like"
> interface, more suitable to scripts than OO clients.

> Also, Matt, who says you cannot build a class factory in VB?  It may not
be
> quite as sophisticated as some, but a simple class factory is well within
VB
> capabilities:

>   Public Function CreateDog( ) As IAnimal
>     Dim oDog As IAnimal
>     Set oDog = New Dog
>     Set CreateDog = oDog
>   End Function

> Put that in a GlobalMultiUse Class, and off you go.  (If the creation of
Dog
> is even more complicated, you can change Dog to Private and force the VB
> clients to use the factory.)

> Finally, there is always the simple interface cast routine.  Not sure how
> this will work with ASP, but it might be worth a try.  Got this idea from
> {*filter*} Visual Basic:

>   Public Function IAnimal(obj As IAnimal) As IAnimal
>     Set IAnimal = obj
>   End Function

> Write a routine like that, in the component, for each interface exposed by
> the component.  (I put mine in a GlobalMultiUse Class for convenience, but
> it does not really matter where it goes, as long as the client can get to
> it.)  With a class like this, all the client needs is any object that it
> knows is an IAnimal:

>   Dim oDog As Dog
>   Set oDog = New Dog
>   IAnimal(oDog).Speak

> Steven



Sun, 08 Dec 2002 03:00:00 GMT  
 can't use VB Interface in both VB and ASP
It was my understanding that this prohibition only applied to distributed
objects (DCOM and/or MTS--i.e. anything marshaled).  I've done stuff like
this for local objects for 3 years now, and never had any trouble.  Am I
missing something?
Quote:

>Do not do this in your factory:

>Set oDog = New Dog

>Dog will not be created by the COM runtime.  You will get weird problems
>eventually.  Use CreateObject("...")



Sun, 08 Dec 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. 'Canned' data in VB program

2. Problems calling VB Components from ASP using Interfaces Classes

3. Problems executing ASP components using VB Interfaces

4. Questions about Built-in ASP object Interfaces for VB

5. Internet DB applications with VB(Not ASP or browser interfaces)

6. line graph in vb (canned routine)

7. Questions about Built-in ASP object Interfaces for VB

8. Accessing VB component interfaces from ASP script?

9. Wrong interface returned to ASP from VB, only when compiled

10. VB COM Interface and ASP

11. Accessing VB component interfaces from ASP script?

12. Questions about Built-in ASP object Interfaces for VB

 

 
Powered by phpBB® Forum Software