Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Problem Using an Object Variable in a Class Definition

Hello,

I was hoping to get some help with what I hope is a simple problem. I
am trying to nest a user defined object inside another user defined
object. The syntax seems to work but it does not achieve the desired
result. It works using Type statement in traditonal non object mode
but when I convert it to objects, it fails.

Here is the code using the traditional approach. I used a street
intersection as an example.
-------------------------------------------------------------------------
'Commented code included to show full implementation
Type Signal
Red As Boolean
'Yellow As Boolean
'Green As Boolean
End Type

Type Intersection
North As Signal
'South As Signal
'East As Signal
'West As Signal
End Type

' Here is the invocation that shows the state of one light at the
intersection

Sub TraditionalVersion()
Dim HollywoodAndVine As Intersection
MsgBox HollywoodAndVine.North.Red 'DESIRED RESULT
End Sub
---------------------------------------------------------------------------
Notice I can declare a type as another type and then access the
variables using standard .dot notation

I want to convert these variables to objects and achieve the same
result. Here is the code I tried (The two classes are on separate
class modules)

--------------------------------------------------------------------------------
' Intersection Class
'Properties for commented code omitted
Private b_north As Object
'Private b_south As Object
'Private b_east As Object
'Private b_west As Object

Public Property Get NorthBound() As Object
NorthBound = b_north
End Property

Public Property Let NorthBound(TransDirection As Object)
b_north = TransDirection
End Property
------------------------------------------------------------------------------

'Signal Class
'Properties for commented code omitted
Private b_red As Boolean
'Private b_yellow As Boolean
'Private b_green As Boolean

Public Property Get RedLight() As Boolean
RedLight = b_red
End Property

Public Property Let RedLight(TransRed As Boolean)
b_red = TransRed
End Property
-------------------------------------------------------------------

'Main Control Loop
Sub ObjectVersion()
Dim ParkAveAndFifth As New IntersectionObject
Dim NorthSignal As New SignalObject
MsgBox NorthSignal.RedLight 'This statement proves that the
NorthSignal Object gets created
Set ParkAveAndFifth.NorthBound = NorthSignal 'Assign local object
variable to object variable in the Intersection Object
'Previous line of code Generates Object Variable Not Set error
message. Why???
MsgBox ParkAveAndFifth.NorthBound.RedLight 'DESIRED RESULT
End Sub

It seems like this should work. Sorry for the long post but I'm sure
I'm just making a dumb mistake. Thank you in advance. I really
appreciate your help.

Box



------------------------------------------------
Message posted from http://www.ExcelTip.com/

-- View and post Excel related usenet messages directly from http://www.ExcelTip.com/forum
-- Hundreds of free MS Excel tips, tricks and solutions at http://www.ExcelTip.com/
------------------------------------------------
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 459
Default Problem Using an Object Variable in a Class Definition

Boxman,
I think this is what you wanted to do:

------------------------
Option Explicit

' Intersection Class
' Put code in a
' class module named 'CIntersection'

Private o_north As CSignal

Public Property Get NorthBound() As CSignal
Set NorthBound = o_north
End Property

Public Property Set NorthBound(TransDirection As CSignal)
Set o_north = TransDirection
End Property
------------------------
Option Explicit

' Signal Class
' Put code in a
' class module named 'CSignal'

Private b_red As Boolean

Public Property Get RedLight() As Boolean
RedLight = b_red
End Property

Public Property Let RedLight(ByVal TransRed As Boolean)
b_red = TransRed
End Property
------------------------
Option Explicit

'Main Control Loop

Sub ObjectVersion()
Dim ParkAveAndFifth As CIntersection
Dim NorthSignal As CSignal

Set ParkAveAndFifth = New CIntersection
Set NorthSignal = New CSignal

MsgBox NorthSignal.RedLight
Set ParkAveAndFifth.NorthBound = NorthSignal
MsgBox ParkAveAndFifth.NorthBound.RedLight

End Sub
------------------------
You were nearly there. Some general points:

The name of the class module determines the name class and you must
use this name when you declare your object variables.

When the property is an object you need a Get/Set pair (rather than a
Get/Let) and you must use the Set keyword in both.

Get into the habit of instantiating objects separate from their
declaration i.e. don't use Dim As New.

A minor point but you usually pass a variable ByVal to a property Let
but always pass an object ByRef to a property Set (using the ByVal
keyword would still pass a pointer to the object anyhow).

There used to be some good articles in the KB but they don't seem to
be available just now (I'm waiting for them to be replaced by .NET
equivalents <g). All the relevant articles I've seen are written with
VB6 in mind. For example, check out the links on this page:

http://msdn.microsoft.com/library/de...nentdesign.asp

However, if you have Excel2000 you should be able to get the code to
work, as long as you recognize some things in the VB6 IDE don't exist
in VBA VBE but these limitations can largely be circumvented. For
example, the VBE doesn't have a facility specifying the default
property of a class but you can do this manually by exporting the
module, editing the text file and re-importing.

Boxman wrote in message ...
Hello,

I was hoping to get some help with what I hope is a simple problem. I
am trying to nest a user defined object inside another user defined
object. The syntax seems to work but it does not achieve the desired
result. It works using Type statement in traditonal non object mode
but when I convert it to objects, it fails.

Here is the code using the traditional approach. I used a street
intersection as an example.
-------------------------------------------------------------------------
'Commented code included to show full implementation
Type Signal
Red As Boolean
'Yellow As Boolean
'Green As Boolean
End Type

Type Intersection
North As Signal
'South As Signal
'East As Signal
'West As Signal
End Type

' Here is the invocation that shows the state of one light at the
intersection

Sub TraditionalVersion()
Dim HollywoodAndVine As Intersection
MsgBox HollywoodAndVine.North.Red 'DESIRED RESULT
End Sub
---------------------------------------------------------------------------
Notice I can declare a type as another type and then access the
variables using standard .dot notation

I want to convert these variables to objects and achieve the same
result. Here is the code I tried (The two classes are on separate
class modules)

--------------------------------------------------------------------------------
' Intersection Class
'Properties for commented code omitted
Private b_north As Object
'Private b_south As Object
'Private b_east As Object
'Private b_west As Object

Public Property Get NorthBound() As Object
NorthBound = b_north
End Property

Public Property Let NorthBound(TransDirection As Object)
b_north = TransDirection
End Property
------------------------------------------------------------------------------

'Signal Class
'Properties for commented code omitted
Private b_red As Boolean
'Private b_yellow As Boolean
'Private b_green As Boolean

Public Property Get RedLight() As Boolean
RedLight = b_red
End Property

Public Property Let RedLight(TransRed As Boolean)
b_red = TransRed
End Property
-------------------------------------------------------------------

'Main Control Loop
Sub ObjectVersion()
Dim ParkAveAndFifth As New IntersectionObject
Dim NorthSignal As New SignalObject
MsgBox NorthSignal.RedLight 'This statement proves that the
NorthSignal Object gets created
Set ParkAveAndFifth.NorthBound = NorthSignal 'Assign local object
variable to object variable in the Intersection Object
'Previous line of code Generates Object Variable Not Set error
message. Why???
MsgBox ParkAveAndFifth.NorthBound.RedLight 'DESIRED RESULT
End Sub

It seems like this should work. Sorry for the long post but I'm sure
I'm just making a dumb mistake. Thank you in advance. I really
appreciate your help.

Box



------------------------------------------------
Message posted from http://www.ExcelTip.com/

-- View and post Excel related usenet messages directly from http://www.ExcelTip.com/forum
-- Hundreds of free MS Excel tips, tricks and solutions at http://www.ExcelTip.com/
------------------------------------------------

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Problem Using an Object Variable in a Class Definition

Many thanks... That cleared up the issue very nicely and allowed me to
actually simplify the code even further...
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Runtime Error '91' Object variable or With block variable not set Alec Coliver Excel Discussion (Misc queries) 2 October 24th 09 02:29 PM
VBA variable definition help needed. jenn k New Users to Excel 4 September 5th 08 08:55 PM
Solver Problem Definition testspecmed Excel Worksheet Functions 0 October 5th 06 07:06 PM
Object Variable Not Set Error on Selection object Jean Excel Worksheet Functions 3 July 24th 06 06:45 PM
Object class in Excel VBA Tom Chau Excel Discussion (Misc queries) 1 June 29th 06 11:29 AM


All times are GMT +1. The time now is 12:15 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"