Class Property Question -- Different Arguments in Get and Let 
Author Message
 Class Property Question -- Different Arguments in Get and Let

An excellent 1999 Rod Stephens article on algorithms and data structures
describes Sparse Arrays. He has a SparseArray Class with the following
Get/LLet definitions for Property Value:

Public Property Get Value (ByVal row_num As Integer, ByVal col_num As
Integer) As String
Public Property Let Value (ByVal row_num As Integer, ByVal col_num As
Integer, ByVal new_value As String)

After instantiating a new Value object, these properties are called thus:
Call to "Let":
    Private Sub btnSet_Click()
        SparseArray.Value(tbRow, tbCol) = tbVal    ' <--- THIS IS THE
STUMPER!

Call to "Get":
    Private Sub btnGet_Click()
        Dim sResult As String
        sResult = SparseArray.Value(tbRow, tbCol)

tbRow & tbCol are text boxes using default property "value".

Could somebody help me understand (1) how the property definitions for Get
and Let can use different numbers of arguments and (2) how the call to the
"Let" property works? I'm absolutely stumped how  SparseArray.Value(tbRow,
tbCol) = tbVal can assign the value of tbVal to the variable new_value in
Property Let Value.

When I break at entry to Property Let, the values for row_num and col_num
are passed immediately, but new_value is an empty string. The value for
new_value doesn't get created until after the "Public Property Let" line is
executed (like a function being executed).

Thanks for your Class expertise!
Steve



Thu, 24 Nov 2005 17:31:26 GMT  
 Class Property Question -- Different Arguments in Get and Let
Hi Steve,

    Property Get and Let statements always have different numbers of
arguments. The easiest way to understand this is to look at the simplest
case:

Property Get Demo() As Boolean
    Demo = bSomeStoredValue
End Property

Property Let Demo(ByVal bNewValue As Boolean)
    bSomeStoredValue = bNewValue
End Property

    The property Get statement requires no arguments because it's just
returning the value of some data stored internally by the class. The
property Let statement requires at least one argument, because it's setting
the state of some internal class variable.

    The property Get and Let statements can have additional arguments, in
the example you sited, these additional arguments are required to identify
exactly what needs to being returned or set from among a number of choices
in an array. Just remember the following two rules:

1) The property Get and Let statements must have exactly the same number and
type of *additional* arguments.

2) The last argument to property Let is special. That argument is passed in
by the assignment operator in a statement like:

        Class.Property(Arg1, Arg2) = Arg3

This is why you don't see it's value until after you've stepped over the
property Get definition line. The assignment hasn't taken place until you're
inside the property Get procedure. The additional arguments (Arg1 through
Arg2) have their values set before the property procedure is ever called, so
their values can be read before you step into the procedure. Hope that
answered all your questions, post back if not.

--
Rob Bovey, MCSE, MCSD, Excel MVP
Application Professionals
http://www.appspro.com/

* Please post all replies to this newsgroup *
* I delete all unsolicited e-mail responses *


Quote:
> An excellent 1999 Rod Stephens article on algorithms and data structures
> describes Sparse Arrays. He has a SparseArray Class with the following
> Get/LLet definitions for Property Value:

> Public Property Get Value (ByVal row_num As Integer, ByVal col_num As
> Integer) As String
> Public Property Let Value (ByVal row_num As Integer, ByVal col_num As
> Integer, ByVal new_value As String)

> After instantiating a new Value object, these properties are called thus:
> Call to "Let":
>     Private Sub btnSet_Click()
>         SparseArray.Value(tbRow, tbCol) = tbVal    ' <--- THIS IS THE
> STUMPER!

> Call to "Get":
>     Private Sub btnGet_Click()
>         Dim sResult As String
>         sResult = SparseArray.Value(tbRow, tbCol)

> tbRow & tbCol are text boxes using default property "value".

> Could somebody help me understand (1) how the property definitions for Get
> and Let can use different numbers of arguments and (2) how the call to the
> "Let" property works? I'm absolutely stumped how  SparseArray.Value(tbRow,
> tbCol) = tbVal can assign the value of tbVal to the variable new_value in
> Property Let Value.

> When I break at entry to Property Let, the values for row_num and col_num
> are passed immediately, but new_value is an empty string. The value for
> new_value doesn't get created until after the "Public Property Let" line
is
> executed (like a function being executed).

> Thanks for your Class expertise!
> Steve



Thu, 24 Nov 2005 18:33:20 GMT  
 Class Property Question -- Different Arguments in Get and Let
Rob...thanks again. Excellent, very easy to understand response. I
hadn't thought about the basic case you pointed out. And I had a feeling
that the last argument was special and is passed in via the reference
operator.

Steve



Quote:
> Hi Steve,

>     Property Get and Let statements always have different numbers of
> arguments. The easiest way to understand this is to look at the
> simplest case:

> Property Get Demo() As Boolean
>     Demo = bSomeStoredValue
> End Property

> Property Let Demo(ByVal bNewValue As Boolean)
>     bSomeStoredValue = bNewValue
> End Property

>     The property Get statement requires no arguments because it's just
> returning the value of some data stored internally by the class. The
> property Let statement requires at least one argument, because it's
> setting the state of some internal class variable.

>     The property Get and Let statements can have additional arguments,
>     in
> the example you sited, these additional arguments are required to
> identify exactly what needs to being returned or set from among a
> number of choices in an array. Just remember the following two rules:

> 1) The property Get and Let statements must have exactly the same
> number and type of *additional* arguments.

> 2) The last argument to property Let is special. That argument is
> passed in by the assignment operator in a statement like:

>         Class.Property(Arg1, Arg2) = Arg3

> This is why you don't see it's value until after you've stepped over
> the property Get definition line. The assignment hasn't taken place
> until you're inside the property Get procedure. The additional
> arguments (Arg1 through Arg2) have their values set before the
> property procedure is ever called, so their values can be read before
> you step into the procedure. Hope that answered all your questions,
> post back if not.



Thu, 24 Nov 2005 19:02:14 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Concept of Property Get/Set/Let in Class Module

2. Property Get, Set Let with Classes

3. Accessing Properties of One Class Module From Another Class Module

4. Exposing Properties collection in custom classes as many MS Classes do

5. Message Class Error: Message Class Property Unkown

6. Parent property for Classes - 2 questions

7. Properties Property for a Custom Class

8. Properties Property for a Custom Class

9. GetObject - class argument for word ver6?

10. Class Argument error on Chart Wizard


 
Powered by phpBB® Forum Software © phpBB Group