Results 1 to 3 of 3

Thread: Classic VB - How do I make properties?

Threaded View

  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.

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