Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Runtime Error '91' Object variable or With block variable not set | Excel Discussion (Misc queries) | |||
VBA variable definition help needed. | New Users to Excel | |||
Solver Problem Definition | Excel Worksheet Functions | |||
Object Variable Not Set Error on Selection object | Excel Worksheet Functions | |||
Object class in Excel VBA | Excel Discussion (Misc queries) |