Solved

Class-module (property or public)

Posted on 2001-08-31
10
421 Views
Last Modified: 2013-11-23
What's the difference between these code-samples when they are written in a class module? They seem to give the same result... which method should I prefer to use. I've heard that public variables shouldn't be used too much.

'-----------------------
'Sample 1
Public XCoord As Single


'-----------------------
'Sample 2
Private mvarX As Single

Public Property Get XCoord() As Single
    XCoord = mvarX
End Property

Public Property Let XCoord(vData As Single)
    mvarX = vData
End Property
0
Comment
Question by:Marshow
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 8

Accepted Solution

by:
Dave_Greene earned 100 total points
ID: 6446497
They are essentially the same.  Only with the Get / Let combination you can ensure that the data is valid before accepting it.

Also you can use GET to get a value, but without coding the LET statement it cannot be changed.
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 6446506
Primary advantage is if you want to add code to the property.  Code might be a conversion, verification, whatever.:

Public Property Get XCoord() As Single
   if mvarX < 0 then
      XCoord = 0
   else
      XCoord = mvarX
   end if
End Property

Public Property Let XCoord(vData As Single)
   mvarX = vData
End Property

Just because you don't think you want to do this now, plan for the future, because then you can modify the code without rewriting the whole class.
0
 
LVL 8

Expert Comment

by:Dave_Greene
ID: 6446512
An Example
Say you have the following code...


Private mvarX As Single

Public Property Get Key() As Single
   Key = mvarX
End Property

Public Function DoLookup()
   'make a call to the database
   'set the key value
   mvarX = RS.Fields("KEY")
End Function

'
Now if you are coding against the class

dim oClass as Class1

oClass.DoLookup

msgbox oClass.Key  'Will return the value from the lookup]

oClass.Key = "1234"  ' Will throw an exception.  Cannot be modified
 
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6446566
If you use a public var in a class module, VB automatically converts it as a pair of porperty let/get or set/get if is a object var.
Using last approach (Create pair by yourself) is a better programming practice and you will have more control (validate dat could be one advantage)
Cheers
0
 

Author Comment

by:Marshow
ID: 6446665
Thanks, exactly what I wanted to now!
0
 
LVL 15

Expert Comment

by:ameba
ID: 6446685
With private objects, syntax 2 won't work if used in public class module.

Public TextBox As TextBox  ' This is OK.

Property Get TextBox() As TextBox   ' compile error
End Property
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 6446840
You *can* name a variable after an object class, but is it a good idea?  It generally only leads to confusing code.

This works ok as well:

Option Explicit
Private mTxt As TextBox

Public Property Get txtBox() As TextBox
    Set txtBox = mTxt
End Property

Public Property Set txtBox(ByVal vNewValue As TextBox)
    Set mTxt = vNewValue
End Property
0
 
LVL 15

Expert Comment

by:ameba
ID: 6446853
Yes, PaulHews
That works in Standard EXE.

I said it won't work in "public class module" - start new ActiveX EXE, paste that code to a class1, press Ctrl+F5 and there will be compile error:
"Private object modules cannot be used in Public code modules as parameters or return types for public procedures, as ..."
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 6446895
Ok, I see what you mean.  Interesting that just exposing it as a public variable works.  I thought that COM just intrinsically did the same thing whether you expose the object through a property or through a variable.  Hm.  Learn something new everyday around here.  :)
0
 
LVL 15

Expert Comment

by:ameba
ID: 6446905
When there is a need to pass control between Standard EXE and DLL, people normally change declaration:

Public Property Get txtBox() As TextBox

to:

Public Property Get txtBox() As Object

and that works.  But that is late bound call.  By using normal public variable, we can have early bound call and type checking.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question