Class Basics (Slightly OT?)

S

Simon Harris

Hi All,

Being a classic ASP programmer, I'm trying to get to grips with OOP,
specifically using classes.

I have setup my class with various properties, so far so good. What I dont
quite get is the logic of using the properties.

E.g. If I want to get the RoadName property of my address class into a
database, do I do something like this...

Public function saveToDB()
strSQL = "Insert into TblName RoadName Values " & address.RoadName
bla bla bla....run the sql here!
End Function

Also, if I want to get a specific address from my DB and create an address
object from it, do I do something like this?

Public Function GetAddress(byval RoadName as String)
strsql = "select roadname fromtblname where roadname = " &
roadname
bla bla...run the SQL
address.roadname = dr("roadname")
End Function

I guess I just need someone to confirm my thinking is right on this, or if
not, point me in the right direction! :)

Thanks,
Simon.
 
S

Scott M.

You've got it.

Properties are nothing more than variables that are specific to an object.
 
K

Kevin Spencer

Properties are nothing more than variables that are specific to an object.

Well, not exactly. FIELDS are variables that are global to a class. A
Property is actually a special entity that is composed of one or 2 methods
(functions), one for getting, and/or one for setting the Property. In other
words, a field is state, while a Property is process. A field is a container
for data. A Property is an amalgam of one or more processes that either
return or process data. Typically, a Property is used to expose and
encapsulate a Field. However, a Property doesn't have to be linked to any
data at all.

To give you an idea of what I'm describing, consider the 2 following
examples:

// Property that exposes a field
public class foo
{
private int _bar = 0; //field
public int bar // property
{
get
{
return _bar;
}
set
{
_bar = value;
}

}
}

public class foo
{
public int bar //property
{
get
{
return 0;
}
}
}

The simplified example above doesn't do justice to the possibilities of
properties. For example, let's say you create a class that draws a
rectangle. You could create a read-only property of that class that returns
the area of the rectangle, by multiplying the length times the width.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.
 
S

Scott M.

You're right Kevin. I thought that to go into that detail would be
splitting hairs. I thought (and still do) that the general analogy of
property to variable is a good beginning way to think about properties since
the purpose of a property is to maintain some sort of state of the object
and the purpose of a variable is to maintain some user defined state
information.
 

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

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top