Solved

Class-module (property or public)

Posted on 2001-08-31
10
413 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
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
Comment Utility
Thanks, exactly what I wanted to now!
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 15

Expert Comment

by:ameba
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now