PDA

View Full Version : Solved: What is Property Let and Property Get ?



Erdin? E. Ka
11-11-2006, 02:06 PM
Hi there, :hi:

As my teachers known, nowadays i have wondering much about Class Modules..:*)

But there isn't improving for me. In spite of i studing much about it last two days but still i couldn't write and sample about Property Let and Property Get.

I researched some Internet pages about it and researched Google Search.

I have got some samples now but i can't understand yet.

Actually i am not a programmer. Maybe the problem is my capacity is not enaugh to quick learn. :dunno :rotlaugh:

Could you give me a "very easy sample" for beginning?

Thank you very much...

( Sorry, i wrote wrong the header of the thread ... )

lucas
11-11-2006, 02:35 PM
Header fixed....

lucas
11-11-2006, 02:49 PM
I have attached an example(fairly simple)using a class file to toggle buttons on a form. I think Jake originally posted this some time back.

You need to look at the form and its code...the buttons are in a frame for example.

There are 2 procedures in the class file. One that gives a messagebox telling which button is depressed and the other identifies the button by number when you hover over it.

I'm not much help on class files but there are lots of people here who can answer your questions once you've had a chance to look it over.

Hope this helps.

Erdin? E. Ka
11-11-2006, 03:20 PM
Header fixed....


Hi,

Thank you very much lucas. :thumb


I have attached an example(fairly simple)using a class file to toggle buttons on a form. I think Jake originally posted this some time back.

You need to look at the form and its code...the buttons are in a frame for example.

There are 2 procedures in the class file. One that gives a messagebox telling which button is depressed and the other identifies the button by number when you hover over it.

I'm not much help on class files but there are lots of people here who can answer your questions once you've had a chance to look it over.

Hope this helps.


lucas, thank you very much your kindly and useful help. It's a very understandable sample for Class Module. It will be to useful for me. :friends:

Also i am waiting for Property Get and Property Let sample.

Thanks in advance.:hi:

Norie
11-11-2006, 03:25 PM
erdinc

It might help if you explained how you might want to use Property Get/Let.

I'm sure if you do that then somebody will be able to give an example.

Erdin? E. Ka
11-11-2006, 04:08 PM
...It might help if you explained how you might want to use Property Get/Let...

Hi Norie :hi: ,

Actually my problem is this :think: , there is no any problem. I just want to learn of Property Get/Let's properties, when i must/should use these?, what is the advantages of Property Get/Let's properties... something like this... bla bla bla...

If i know that "what is Property Get/Let", then i will studing about it. And then maybe i can ask a specific question about it.

Now i am unemployed, and i don't want to spend my time for free, i want to learn something untill get a job. Nowadays i am interesting about Class Module, XLL, SmartTag, SmartDocument.

I came across to Property Get/Let while i am researching about these matters.

So bad thing "being inexperinced"! :help

Thank you very much for all...

SamT
11-11-2006, 07:28 PM
Custom Class is the same as Custom Object.
Objects have Properties, Methods, Events, Collections, and other Objects.

Property [Set\Get\Let] procedures are in your object. If you write a new Class called Translator and you make a Property Set procedure inside it called Text. Text Procedure maybe a Case; Case ANSII Do AnsiiThing, Case Unicode Do UnicodeThing, Case Etc.....

I get your new Tanslator Module. My VBA call to set it's Text Property looks like


Case Email Sender = Girlfriend Then
Translator.Text = Ansii
Case Sender = Erdin? Then
Translator.Text = Unicode



SamT

Erdin? E. Ka
11-11-2006, 10:04 PM
Hi SamT,
First of all i want to say, "please excuse me". Because, you try to help me but i can't learn something via your helps...

I think your help like a jewellery for who can understand. But i am not good at English. For that i want to see a completed easy sample.

I am sorry, i think i expend your precious times.

Thanks.

Bob Phillips
11-12-2006, 03:27 AM
Erdin?,

This is not a simple exercise, but we can do it. What I will do is feed you some files one by one, and you get to grips with it before we go to the next.

We'll use the (not the greatest IMO) standard employee example, and the first file is attached.

Take a look, understand it and we can move on. Try and work out what the major constraints of this file are.

mdmackillop
11-12-2006, 03:29 AM
It's not code I've ever used, but here's the examples from Excel 2003 VBA Help, combined into one one routine.

Dim CurrentColor As Integer
Const BLACK = 0, RED = 1, GREEN = 2, BLUE = 3

' Set the pen color property for a Drawing package.
' The module-level variable CurrentColor is set to
' a numeric value that identifies the color used for drawing.

Property Let PenColor(ColorName As String)
Select Case ColorName ' Check color name string.
Case "Red"
CurrentColor = RED ' Assign value for Red.
Case "Green"
CurrentColor = GREEN ' Assign value for Green.
Case "Blue"
CurrentColor = BLUE ' Assign value for Blue.
Case Else
CurrentColor = BLACK ' Assign default value.
End Select
End Property

' Returns the current color of the pen as a string.

Property Get PenColor() As String
Select Case CurrentColor
Case RED
PenColor = "Red"
Case GREEN
PenColor = "Green"
Case BLUE
PenColor = "Blue"
End Select
End Property

Sub pen()
' The following code sets the PenColor property for a drawing package
' by calling the Property let procedure.
PenColor = "Red"

' The following code gets the color of the pen
' calling the Property Get procedure.
ColorName = PenColor

MsgBox ColorName
End Sub

SamT
11-12-2006, 04:33 AM
Dr Mack,
As I understand, or more likely, misunderstand, the Property Statements above are inside the Drawing package, but the Sub Pen() is not.

The Sub Pen() is used in the module that is calling the drawing Package.

Is my understanding correct. :dunno

mdmackillop
11-12-2006, 05:31 AM
Here a revised example. The purpose is to set CurrentColor, which is the integer value of the colour to be used by the drawing programme (In this case the drawing programme is the sub DrawLine)

Option Explicit
Option Compare Text

Dim CurrentColor As Integer
Const BLACK = 8, RED = 10, GREEN = 11, BLUE = 12

' Set the pen color property for a Drawing package.
' The module-level variable CurrentColor is set to
' a numeric value that identifies the color used for drawing.

Property Let PenColor(ColorName As String)
Select Case ColorName ' Check color name string.
Case "Red"
CurrentColor = RED ' Assign value for Red.
Case "Green"
CurrentColor = GREEN ' Assign value for Green.
Case "Blue"
CurrentColor = BLUE ' Assign value for Blue.
Case Else
CurrentColor = BLACK ' Assign default value.
End Select
End Property

Sub SetColour()
Dim ColorName As String
' The following code sets the PenColor property for a drawing package
' by calling the Property let procedure.
PenColor = InputBox("Enter colour: Black, Red, Green or Blue")
DrawLine
End Sub

Sub DrawLine()
Dim sh As Shape
For Each sh In ActiveSheet.Shapes
If Left(sh.Name, 6) <> "Button" Then sh.Delete
Next
With ActiveSheet.Shapes.AddLine(10, 10, 250, 250).Line
.ForeColor.SchemeColor = CurrentColor
End With
End Sub

SamT
11-12-2006, 07:03 AM
Mack,

While you were posting the Revised Example, I was writing. Which probably explains my confusion on reading your post.

BTW, I really appreciate the time you and others take knocking sense into my head.

In re your revised example, I assume all the code is in the same module. It seems like a convoluted way to assign a value to the variable "CurrentColor." The Module has the Property "PenColor" but the Sub "DrawLine" uses the Variable instead of the Property.

Here Is what I was Writing instead of reading your post first. Could you critique it as to form only as I know it is not porperly cooded.


'PseudoCode for MyAccountingPackage
Public VAT As Boolean
Public SalesTax As Boolean
Public SEMT As Boolean ' Self EMployment Tax
Public CorpTax As Boolean

VAT = False
SalesTax = False
SEMT = False
CorpTax = False

Property Let Country(CountryName As String) 'Needs outside input to set value
Select Case CountryName
Case United States
SalesTax = True
Case Britain
VAT = True
End Select
End Property

Property Get Country() As String 'Calling program can determine what value "Country" has.
If VAT Then
Country = "Britain"
ElseIf SalesTax Then
Country = "US
Else Country = "the Country Property has not been set."
End If
End Property

Property Let Business(BusinessType As String)
Select Case BusinessType
Case Sole Proprieter
SEMT = True
Case Corporation
CorpTax = True
End Select
End Property

Property Get TaxRate() As Double 'ReadOnly Because no "Property Let TaxRate" above.
If SalesTax Then
TaxRate = .075
ElseIf VAT Then
TaxRate = .17
Else TaxRate = 0
End If
End Property

'More Statements
'End of MyAccountingPackage Code

'VBA PseudoCode for Book1.Module1
'Insert Early Binding to MyAccountingPackage Class
Private Sub App_WorkbookActivate()
MyAccountingPackage.Country = US
MyAccountingPackage.Business = Sole Proprieter
End Sub

'VBA PseudoCode for Book1.Sheet1
Sub ComputeTaxes()
Range(MyTaxesDue) = Range(AmountTaxesDueOn) * TaxRate
End Sub





Thanks mucho much

SamT

mdmackillop
11-12-2006, 07:18 AM
Hi Sam
As far as I can see, these functions have thier main uses in regard to Class modules, as per XLD's example. I would suggest following the logic there.
Regards
MD

stanl
11-12-2006, 08:59 AM
Now i am unemployed, and i don't want to spend my time for free, i want to learn something untill get a job.

I know how you feel. My position was terminated last week, along with meager health benefits just as my wife became ill.

I have often found it doesn't pay to get too technical with programming syntax. Get/Let is called Get/Put in .wsc Get/Set in other languages. Basically they are declared in pairs to define a variable that has scope (or is recognized) by other parts of the program. If there is not a Let for each Get then the variable (whether it is a property, a pointer or some other construct of a particular programming language) is read-only.

This may seem a bit off your original question, but this link
http://www.topxml.com/conference/wrox/2000_vegas/text/brianm_wsc.pdf

illustrates Get/Put in .wsc scripting and use of the free scripting wizard. I have used .wsc files a lot with Excel - as external class objects that can be re-used by any number of workbooks and can be modified during runtime.

I wish you luck in your job search. You appear to be a 'problem-solver' and in the end that may serve you as well if not better than being a programmer.
Stan

Ken Puls
11-12-2006, 11:12 AM
Hi Erdinc,

I'm sure these guys will have you well in hand, but I know that when I'm trying to learn, several examples can be helpful.

I have a couple of articles on my site which use Property Let and Property Get statements. Neither actually uses a standard class module, but rather the ThisWorkbook module and a Userform module. (Both of these are, of course, special types of class modules.)

The quick purpose of these statements is to create your own custom property, which will hold... something. (I'll explain with Value.) The Property Get returns the value of the property, and specified alone would give you a read only property. The Property Let statement allows you to set the value of the property. So using both Let and Get, you create a read/write property. You'll never use just Let alone.

As I said, I have two examples. The first is the easier one to follow, and that is how to control a debug mode for an Excel workbook. In this particular case, the property sits in the ThisWorkbook module, as compared to your standard class module. You can find it here (http://www.excelguru.ca/node/50).

My second example is a bit more complex, and uses the method to pass defaults to/from a userform. You can find it here (http://www.excelguru.ca/node/73). Be warned that you'd need to download the file, and step through the whole thing. It may not be easy code to follow, as it reproduces a VBA messagebox, so has a LOT going on in it.

My suggestion would be to look at the Debug Mode example. Step through the code Workbook_Open routine, then comment out the Property Let statement and try it again. Then try using the immediate window to get the value of the property. That should give you a feel for what each does.

After you have a handle on how that works, then try moving it into another class module.

HTH,

Erdin? E. Ka
11-12-2006, 11:29 AM
I'm thankful for the useful and genuine helps. Now i am getting leraning better, and i understand the matter.:hi:


Erdin?,

This is not a simple exercise, but we can do it. What I will do is feed you some files one by one, and you get to grips with it before we go to the next.

We'll use the (not the greatest IMO) standard employee example, and the first file is attached.

Take a look, understand it and we can move on. Try and work out what the major constraints of this file are.

Hi xld,

So, i want to move on with your Employee example. Now i want to add Monday Extra Hours for "Erdin?" employee.

For example;

Public Function WeeklyPay() As Double
WeeklyPay = mNormalHrs * Rate + mOverTimeHrs * Rate * 1.5 + (mMnd_Ext_Hrs * Rate * 2)
End Function

Then i added Monday_Extra_Hours = [E1] into the codes below:


Property Let HoursPerWeek(Hours As Double)
mNormalHrs = WorksheetFunction.Min(35, Hours)
mOverTimeHrs = WorksheetFunction.Max(0, Hours - 35)
Monday_Extra_Hours = [E1]
End Property


But it's looks like not useful, how should i change the codes for like below?

Public Sub SetEmployeePay()
Set Employee = New clsEmployee
With Employee
.Name = "Erdin?"
.Rate = 15
.Monday_Extra_Hours = 8
.HoursPerWeek = 35
End With
End Sub

Thanks a lot. :yes

Erdin? E. Ka
11-19-2006, 02:28 PM
Hi everyone!!! Here i am!!

Ken Puls, Sam, Stan, mdmackillop, xld, Lucas, Norie; I am so glad to your all kindly helps. Thank you very much. I was very busy last week and for that i couldn't an answer here.

I studied much about Class Modules, property let-get. And now i have more knowledges about these with your clear helps.

Best Regards.