Solved

Class-module (property or public)

Posted on 2001-08-31
10
422 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
which modules are active in VB6 project? 6 56
DBCtrlGrid, Delphi, Scroll 8 82
Dinamic report to Crosstab query 9 59
Connect to SQL 2008 r2 server over the Internet 4 74
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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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 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…

737 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