Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 248
  • Last Modified:

Easy Class Question

How do you call one class from a different class. They are in the same dll.
Thanks,
kmurphy
0
kmurphy7
Asked:
kmurphy7
  • 16
  • 5
  • 3
  • +4
1 Solution
 
AndrewDevCommented:
In whichever method or property of class 1 you wnat it to happen

dim objclass2 as class2
set objclass2 = new class2

' do whatever you do with the class

set objclass2 = nothing
0
 
AndrewDevCommented:
I should have added, if its in a different dll you will need to add the dll to your project references. In the same dll that isn't necessary.

0
 
kmurphy7Author Commented:
I have the following in class1
Public Sub writeStuff(whatToWrite As String)
    Res.Write whatToWrite
End Sub

In class 2 I have:
Dim test As firstClass

Public Sub doSomething()
    Set test = New firstClass
    test.writeStuff ("Write something already")
End Sub

When I call it from the asp page I get:
   DataObject error '800a01a8'

   Object required
Any suggestions
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
kmurphy7Author Commented:
I have the following in class1
Public Sub writeStuff(whatToWrite As String)
    Res.Write whatToWrite
End Sub

In class 2 I have:
Dim test As firstClass

Public Sub doSomething()
    Set test = New firstClass
    test.writeStuff ("Write something already")
End Sub

When I call it from the asp page I get:
   DataObject error '800a01a8'

   Object required
Any suggestions
0
 
Arthur_WoodCommented:
Where do you define firstClass?

I suspect that what you need is:

Dim test As Class1

Public Sub doSomething()
   Set test = New Class1
   test.writeStuff ("Write something already")
End Sub


The Name that you give the Class is ALSO the name by which you refer to it when you create an object based ON that class.
0
 
kmurphy7Author Commented:
I'm sorry. I do have Class1 instead of firstClass. I was looking at the wrong project.
0
 
kmurphy7Author Commented:
I am using the following to call the dll:
<%
dim ObjReference
Set ObjReference = Server.CreateObject
("DataObject.class2")
ObjReference.doSomething()
%>

Is this correct. DataObject is the name of the project
0
 
GeoffKellCommented:
What is the Res object inside Class1. This could be where the error is occurring?

Have you tried stepping through the code in the IDE to find the exact point of failure.

Regards
GK
0
 
kmurphy7Author Commented:
Public Sub OnStartPage(sc As ScriptingContext)
    Set Req = sc.Request
    Set Res = sc.Response
    Set Svr = sc.Server
    Set Ses = sc.Session
End Sub

Also how do you step through a project. I am fairly new to vb
0
 
kmurphy7Author Commented:
Public Sub OnStartPage(sc As ScriptingContext)
    Set Req = sc.Request
    Set Res = sc.Response
    Set Svr = sc.Server
    Set Ses = sc.Session
End Sub

Also how do you step through a project. I am fairly new to vb
0
 
kmurphy7Author Commented:
Sorry about the double posts. i clicked the refresh button.
0
 
nigelroweCommented:
If I were you I would change the name of your project. DataObject is a VB object, hence the message...

DataObject error '800a01a8'
0
 
PaulHewsCommented:
If you need to use an existing instance of one class in the other, you will need to pass an reference of one to the other.

in project

dim oX1 as MyDll.Class1
dim oX2 as MyDll.Class2

oX2.sinit oX1

Note now that when you set oX1 = Nothing there is still a reference in oX2.
0
 
kmurphy7Author Commented:
I tried changing the name now I get testObject error...
0
 
kmurphy7Author Commented:
PaulHews,
I get the same error message when I tried calling it the way you said.
0
 
Arthur_WoodCommented:
In order to Step through a program, in the IDE (Integrated Developemtn Environment), if you simply place the editting cursor on the line whare you want the program to "pause" - so that you can then begin to single step through the code - and then press the F9 button on you keyboard, thast will set a BreakPoint (notice how the line is now highlighted in RED).  When you the run the program, when execution gets to that line, the program will PAUSE - not STOP Completely, and you will be in what is called DEBUG mode.  You can then execute one line of code ate a time by pressing the F8 key - notice the yellow arrow at the left margin, which indicates the line that IS ABOUT TO BE EXECUTED.  You can also view any value you wish by using the Immediate Window (activated by pressing CTRL-G (control and G keys together).

This is a very valuable technique to learn to use as early in your VB experience as possible.

Arthur Wood
0
 
kmurphy7Author Commented:
When I try to step through it nothing happens. I don't see the yellow arrow
0
 
Arthur_WoodCommented:
That would suggest that the line of code where you are trying to satart the single step (Debug Mode) is never being executed.  Earlier you showed some VBScript :

"I am using the following to call the dll:
<%
dim ObjReference
Set ObjReference = Server.CreateObject
("DataObject.class2")
ObjReference.doSomething()
%>"
 

Are you running this in the VB Development enviroment, or in IE? (perhaps in Visual InterDev?)
0
 
VincentLawlorCommented:
It should be a simple matter of newing your class if it is already in the DLL

To debug the dll you will need to open the Visual Basic Project for the DLL
Set a break point at the point you wish to debug.

Run the project and choose the default option you are presented.

When the object gets loaded from ASP or what ever, code execution will be halted at your break point.


Also where is Res being created ?

V.

0
 
kmurphy7Author Commented:
I am running it in IE
0
 
kmurphy7Author Commented:
Here is all of my code.

'Class1

Dim Req         'Request Variable
Dim Res         'Response Variable
Dim Svr         'Server Variable
Dim Ses         'Session Variable

Public Sub OnStartPage(sc As ScriptingContext)
'**************************************************
'** This method sets the variables and is called **
'** as soon as the dll is called.                **
'**************************************************
    Set Req = sc.Request
    Set Res = sc.Response
    Set Svr = sc.Server
    Set Ses = sc.Session
End Sub

Public Sub writeStuff(whatToWrite)
    Res.Write whatToWrite
End Sub


'Class2

Dim s As MyDataObject.Class1
Dim Req         'Request Variable
Dim Res         'Response Variable
Dim Svr         'Server Variable
Dim Ses         'Session Variable
Public Sub OnStartPage(sc As ScriptingContext)
'**************************************************
'** This method sets the variables and is called **
'** as soon as the dll is called.                **
'**************************************************
    Set Req = sc.Request
    Set Res = sc.Response
    Set Svr = sc.Server
    Set Ses = sc.Session
End Sub
Public Sub doSomething()
        Res.Write "before the set<BR>"
    Set s = New MyDataObject.Class1
        Res.Write "after the set"
    s.writeStuff ("work damn it")
End Sub


<%
'ASP Code'
dim ObjReference
Set ObjReference = Server.CreateObject("MyDataObject.Class2")
ObjReference.doSomething()
%>
0
 
VincentLawlorCommented:
Ok in your DLL project Properties go to Debugging and select either "Wait for Components to be created" or "Start Browser with URL" where the URL is the page you are running the component from.

When a method on the DLL is about to be executed it will stop at your break point.

V.
0
 
kmurphy7Author Commented:
OK when it gets to Res.Write whatToWrite on class1 a window pops up and says "object required"
0
 
VincentLawlorCommented:
Then Res is not a valid object or hasn't yet been set.
You need to call your OnStartPage method passing your Scripting context first before calling dosomething otherwise the Res will not be initialised.

V.


0
 
kmurphy7Author Commented:
How would I pass the Scripting context?
0
 
VincentLawlorCommented:
Declare a Private ScriptingContext

Private prvScriptingContext


And in On StartPage

Public Sub OnStartPage(sc as ScriptingContext)

 Set prvScriptingContext = sc

end Sub


Public Sub doSomething()
   prvScriptingContext.Response.Write "before the set<BR>"
   Set s = New MyDataObject.Class1
       prvScriptingContext.Response.Write "after the set"
   s.writeStuff ("work damn it")
End Sub

Try it and let me know if it works.
Should be the same as yours but for some reason it isn't.

You could also try setting a breakpoint in the OnStartPage sub and see if everything is as it should be.

V.
0
 
kmurphy7Author Commented:
i get object required on prvScriptingContext.Response.Write "before the set<BR>"
0
 
VincentLawlorCommented:
This is the problem then this object is not yet initialised.

What happens inside the OnStartPage sub ?? Does the private ScriptingContext get set ??

I'll have a look at this tomorrow.

v.
0
 
kmurphy7Author Commented:
Thank You all for you help
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 16
  • 5
  • 3
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now