For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic > October 2004 > Properties that return Objects









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 Properties that return Objects
John

2004-10-27, 3:55 pm

What is the benifit of using properties that return objects?
Bob Butler

2004-10-27, 3:55 pm

"John" <John@discussions.microsoft.com> wrote in message news:80206105-
9DF4-4404-8040-1D984E7C1EBA@microsoft.com
> What is the benifit of using properties that return objects?


If you need to access multiple properties of the returned object you can
retain the reference and do it directly.

--
Reply to the group so all can participate
VB.Net... just say "No"

Ralph

2004-10-27, 3:55 pm


"John" <John@discussions.microsoft.com> wrote in message
news:80206105-9DF4-4404-8040-1D984E7C1EBA@microsoft.com...
> What is the benifit of using properties that return objects?


It is important to appreciate that one almost nevers 'returns' an object.
You return a 'reference' to one.

-ralph


John

2004-10-27, 3:55 pm

Do you happen to have a snippet of code to illustrate how that is done?


"Bob Butler" wrote:

> "John" <John@discussions.microsoft.com> wrote in message news:80206105-
> 9DF4-4404-8040-1D984E7C1EBA@microsoft.com
>
> If you need to access multiple properties of the returned object you can
> retain the reference and do it directly.
>
> --
> Reply to the group so all can participate
> VB.Net... just say "No"
>
>

Jeff Johnson [MVP: VB]

2004-10-27, 3:55 pm


"John" <John@discussions.microsoft.com> wrote in message
news:6D0DA639-8BA5-4E95-BBAD-DA530B1B04FE@microsoft.com...

[color=darkred]
> Do you happen to have a snippet of code to illustrate how that is done?


How you return an object or how you access multiple properties of that
object? I'll illustrate #2:

With ListView1.ListItems("Key1")
.Text = "Item (Key1)"
.Ghosted = True
End With

The ListItems property of the ListView control returns a reference to a
ListItem object.


Bob Butler

2004-10-27, 3:55 pm

"Jeff Johnson [MVP: VB]" <i.get@enough.spam> wrote in message
news:eGz6cdDvEHA.2876@TK2MSFTNGP12.phx.gbl
> "John" <John@discussions.microsoft.com> wrote in message
> news:6D0DA639-8BA5-4E95-BBAD-DA530B1B04FE@microsoft.com...
>
>
>
> How you return an object or how you access multiple properties of that
> object? I'll illustrate #2:
>
> With ListView1.ListItems("Key1")
> .Text = "Item (Key1)"
> .Ghosted = True
> End With
>
> The ListItems property of the ListView control returns a reference to
> a ListItem object.


You can also do it with something like:
Dim oItem As ListItem
Set oItem=ListView1.ListItems("Key1")

and you can then retain the reference in "oItem" as long as you need it.

--
Reply to the group so all can participate
VB.Net... just say "No"

John

2004-10-27, 3:55 pm

Okay that makes sense.

So how would I implement this in my own code? For example, I have two
Classes: Class1 (containing Property1, Property2 and Method1) and Class2
(containing Property3 and Method2).

How do I create a reference to Class2's members using a property in Class1?

More specifically, what is the syntax for the Property Get/Let statements in
Class1 in order to achieve this "reference" to Class2?





"Jeff Johnson [MVP: VB]" wrote:

>
> "John" <John@discussions.microsoft.com> wrote in message
> news:6D0DA639-8BA5-4E95-BBAD-DA530B1B04FE@microsoft.com...
>
>
>
> How you return an object or how you access multiple properties of that
> object? I'll illustrate #2:
>
> With ListView1.ListItems("Key1")
> .Text = "Item (Key1)"
> .Ghosted = True
> End With
>
> The ListItems property of the ListView control returns a reference to a
> ListItem object.
>
>
>

Bob Butler

2004-10-27, 3:55 pm

"John" <John@discussions.microsoft.com> wrote in message news:9841B481-
8A37-441B-A8A7-5A8F29075D1A@microsoft.com
> Okay that makes sense.
>
> So how would I implement this in my own code? For example, I have
> two Classes: Class1 (containing Property1, Property2 and Method1)
> and Class2 (containing Property3 and Method2).
>
> How do I create a reference to Class2's members using a property in
> Class1?
>
> More specifically, what is the syntax for the Property Get/Let
> statements in Class1 in order to achieve this "reference" to Class2?



----------------------------------In Class1:

Private moC2Ref As Class2
Private mlProperty1 As Long
Private mlProperty2 As Long

Private Sub Class_Initialize()
Set moC2Ref = New Class2
End Sub

Private Sub Class_Terminate()
Set moC2Ref = Nothing
End Sub

Public Property Get C2Ref() As Class2
Set C2Ref = moC2Ref
End Property

Public Property Let C2Ref(ByVal NewC2Ref As Class2)
moC2Ref = NewC2Ref
End Property

Public Property Get Property1() As Long
Property1 = mlProperty1
End Property

Public Property Let Property1(ByVal NewProperty1 As Long)
mlProperty1 = NewProperty1
End Property

Public Property Get Property2() As Long
Property2 = mlProperty2
End Property

Public Property Let Property2(ByVal NewProperty2 As Long)
mlProperty2 = NewProperty2
End Property

Public Sub Method1()
MsgBox "In Method1 of Class1", vbOKOnly, "C1.M1"
End Sub

----------------------------------In Class2:

Private mlProperty3 As Long

Public Property Get Property3() As Long
Property3 = mlProperty3
End Property

Public Property Let Property3(ByVal NewProperty3 As Long)
mlProperty3 = NewProperty3
End Property

Public Sub Method2()
MsgBox "In Method2 of Class2", vbOKOnly, "C2.M2"
End Sub

----------------------------------In the calling code:

Dim oC1 As Class1
Dim oC2 As Class2
Set oC1 = New Class1
Set oC2 = oC1.C2Ref
oC2.Method2
Set oC2 = Nothing
Set oC1 = Nothing


--
Reply to the group so all can participate
VB.Net... just say "No"

John

2004-10-27, 3:55 pm

Ahhh! Excellent. Thank You Bob!

Thanks again guys. I truly appreciate your help!

I look forward to implementing this new technique!

John
Larry Serflaten

2004-10-27, 3:55 pm


"Bob Butler" <tiredofit@nospam.com> wrote

> Public Property Let C2Ref(ByVal NewC2Ref As Class2)
> moC2Ref = NewC2Ref
> End Property


Interesting, does that work? (I didn't test it)

I've always implemented that part of it as:

Public Property Set C2Ref(ByRef Value As Class2)
Set moC2Ref = Value
End Property


???
LFS

Ken Halter

2004-10-27, 3:55 pm

Bob Butler wrote:
>
> Public Property Let C2Ref(ByVal NewC2Ref As Class2)
> moC2Ref = NewC2Ref
> End Property


Minor mod... (if I'm not overlooking something)

Public Property Set C2Ref(ByVal NewC2Ref As Class2)
Set moC2Ref = NewC2Ref

--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
Please keep all discussions in the groups..
Bob Butler

2004-10-27, 3:55 pm

"Ken Halter" <Ken_Halter@Use_Sparingly_Hotmail.com> wrote in message
news:uEmd%23%23DvEHA.2016@TK2MSFTNGP15.phx.gbl
> Bob Butler wrote:
>
> Minor mod... (if I'm not overlooking something)
>
> Public Property Set C2Ref(ByVal NewC2Ref As Class2)
> Set moC2Ref = NewC2Ref


You aren't overlooking anything... I used an old code generator to create
the property procedures and that's a bug that I've never taken the time to
go back and fix. Since I was never calling the Property Let I didn't run
into the error. BTW, it is also valid to use this:
Public Property Let C2Ref(ByVal NewC2Ref As Class2)
Set moC2Ref = NewC2Ref
End Property

That allows you to set the object without using the Set keyword as in
oC1.C2Ref = New Class2
Whether or not that's a good option to allow may be debatable but it works.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com