Hey there,
im new to the whole .net at the moment.
can anyone tell me what is the purpose of the property functions are.
EG:
Public Property fnName()
End Property
Cheers
Printable View
Hey there,
im new to the whole .net at the moment.
can anyone tell me what is the purpose of the property functions are.
EG:
Public Property fnName()
End Property
Cheers
When you're writing your class, you can add a property there, much like you've shown in your post.
The purpose of this is that it allows you to include any functionality, checks, constraints, etc. on the input that the property receives. For example, if you have a Gender() Property, you do not want it to receive a value like "Cander" or "Rude". So you can include some code within your Property to check whether the value being received is correct or not!
Another example of a constraint would be to check if its a proper date, to check the length, to check the integer value being received (you might want to set an upper limit), etc.
But would it not be much the same to do that on the UI form.
Also how are they called, or is it an automatic check that is performed when tryin to assign a value to a variable, eg: the code below
Private m_id As Integer
Public Property id() As Integer
Get
Return m_id
End Get
Set(ByVal Value As Integer)
m_id = Value
End Set
End Property
No, you're saving on code, time and the hassle of doing a check everytime a value is passed to the property.Quote:
Originally posted by Strider
But would it not be much the same to do that on the UI form.
Here, I'm not letting the value EVER go above 20.Quote:
Private m_id As Integer
Public Property id() As Integer
Get
Return m_id
End Get
Set(ByVal Value As Integer)
If Value > 20 Then
'Throw an exception or
m_id = 20
Else
m_id = Value
End If
End Set
End Property
ok i get it but when you try an assign a value to m_id does it automatically go to the public property id(), or how is it called
Think in terms of flow. When you assign it to m_id, the next time you try to get that value, the Get() gives you m_id.Quote:
Originally posted by Strider
ok i get it but when you try an assign a value to m_id does it automatically go to the public property id(), or how is it called
The properties you create are just like the properties that show in the properties window for the controls.
If you create a class blah, then:
VB Code:
Dim dull as blah dim i as integer dull.id = 10 i = dull.id
Salv, where's my Stephen Hawking paper?!
Didn't you get it? I sent it to you. Must be that darn entropy again.