Home > Archive > Visual Basic > March 2006 > VB6 - ActiveX DLL with 2 forms and a class
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
VB6 - ActiveX DLL with 2 forms and a class
|
|
| jhall@datalyzer.com 2006-03-29, 6:56 pm |
| I have a ActiveX DLL project with 2 forms and a class
frmSheet
frmChart
class
To simplify:
class has two subs:
Public Sub CalcDisplayChart(ID as long)
'do calcs
'fill in chart info
'frmChart.chart.properties = x 'a large set of properties are set
before showing the form
frmChart.show
End Function
Public Sub ShowSheet()
frmSheet.Show
End Sub
*******
Within frmSheet:
Private Sub ButtonClicked()
SelectedID = x
CalcDisplayChart(SelectedID) '???
End Sub
??? - This is where the program cannot find CalcDisplayChart
When another program instantiates this class, the CalcDisplayChart and
ShowSheet work fine. But, cannot launch the CalcDisplayChart from
within ShowSheet form.
| |
| Ken Halter 2006-03-29, 6:56 pm |
| <jhall@datalyzer.com> wrote in message
news:1143656998.298667.74860@j33g2000cwa.googlegroups.com...
>I have a ActiveX DLL project with 2 forms and a class
> frmSheet
> frmChart
> class
>
> To simplify:
> class has two subs:
>
> Public Sub CalcDisplayChart(ID as long)
> 'do calcs
> 'fill in chart info
> 'frmChart.chart.properties = x 'a large set of properties are set
> before showing the form
> frmChart.show
> End Function
>
> Public Sub ShowSheet()
> frmSheet.Show
> End Sub
>
> *******
>
> Within frmSheet:
> Private Sub ButtonClicked()
> SelectedID = x
> CalcDisplayChart(SelectedID) '???
> End Sub
>
> ??? - This is where the program cannot find CalcDisplayChart
>
> When another program instantiates this class, the CalcDisplayChart and
> ShowSheet work fine. But, cannot launch the CalcDisplayChart from
> within ShowSheet form.
Since there can be anywhere from 1 to millions of instances of that class,
the form will need a direct reference to the exact instance you're working
with. Even then, you seem to be treating that public sub as if it were in a
BAS module instead of a class. Even if were just in a form, you'd have to
tell VB which form it was.
The call will need to change to...
Call myObjectVariableThatPointsToTheClass.CalcDisplayChart(SelectedID)
--
Ken Halter - MS-MVP-VB - Please keep all discussions in the groups..
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
| |
| jhall@datalyzer.com 2006-03-29, 6:56 pm |
| I guess I was treating the entire project as part of the same class,
since the forms were a recent addition. Previously the only module was
the class.
I have not created an object of this class within the form.
I was worried about doing that since the form was launched from an
instance of the class. What is the relationship between the object and
the form and the new instance?
How do I find the name of the object that I am currently in?
Even if I do create a new object of this class type within the form, I
need to use the same database connection string as the original object,
so I think I need to have the original object name to retrieve the a
handle to the connection.
| |
| Ken Halter 2006-03-29, 6:56 pm |
| <jhall@datalyzer.com> wrote in message
news:1143662335.739716.202500@i40g2000cwc.googlegroups.com...
>I guess I was treating the entire project as part of the same class,
> since the forms were a recent addition. Previously the only module was
> the class.
>
> I have not created an object of this class within the form.
> I was worried about doing that since the form was launched from an
> instance of the class. What is the relationship between the object and
> the form and the new instance?
>
> How do I find the name of the object that I am currently in?
>
> Even if I do create a new object of this class type within the form, I
> need to use the same database connection string as the original object,
> so I think I need to have the original object name to retrieve the a
> handle to the connection.
The "best" way to deal with multiple objects is to pass a reference to any
sub that requires it... that takes a bit of a re-write from what you have.
For example, the code below....
> Public Sub ShowSheet()
> frmSheet.Show
> End Sub
....should be changed to something more like....
> Public Sub ShowSheet()
> Call frmSheet.ShowForm(Me)
> End Sub
....then, in frmSheet, add a sub that looks a bit like....
Public Sub ShowForm(TheClass As Class) 'assuming "Class" is really the name
of your class module.
'Since we're now in the form, use Me instead of the forms name
Me.Show
'At this point, the "TheClass" parameter holds a reference to the class
that called the sub. You can use
'that reference to gain access to the Public or Friend contents of the
class.
'If you store the reference, make double sure you set it to Nothing
before the form closes or your
'app may never end.
End Sub
.....but, based on the code you've already posted, this'll "work" <g>...
circular references are generally a "bad thing" but, if careful, you can get
away with using them.
'=========frmSheet code
Option Explicit
Public TheClass As Class
Private Sub Command1_Click()
Call TheClass.CalcDisplayChart(12345)
End Sub
'=========Class code
Option Explicit
Public Sub ShowSheet()
Set frmSheet.TheClass = Me
frmSheet.Show
End Sub
Public Sub ShutDown()
'Since, the way you have it coded so far requires a circular
'reference (class refers to form refers to class),
'Be absolutely sure to run this routine before attempting
'to close the app. It'll never close if you don't
On Error Resume Next
Set frmSheet.TheClass = Nothing
Unload frmSheet
Set frmSheet = Nothing
Err.Clear
End Sub
Public Sub CalcDisplayChart(ID As Long)
Debug.Print ID
End Sub
'=========
--
Ken Halter - MS-MVP-VB - Please keep all discussions in the groups..
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
| |
| jhall@datalyzer.com 2006-03-29, 6:56 pm |
| I understand. I can pass in the connection string the same way as the
object itself.
Would it be preferable to:
Private Sub Command1_Click()
Dim chartObj as New Class
'Take care of db initialization here
charObj.CalcDisplayChart(12345)
Set chartObj = Nothing
End Sub
| |
| Ken Halter 2006-03-29, 6:56 pm |
| <jhall@datalyzer.com> wrote in message
news:1143667185.197701.41420@g10g2000cwb.googlegroups.com...
>I understand. I can pass in the connection string the same way as the
> object itself.
>
> Would it be preferable to:
>
> Private Sub Command1_Click()
> Dim chartObj as New Class
> 'Take care of db initialization here
> charObj.CalcDisplayChart(12345)
> Set chartObj = Nothing
> End Sub
If the form/class relationship is based on code only (no
properties/variables requirement), that'll work. If you need access to an
existing instance that may have a bunch of properties you need from the
form, you'll have to pass the class to the form. Thing is, in a DLL, the
main class basically "owns" everything else. The class should be in charge
of creating and manipulating the forms and the forms should be returning
info to the class (if required). One way to look at it is... the main class
is the Parent and anything the class creates are children. The code you have
there is creating a Parent from a child. That works fine as long as you
don't need info from the existing "Parent"
--
Ken Halter - MS-MVP-VB - Please keep all discussions in the groups..
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
| |
| Jeff Johnson [MVP: VB] 2006-03-29, 6:56 pm |
|
<jhall@datalyzer.com> wrote in message
news:1143667185.197701.41420@g10g2000cwb.googlegroups.com...
> charObj.CalcDisplayChart(12345)
Calling a procedure directly without using its return value in VB does not
require parentheses, and I recommend you drop them or they'll bite you in
the ass one day.
charObj.CalcDisplayChart 12345
|
|
|
|
|