Solved

Class-module (property or public)

Posted on 2001-08-31
10
416 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
bit defender blocks good applications 2 82
message box in access 4 41
Delphi: Connect to running MS Outlook 4 49
When i run adoquery my application freezes 26 152
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

867 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

25 Experts available now in Live!

Get 1:1 Help Now