|
-
Jul 31st, 2000, 06:58 PM
#1
Thread Starter
Junior Member
I'm working with classes with Private Variables. How exactly do Property Let and Get work to return and change the variables values?
-
Jul 31st, 2000, 07:35 PM
#2
Frenzied Member
Property Let/Get is how a class can expose a variable to the outside world say you have a property Value in a Class called clsMyClass
Here is the Code for the Class
Code:
Option Explicit
Dim m_Value as Integer 'Variable to store property
Public Property Get Value As Integer 'Property Get
Value = m_Value
End Property
Public Property Let Value (New_Value As Integer)
m_Value = New_Value
End Property
not very interesting I know
you make an instance of this class called objMyInstance (sorry about the microsoft help file type names but I'm in an unoriginal mood) and do a little code
Code:
Dim objMyInstance As New clsMyClass
objMyInstance.Value = 3
MsgBox objMyInstance.Value
The First line makes a new instance of the class called objMyInstance The Class then Sets aside memory for its internal Variable
the Second line Calls the Property Let procedure with New_Value set to 3 If you don't save the Value into a variable in the property let the class will not remember it.
The Second line Calls the Property Get procedure, as you can see this returns the Value of the variable (where I've gone Value = m_Value) this tells the message box to put up a value of 3
You're not restricted t0o having Simple Properties like this one, here is some code for a clsAngle I wrote to Get around the degrees/radians problems in VB (in VB everything has to be done in radians, this makes it difficult because most users like degrees)
Code:
Option Explicit
Const PI = 3.14159265358979
Dim m_Degrees As Integer
Dim m_Minutes As Integer
Public Property Let Degrees(New_degrees As Integer)
m_Degrees = New_degrees Mod 360
End Property
Public Property Get Degrees() As Integer
Degrees = m_Degrees
End Property
Public Property Let Minutes(New_Minutes As Integer)
m_Minutes = New_Minutes Mod 360
End Property
Public Property Get Minutes() As Integer
Minutes = m_Minutes
End Property
'This is the Value of the Angle in Radians
'It is set as the Default Property.
'This allows us to add angles and perform trig operations on it directly
'It is hidden to keep syntax consistent
Public Property Let Radians(New_Radians As Double)
Dim dblTemp As Double
'scale angle to Degrees.
dblTemp = New_Radians * 180 / PI
Me.Degrees = Fix(dblTemp + (1 / 120))
Me.Minutes = CInt(dblTemp * 60)
End Property
Public Property Get Radians() As Double
Dim dblTemp As Double
dblTemp = Me.Degrees + (CDbl(Me.Minutes) / 60)
Radians = dblTemp * PI / 180
End Property
Public Property Get StringValue() As String
If Me.Minutes = 0 Then
StringValue = Me.Degrees & "°"
Else
StringValue = Me.Degrees & "°" & Me.Minutes & "'"
End If
End Property
As you Can See the Properties are a bit more Complicated and I'm using 2 Variables for 3 properties, this is why the property Let/Get system is used, it allows for more sophisticated data exchange etc than just public variables
Hope this helps
-
Jul 31st, 2000, 07:50 PM
#3
Thread Starter
Junior Member
HEhehe. Thanks for the help. It looks like I was using it right, but I just forgot that I placed the testing code after some code that exited the function. Ooops.
-
Jul 31st, 2000, 09:59 PM
#4
Hyperactive Member
A lot of people are confused between Let, Set and Get.
Even more people are confused about the differences with Let and Set mainly because they don't understand Object Orientation very well.
I know I might be preaching to the converted here but this might clarify some things for others reading who came in to look because they are having problems as well
Let
"Let" basicially means "LET my variable have something placed inside it". This means you can set the values of properties such as strings, dates, integers and the like.
When you do this with a class it looks like this :
Code:
Dim m_fred as Integer
Public Property Let fred(InValue as Integer)
m_fred = InValue
End Property Let
Now that might seem redundant because it seems like a waste of code just to set a value. Why create another variable???
The reason for this is that you can put code inside the "Let" that validates its value first. This stops incorrect values from being assigned allowing you to either reset them to what they were before or to change them to null or blank.
Code:
Dim m_fred as Integer
Public Property Let fred(InValue as Integer)
If InValue > 0 and InValue < 20 Then
m_fred = InValue
End If
End Property Let
Get
"Get" basically means "Go and GET the value of that variable and bring it back for me". This allows you to return datatypes like strings, dates, integers etc.
When you do it as a class it looks like this :
(And again you can put code into it to help out)
Code:
Dim m_fred as Integer
Public Property Get fred() as Integer
If m_fred = 0 Then
m_fred = GetDefaultValue ' Or whatever you like
End If
fred = m_fred
End Property Get
As you can see you can not only retrieve the value but also perform some code to check what the starting value might be
Set
Code:
Set MyObj.list = New collection
This is where it gets tricky. "Set" basicially means "SET-up a new object and give me the pointer to where it is instead of the value inside it". Set is used when you want to point things at structures such as a new object, a collection, a control etc.
Code:
Dim m_list as Collection
Property Set list(NewObject as Collection)
If datatype(NewObject) = "Nothing" Then
Set m_list = New Collection
Else
Set m_list = NewObject
End If
End Property Set
What we have done here is made the variable inside the class (m_list) point to exactly the same area of memory as NewObject... so that both of them now point to the same structure, object, collection or whatever. This means inside the class we can do anything we like with m_list knowing it will use the same area of data as the object it was "Set" with.
Using the "New" keyword tells it to create a NEW and empty structure in memory and then assigns the variable to point to this new structure. As you can see from the above example if the object you are trying to set it with is empty it creates a new collection for itself.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|