Default property for class 
Author Message
 Default property for class

Hello,

I know it is possible to implement a default property for your own classes
(IUnknown....).
Is it possible to implement a default property which allows me to address it
in the following manner:

MyClass("something") = "value"

Or isn't this possible?

Thanks



Mon, 08 Dec 2003 15:13:55 GMT  
 Default property for class


Quote:
>Hello,

>I know it is possible to implement a default property for your own classes
>(IUnknown....).
>Is it possible to implement a default property which allows me to address it
>in the following manner:

>MyClass("something") = "value"

>Or isn't this possible?

>Thanks

Yes.

You just set all the default properties in the Initialize Sub of the
class.

Ie

Private Sub Class_Initialize()
      '  Initialize your class variables here        
End Sub

Richard.



Mon, 08 Dec 2003 18:13:53 GMT  
 Default property for class
That isn't what I mean. I will be a bit more specific.

*** Code in the class (not all, for the idea)

'Global declarations
private m_colValues as Collection

Private Sub Class_Initialize()
    set m_colValues = new Collection
    m_colValues.Add "Value1", "1"
    m_colValues.Add "Value2", "2"
End Sub

Public Property Get ValueWithId(Byval sId as String) as String
'Retrieves an value with specified Id from collection
On Error Resume Next
    ValueWithId = m_colValues(sId)
End Property

Public Property Let ValueWithId(Byval sId as String, Byval sValue as String)
' I know this isn't correct, but I want to do something like this
'Stores value with specific Id to collection
On Error Resume Next
    m_colValues(sId) = sValue
End Property

*** Code I use on form; This is the code I DONT want to use
Private Sub Command1_Click()
   MyClass.ValueWithId("1", "NewValue") 'Sets value for collection item with
key '1' to 'NewValue'
End sub

*** Code I use on form; This is the code I WOULD like to use
Private Sub Command1_Click()
   MyClass.ValueWithId("1") = "NewValue" 'Sets value for collection item
with key '1' to 'NewValue'
End sub



Mon, 08 Dec 2003 19:32:56 GMT  
 Default property for class
If the property 'ValueWithId' is the property you'd like to have
as the default property, position the cursor inside the
property get or property let procedure, popup the Tools menu
and select procedure attributes.
In that dialog window, click advanced and select '(default)' in the
the Procedure ID combo.

Hope this helps

Alex


Quote:
> That isn't what I mean. I will be a bit more specific.

> *** Code in the class (not all, for the idea)

> 'Global declarations
> private m_colValues as Collection

> Private Sub Class_Initialize()
>     set m_colValues = new Collection
>     m_colValues.Add "Value1", "1"
>     m_colValues.Add "Value2", "2"
> End Sub

> Public Property Get ValueWithId(Byval sId as String) as String
> 'Retrieves an value with specified Id from collection
> On Error Resume Next
>     ValueWithId = m_colValues(sId)
> End Property

> Public Property Let ValueWithId(Byval sId as String, Byval sValue as
String)
> ' I know this isn't correct, but I want to do something like this
> 'Stores value with specific Id to collection
> On Error Resume Next
>     m_colValues(sId) = sValue
> End Property

> *** Code I use on form; This is the code I DONT want to use
> Private Sub Command1_Click()
>    MyClass.ValueWithId("1", "NewValue") 'Sets value for collection item
with
> key '1' to 'NewValue'
> End sub

> *** Code I use on form; This is the code I WOULD like to use
> Private Sub Command1_Click()
>    MyClass.ValueWithId("1") = "NewValue" 'Sets value for collection item
> with key '1' to 'NewValue'
> End sub



Mon, 08 Dec 2003 20:03:45 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Default property in class?

2. Default Properties for class modules: Anyone remember how its done?

3. Class Module Default Property

4. Class default property ?

5. Default property of Win32_Printer class in Win 2000?

6. Default properties for VBScript classes

7. NEWBIE CLASS PROPERTY DEFAULT VALUE

8. NEWBIE CLASS PROPERTY DEFAULT VALUE

9. default property not displayed in properties window.

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

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

12. Properties Property for a Custom Class

 

 
Powered by phpBB® Forum Software