ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Can objects be passed around as in Java? (https://www.excelbanter.com/excel-programming/320558-re-can-objects-passed-around-java.html)

peter

Can objects be passed around as in Java?
 
Well, here it is, but it's going to take a learning curve to figure out
my problem.
But maybe not; like I said, I'm new to Excel/VBA, and maybe it just
looks weird to me.

The main line creates oMACdefn objects of class clsMACdefn5A.
Each oMACdefn contains oFUNCTdefn objects of class clsFUNCTdefn5A.
I want to access the main line's wsWorksheet object from a method (for
now), but I first have a problem coding the Property method. Then the
main line doesn't seem to pass me a good wsWorksheet, tho it looks okay
in the debugger.

Hope it's not too difficult to get thru this stuff,
and maybe even to figure it out!
Thanks,
Peter.


main line :
Dim oMACdefn As clsMACdefn5A
Dim oFUNCTdefn As clsFUNCTdefn5A
Public wsWorksheet As Worksheet

BTW, the main line contains both oMACdefn and oFUNCTdefn's because it's
reading a page that contains the data for both.

clsMACdefn5A:
Private aAllFUNCTRows(1 To maxNumberFUNCTs) As clsFUNCTdefn5A

PROBLEM ONE -- How to code the Property that sets the oMACdefn object
into clsFUNCTdefn5A.

===========

Main line :
Set oFUNCTdefn = New clsFUNCTdefn5A
oFUNCTdefn.doParent oMACdefn 'Compile error,
'Invalid use of Property
' (see below)
oFUNCTdefn.setParent oMACdefn ' ok
' debugger shows obj

clsFUNCTdefn5A
Private oParent As clsMACdefn5A

Public Property Let doParent(ByRef inn As clsMACdefn5A)
Set oParent = inn
End Property
--OR--
Public Property Set doParent(ByRef inn As clsMACdefn5A)
Set oParent = inn
End Property


WORK AROUND:
============

clsMACdefn5A:
oFUNCTdefn.setParent oMACdefn

clsFUNCTdefn5A
Private oParent As clsMACdefn5A
Public Sub setParent(ByRef inn As clsMACdefn5A)
Set oParent = inn
End Sub


PROBLEM TWO -- why doesn't clsMACdefn5A.getWorksheet() work when called
from clsFUNCTdefn5A?

===========

clsMACdefn5A:
Public wsWorksheet As Worksheet
Set wsWorksheet = ActiveSheet ' Class_Initialize
Public Function getWorksheet() As Worksheet
Set getWorksheet = wsWorksheet
End Function

clsFUNCTdefn5A:
Private wsWorksheet As Worksheet
wsWorksheet = oParent.getWorksheet ' runtime error
' obj doesn't support funct/prop.
' this happens when it's getting back to
clsFUNCTdefn5A



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

onedaywhen[_2_]

Can objects be passed around as in Java?
 
peter wrote:
Each oMACdefn contains oFUNCTdefn objects of class clsFUNCTdefn5A.
I want to access the main line's wsWorksheet object from a method

(for
now), but I first have a problem coding the Property method. Then

the
main line doesn't seem to pass me a good wsWorksheet, tho it looks

okay
in the debugger.


A few points. When working with objects, you should use one or both of
a Property Get and a Property Set pair and use a Private (rather than
Public) object variable to retain the reference. And when assigning the
property value, you need to use the Set keyword with the = assignment.

The Class_Initialize() isn't a very useful constructor because it can
take no parameters so I usually write my own constructor (called Init)
which must be explicitly called *after* the object has been
instantiated. Not ideal but there you go. Not that I don't use the
Initialize event as well and your usage is perfectly legitimate... but
for reasons of 'encapsulation' I'd be a little uncomfortable about
using ActiveSheet in the Initialize event of a class; I'd prefer to
pass the ActiveSheet object from the main code. So:

' --- <clsMACdefn5A parent class ---
Option Explicit
Private wsWorksheet As Worksheet

Public Function Init(ByVal oSheet As Worksheet)
Set wsWorksheet = oSheet
End Function

Public Property Get Worksheet() As Worksheet
Set Worksheet = wsWorksheet
End Property
' --- </clsMACdefn5A parent class ---

' ---<clsFUNCTdefn5A child class ---
Option Explicit
Private oParent As clsMACdefn5A

Public Property Set Parent(ByVal inn As clsMACdefn5A)
Set oParent = inn
End Property

Public Property Get Parent() As clsMACdefn5A
Set Parent = oParent
End Property
' ---</clsFUNCTdefn5A child class ---

Sub MainCode()
Dim oMACdefn As clsMACdefn5A
Set oMACdefn = New clsMACdefn5A
oMACdefn.Init ActiveSheet

Dim oFUNCTdefn As clsFUNCTdefn5A
Set oFUNCTdefn = New clsFUNCTdefn5A
Set oFUNCTdefn.Parent = oMACdefn

End Sub

BTW As a heads up, I'm generally very wary of references (pointers) to
a parent class because it is a fine way to create a memory leak. It is
usual for an instance of a parent class to hold references to its child
objects, so if the child also holds a reference to the parent you have
a circular reference. This is a problem when it comes to destroy the
parent object: the references to the parent within the children have to
be explicitly released before the parent object can be released from
memory. This can't simply be done in the Class_Terminate event because
the Terminate event will not fire until the object is actually being
destroyed and this will not happen until the references have been
released, catch 22. So you must have a destructor routine which must be
explicitly called before trying to set the parent to Nothing. Failing
to do this will result in the object remaining in memory until the app
quits i.e. leaking memory. I prefer to use a dedicated class to provide
a reference to the parent on demand via an event but that's another
story ...

Jamie.

--


peter

Can objects be passed around as in Java?
 
Thanks for the suggestions Jamie, I'll implement them ASAP.

Can you say for sure that using Let with objects is a Bad Thing? I've
been using them exclusively for several hundred lines of code now.

Peter.



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Jamie Collins

Can objects be passed around as in Java?
 

peter wrote:
Can you say for sure that using Let with objects is a Bad Thing?

I've
been using them exclusively for several hundred lines of code now.


No, I can't say for sure <g but I have seen problems in these ng where
using Let instead of Set has been an issue. I *can* say I've always
used Set and have had not similar problems.

Jamie.

--


Stephen Bullen[_4_]

Can objects be passed around as in Java?
 
Hi Peter,

Can you say for sure that using Let with objects is a Bad Thing? I've
been using them exclusively for several hundred lines of code now.


FWIW, when not using Set, you're actually asking for the default
property of the object, so:

wsWorksheet = oParent.getWorksheet ' runtime error
' obj doesn't support funct/prop.
' this happens when it's getting back to

the getWorksheet is returning the sheet, but by using the (implicit)
Let, you're asking for it's default property. The Worksheet object
doesn't have a default property, so you get the RTE.

If you'd used:

Set wsWorksheet = oParent.getWorksheet

you're telling the VB compiler that the wsWorksheet object should be set
to refer to the worksheet object returned from the getWorksheet
property, so all is well.

So:

- When setting objects to refer to each, always use Set and Property
Set/Get

- When using strings/numbers to refer to each other, always use Let (or
omit it) and Property Let/Get

Regards

Stephen Bullen
Microsoft MVP - Excel
www.oaltd.co.uk



peter

Can objects be passed around as in Java?
 
Thanks Stephen, now I see it.

It wasn't the Property but what I did with the result of it!



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Stephen Bullen[_4_]

Can objects be passed around as in Java?
 
Hi Peter,

It wasn't the Property but what I did with the result of it!


Exactly!

Regards

Stephen Bullen
Microsoft MVP - Excel
www.oaltd.co.uk




All times are GMT +1. The time now is 05:25 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com