Get .... EndGet

  • Thread starter Thread starter Kalim Julia
  • Start date Start date
K

Kalim Julia

Anybody can explain me what is
1. Get ..... EndGet
2. Set ...... EndSet

Please send me any example.
 
: Anybody can explain me what is
: 1. Get ..... EndGet
: 2. Set ...... EndSet
:
: Please send me any example.


They relate to properties.


As an example, say you have a variable in your class that you want to
expose. Users are allowed full access to read the variable, but you don't
want users to give the variable a value greater than 100. If you make the
variable public, users can assign any value at all to it. By using a
property however, you can trap invalid values.


----------------------------------
Private mSomeVariable As Integer

Public Property SomeVariable() As Integer
Get
Return mSomeVariable
End Get

Set '(input parameter 'value' implied)
If value > 100 Then
Throw New Exception("value cannot exceed 100")
Else
mSomeVariable = value
End If
End Set
End Property
----------------------------------


Another use would be if you want to allow users to see the value of the
variable but do not want them to be able to change it. A 'read only'
property allows that.


----------------------------------
Private mSomeVariable

Public Readonly Property SomeVariable
Get
Return mSomeVariable
End Get
End Property
 
Kalim Julia said:
Anybody can explain me what is
1. Get ..... EndGet
2. Set ...... EndSet

Enter "Get keyword" in the documentation's index...
 
I've tried it in my program (set validation not exceed 100)
====================================
Private mlVARIABLE As Integer
Public Property _mlVARIABLE() As Integer
Get
Return mlVARIABLE
End Get
Set(ByVal Value As Integer)
If Value > 100 Then
Throw New Exception("tidak kurang dari 100")
Else
mlVARIABLE = Value
End If
End Set
End Property

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
mlVARIABLE = 200
mSomeVariable = 153
End Sub
====================================
But when I assign mlVARIABLE = 200, it just goes through without any
validation message. Why ?
 
Kalim Julia said:
I've tried it in my program (set validation not exceed 100)
====================================
Private mlVARIABLE As Integer
Public Property _mlVARIABLE() As Integer
Get
Return mlVARIABLE
End Get
Set(ByVal Value As Integer)
If Value > 100 Then
Throw New Exception("tidak kurang dari 100")
Else
mlVARIABLE = Value
End If
End Set
End Property

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
mlVARIABLE = 200
mSomeVariable = 153
End Sub
====================================
But when I assign mlVARIABLE = 200, it just goes through without any
validation message. Why ?

A prioperty is only any use if you actually make use ofit.
Your code is setting the Private member variable (mlVARIABLE)
directly, bypassing the Property completely. Use

Me._mlVARIABLE = 200

instead.

BTW, I think it is customary to have the /member/ variable with
the '_'-prefixed name, and the Property with just the simple name,
as in

Private _mlVARIABLE as Integer = 0

Public Property Variable() as Integer
.. . .

then

Me.Variable = 200

HTH,
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top