Solved

Easy Class Question

Posted on 2001-07-10
29
237 Views
Last Modified: 2011-09-20
How do you call one class from a different class. They are in the same dll.
Thanks,
kmurphy
0
Comment
Question by:kmurphy7
  • 16
  • 5
  • 3
  • +4
29 Comments
 
LVL 5

Expert Comment

by:AndrewDev
ID: 6268920
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
 
LVL 5

Expert Comment

by:AndrewDev
ID: 6268928
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
 

Author Comment

by:kmurphy7
ID: 6268936
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
 

Author Comment

by:kmurphy7
ID: 6268939
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
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 6268953
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
 

Author Comment

by:kmurphy7
ID: 6268963
I'm sorry. I do have Class1 instead of firstClass. I was looking at the wrong project.
0
 

Author Comment

by:kmurphy7
ID: 6268976
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
 
LVL 5

Expert Comment

by:GeoffKell
ID: 6268983
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
 

Author Comment

by:kmurphy7
ID: 6268989
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
 

Author Comment

by:kmurphy7
ID: 6268998
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
 

Author Comment

by:kmurphy7
ID: 6269030
Sorry about the double posts. i clicked the refresh button.
0
 
LVL 3

Expert Comment

by:nigelrowe
ID: 6269076
If I were you I would change the name of your project. DataObject is a VB object, hence the message...

DataObject error '800a01a8'
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 6269088
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
 

Author Comment

by:kmurphy7
ID: 6269099
I tried changing the name now I get testObject error...
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:kmurphy7
ID: 6269113
PaulHews,
I get the same error message when I tried calling it the way you said.
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 6269132
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
 

Author Comment

by:kmurphy7
ID: 6269204
When I try to step through it nothing happens. I don't see the yellow arrow
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 6269289
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
 
LVL 4

Expert Comment

by:VincentLawlor
ID: 6269300
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
 

Author Comment

by:kmurphy7
ID: 6269490
I am running it in IE
0
 

Author Comment

by:kmurphy7
ID: 6269518
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
 
LVL 4

Expert Comment

by:VincentLawlor
ID: 6269528
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
 

Author Comment

by:kmurphy7
ID: 6269576
OK when it gets to Res.Write whatToWrite on class1 a window pops up and says "object required"
0
 
LVL 4

Expert Comment

by:VincentLawlor
ID: 6269660
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
 

Author Comment

by:kmurphy7
ID: 6269740
How would I pass the Scripting context?
0
 
LVL 4

Accepted Solution

by:
VincentLawlor earned 100 total points
ID: 6269802
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
 

Author Comment

by:kmurphy7
ID: 6270052
i get object required on prvScriptingContext.Response.Write "before the set<BR>"
0
 
LVL 4

Expert Comment

by:VincentLawlor
ID: 6271060
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
 

Author Comment

by:kmurphy7
ID: 6273004
Thank You all for you help
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now