Need definition of Field vs Property

I

Internet User

What is the definition of a field in VB.net (other than database functions)?
How does one distinguish between a "field" and a "property"?
Or are fields and properties related to each other in some way?

Thanks in advance.
 
G

Guest

Internet User,

A field is a public variable in a class. As such it can be directly accessed
from an object created from the class.

A property is a set of accessor methods (Get/Set) that mediate access to
data stored in a private variable in a class. As such, the data cannot be
directly access from an object created from the class.

Kerry Moorman
 
B

Brian Gideon

Internet User,

A field is a public variable in a class. As such it can be directly accessed
from an object created from the class.

A field is any class level variable whether public, private, or
internal.
 
A

AMDRIT

You can search google for a whole host of answers and suppositions.

I think of fields as state and data of the object, while properties are
controlled interaction with the object's state and data.

example: (from here you surmize your own thoughts)

'State and data for our person object
private pstrPersonName as string
private pdteDateOfBirth as date
private pblnIsDirty as boolean

Public Event StateChanged(sender as object, e as system.eventargs)

'Provide information about the object's data with this state flag (Is Dirty)
' we assign it as readonly in this property because we do not
' want consumers to inadvertantly mess it all up.
public readonly property IsDirty as boolean
get
return pblnIsDirty
end get
end property

'Provide controlled access to Date of Birth
' the control is that our person must be within driving age
' (16-71), reject the update if the person is not in that range.
public property DateOfBirth as Date
get
return pdteDateOfBirth
end get
set (value as date)

dim iTemp as integer = datediff("yyyy",value, now)

if itemp >=72 or itemp < 16 then 'Cannot have a driver's license
'throw an exception DateOfBirth is invalid
end if

pdteDateOfBirth = value
pblnIsdirty = true
raiseevent StateChanged(me,new systemeventargs)
end set
end property

'Instruct the object to save it's data and update its state
public sub Save()
if not me.IsDirty then exit sub
'perform save logic
pblnIsDirty = false
raiseevent StateChanged(me,new systemeventargs)
end sub
 
M

Martin H.

Hello Internet User,

In addition to Amdrit's comment:
I think of fields as state and data of the object, while properties are
controlled interaction with the object's state and data.

A property is a Function (Get) /Sub (Set) combination which usually has
a variable behind.

The advantage of the property to a plain (public) variable is that you
can react on changes of that variable. That might be to send an error if
a value is OK with the variable, but you don't want to allow it (e.g.
transparent for a color) or if you need to recalculate something in your
class after the variable has been changed.

Best regards,

Martin
 

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

Top