Results 1 to 3 of 3

Thread: Classic VB - How do I make properties?

  1. #1

    Thread Starter
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Classic VB - How do I make properties?

    You can make properties for the following things: Forms, UserControls and Class Modules. Basically any element that will behave as an object.


    When to use properties?

    Properties are handy when you want to generalize your code: if you're changing a setting of something, it might be better to create it as a property. Especially if you might have a need to get a return value as well. The property code can then do some additional stuff, such as switching form's menu items automatically to correct value, so you don't need to do it from whereever you set the property.


    Property Get

    Property Get is rather simple: it always returns a value.
    VB Code:
    1. ' this could be in a usercontrol
    2. Dim m_Caption As String
    3.  
    4. Public Property Get Caption() As String
    5.     Caption = m_Caption
    6. End Property

    After this you could call it like this from a form the usercontrol is placed on:
    VB Code:
    1. Me.Caption = UserControl1.Caption


    You can also pass objects:
    VB Code:
    1. ' again, in a usercontrol
    2. Dim m_Picture As IPictureDisp
    3. ' reference to "Ole Automation" required! by default it is included
    4. Public Property Get Picture() As IPictureDisp
    5.     Set Picture = m_Picture
    6. End Property

    In this case you would call it like this:
    VB Code:
    1. Set Me.Picture = UserControl.Picture


    Property Let and Property Set

    Property Let is used when you pass a variable. Set is used when you pass an object.

    VB Code:
    1. Dim m_Caption As String
    2.  
    3. Public Property Let Caption(ByVal NewCaption As String)
    4.     m_Caption = NewCaption
    5. End Property

    Usage:
    VB Code:
    1. UserControl1.Caption = "This is my control!"


    VB Code:
    1. Dim m_Picture As IPictureDisp
    2.  
    3. Public Property Set Caption(ByRef NewPicture As IPictureDisp)
    4.     Set m_Picture = NewPicture
    5. End Property

    Usage:
    VB Code:
    1. Set UserControl1.Picture = Picture1.Picture


    Some notes about the examples

    I kept the examples as clean as possible. For this reason, I didn't include additional coding that would redraw the usercontrol for example. Redrawing would be placed in the property handling as otherwise your control would never change. It is a good idea to make a general redrawing procedure within your usercontrol to keep it visually updated with ease.

    The same applies for forms and class modules, though class modules aren't visible. You could use class modules with events though, so when a property gets changed, an event would fire:

    VB Code:
    1. ' in a form
    2. Dim WithEvents MyClass As clsMyClass

    This, however, is out of our topic.


    Adding it all up

    - use a property instead of a sub or function if you need to also get the value
    - use a sub if you just need to set something
    - use a function if you need to set something and require a return value of the result (such as if the function succeeded)
    - Property Get works with any data, be it a regular variable or an object
    - Property Let is for regular variables
    - Property Set is for objects



    Comments and feedback
    Send these via a private message; the replies in the FAQ forum can only contain new information regarding the subject. So any questions, thank yous etc. via a pm
    Last edited by si_the_geek; Oct 14th, 2005 at 07:02 PM.

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Arrow Re: Classic VB - How do I make properties?

    You can also specify parameters for property methods.

    A common example of this is when writing a collection class.

    VB Code:
    1. Property Get Item(ByVal Index As Long) As MyClassType
    2.     Set Item = BaseCollection(Index)
    3. End Property
    4.  
    5. Property Set Item(ByVal Index As Long, ByRef pObject As MyClassType)
    6.     Set BaseCollection(Index) = pObject
    7. End Property

    The function signatures (type and number of arguments) for the Get and Set/Let methods must match, save for the additional final parameter on the Set/Let routine which is what is being assigned to the property.

    As Merri mentioned, Property Set is called when you assign an object reference using the Set keyword. The object is passed by reference. Property Let is called when you assign a value type (Long, String etc.) optionally using the (deprecated) Let keyword, and it is passed by value.

    So, you would use the above property Get/Set methods like this:
    VB Code:
    1. Dim AnObject As MyClassType
    2. Set AnObject = MyCollectionClass.Item(5)  ' Get
    3. Set MyCollectionClass.Item(5) = AnObject  ' Set

    It works, but you may notice that the built-in VB Collection class has a slightly neater syntax:
    VB Code:
    1. Set AnObject = MyCollection(5)
    To accomplish this, you need to set the Item property as the default method of your collection class. To do this, click anywhere in your procedure, and from the Tools menu, select Procedure Attributes. Click Advanced to expand the dialog, and from the Procedure ID dropdown, select (Default). This will enable the use of the shorthand syntax shown above.

    Also, while you are in that dialog box you will see some other interesting options, which can be saved for another topic

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Classic VB - How do I make properties?

    Another tidbit or two about properties.

    In a usercontrol, if you want to persist the property, you should add the PropertyChanged method when the property changes. Of course you will need to cache the property in the usercontrol's WriteProperty event.
    vb Code:
    1. Dim m_Picture As IPictureDisp
    2.  
    3. Public Property Set Picture(newPic As IPictureDisp)
    4.     Set m_Picture = newPic
    5.     PropertyChanged "Picture"
    6. End Property

    Ever notice that you can assign an image to Picture property using the keyword Set and without using that keyword?
    vb Code:
    1. Set Me.Picture = LoadPicture(filename)
    2. Me.Picture = LoadPicture(filename)
    Well if you code your custom "Picture" property with Let and your customer uses Set keyword, an error will occur. Likewise, if you code "Picture" with Set and your customer does not use the Set keyword, an error will occur.

    By using both Let and Set, you can save your customers from those annoying errors. Though this example uses a picture object in the sample code, this same logic can be used for any properties that use Objects.
    vb Code:
    1. ' coded within a class or usercontrol
    2. Dim m_Picture As IPictureDisp
    3.  
    4. Public Property Get Picture() As IPictureDisp
    5.     Set Picture = m_Picture
    6. End Property
    7. Public Property Set Picture(newImage As IPictureDisp)
    8.     Set m_Picture = newImage
    9.     ' do other things as needed, like redrawing after image changed, validating, etc
    10. End Property
    11. Public Property Let Picture(newImage As IPictureDisp)
    12.     Set Me.Picture = newImage
    13.     ' re-route the Let call to our Set call
    14. End Property
    Last edited by LaVolpe; Dec 4th, 2009 at 11:27 AM. Reason: correct couple of typos
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width