Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Class-module (property or public)

Posted on 2001-08-31
10
Medium Priority
?
430 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 400 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 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…
Suggested Courses

604 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