Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 131,614 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 2,069 people online right now. Registration is fast and FREE... Join Now!




Properties vs. Attributes

 
Reply to this topicStart new topic

Properties vs. Attributes, Difference between properties and attributes

legend_018
post 4 Oct, 2008 - 01:03 PM
Post #1


New D.I.C Head

*
Joined: 11 Sep, 2008
Posts: 31

Hi. I'm trying to learn c# using some online tutorials. At one point, it was teaching me that objects are defined by attributes (data) and behaviors. Example of an Object could be a person with eye, color, and height as attributes.

Than later on, I'm learning about properties. The examples give height and width.

So than i'm thinking, hmmm height is giving as an example as an attribute when trying to explain objects and than height is also giving as an example when trying to show code examples for properties.

Than I start getting confused thinking so are attributes the same as properties? Why am I getting confused here?
User is offlineProfile CardPM

Go to the top of the page


modi123_1
post 4 Oct, 2008 - 03:38 PM
Post #2


D.I.C Addict

Group Icon
Joined: 12 Jun, 2008
Posts: 508



Thanked 10 times

Dream Kudos: 100
My Contributions


QUOTE(legend_018 @ 4 Oct, 2008 - 04:03 PM) *

Hi. I'm trying to learn c# using some online tutorials. At one point, it was teaching me that objects are defined by attributes (data) and behaviors. Example of an Object could be a person with eye, color, and height as attributes.

Than later on, I'm learning about properties. The examples give height and width.

So than i'm thinking, hmmm height is giving as an example as an attribute when trying to explain objects and than height is also giving as an example when trying to show code examples for properties.

Than I start getting confused thinking so are attributes the same as properties? Why am I getting confused here?



Yes and no.

You have a class object. it will have methods and routines in it, right? Well if you have a class variable (like an integer, an array, or another object) you can declare it as private, public, friend, and so on. Those would be attributes. Attributes are the class's variables. Properties would be directed ways of interacting with attributes (class variables).

See when I build a class all my class variables are declared private. I don't want someone else mucking around undirected with my attributes (variables). However I *do* want them to use be able to access them so I build properties that limit how and what a user can do to the variable.
User is offlineProfile CardPM

Go to the top of the page

legend_018
post 4 Oct, 2008 - 04:03 PM
Post #3


New D.I.C Head

*
Joined: 11 Sep, 2008
Posts: 31

QUOTE(modi123_1 @ 4 Oct, 2008 - 04:38 PM) *

QUOTE(legend_018 @ 4 Oct, 2008 - 04:03 PM) *

Hi. I'm trying to learn c# using some online tutorials. At one point, it was teaching me that objects are defined by attributes (data) and behaviors. Example of an Object could be a person with eye, color, and height as attributes.

Than later on, I'm learning about properties. The examples give height and width.

So than i'm thinking, hmmm height is giving as an example as an attribute when trying to explain objects and than height is also giving as an example when trying to show code examples for properties.

Than I start getting confused thinking so are attributes the same as properties? Why am I getting confused here?



Yes and no.

You have a class object. it will have methods and routines in it, right? Well if you have a class variable (like an integer, an array, or another object) you can declare it as private, public, friend, and so on. Those would be attributes. Attributes are the class's variables. Properties would be directed ways of interacting with attributes (class variables).

See when I build a class all my class variables are declared private. I don't want someone else mucking around undirected with my attributes (variables). However I *do* want them to use be able to access them so I build properties that limit how and what a user can do to the variable.



Thanks, I guess it's kind of confusing when I'm learning about objects and height is giving as an attribute for it and than it's giving again when explaining properties.
User is offlineProfile CardPM

Go to the top of the page

baavgai
post 4 Oct, 2008 - 05:58 PM
Post #4


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,945



Thanked 94 times

Dream Kudos: 475

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions


When talking about Object Oriented design, attributes and behaviors are more concepts than language constructs. For such design considerations, only the public elements of an object need generally be considered. Or protected, when you get to inheritance.

Consider the following:
csharp

class Point {
public int X, Y; // attribute
public Point(int x, int y) {
this.X = x;
this.Y = y;
}
public void moveX(int diff) { this.X += diff; } // behavior
}


An object variable can be considered an attribute by it's nature. However, it's considered very bad practice to ever expose variables to the public. What if you want to put a rule on the max value of x? How do you impose that on a variable? Remember, one of the big reasons to use OO techniques is to limit the impact of future changes.

csharp

class Point {
private int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() { return this.x; } // attribute
public int getY() { return this.y; } // attribute
public void moveX(int diff) { this.x += diff; } // behavior
}


Now the only exposed elements of this object are methods. It's a little awkward, but it's good practice. Functionally, X is readonly. To make it read write, we can add a setX method. "Getters and setters" are part of a Java best practive for attributes. But they aren't implicit, they just follow a naming convention. In C# we can use a elegant solution: Properties.

csharp

class Point {
private int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int X { get { return this.x; } set { this.x = value } } // attribute
public int Y { get { return this.y; } } // attribute ( read only )
public void moveX(int diff) { this.x += diff; } // behavior
}


With the properties we can give object attributes in a natural way. The following example works with the first class and the last one:
csharp

Point pt = new Point(2,3);
pt.X = 5;


For a program, a Property actually feels like a variable, serves as an OO attribute, and has the functionality of a method.

Hope that made some sense. tongue.gif
User is offlineProfile CardPM

Go to the top of the page

legend_018
post 4 Oct, 2008 - 06:14 PM
Post #5


New D.I.C Head

*
Joined: 11 Sep, 2008
Posts: 31

QUOTE(baavgai @ 4 Oct, 2008 - 06:58 PM) *

When talking about Object Oriented design, attributes and behaviors are more concepts than language constructs. For such design considerations, only the public elements of an object need generally be considered. Or protected, when you get to inheritance.

Consider the following:
csharp

class Point {
public int X, Y; // attribute
public Point(int x, int y) {
this.X = x;
this.Y = y;
}
public void moveX(int diff) { this.X += diff; } // behavior
}


An object variable can be considered an attribute by it's nature. However, it's considered very bad practice to ever expose variables to the public. What if you want to put a rule on the max value of x? How do you impose that on a variable? Remember, one of the big reasons to use OO techniques is to limit the impact of future changes.

csharp

class Point {
private int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() { return this.x; } // attribute
public int getY() { return this.y; } // attribute
public void moveX(int diff) { this.x += diff; } // behavior
}


Now the only exposed elements of this object are methods. It's a little awkward, but it's good practice. Functionally, X is readonly. To make it read write, we can add a setX method. "Getters and setters" are part of a Java best practive for attributes. But they aren't implicit, they just follow a naming convention. In C# we can use a elegant solution: Properties.

csharp

class Point {
private int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int X { get { return this.x; } set { this.x = value } } // attribute
public int Y { get { return this.y; } } // attribute ( read only )
public void moveX(int diff) { this.x += diff; } // behavior
}


With the properties we can give object attributes in a natural way. The following example works with the first class and the last one:
csharp

Point pt = new Point(2,3);
pt.X = 5;


For a program, a Property actually feels like a variable, serves as an OO attribute, and has the functionality of a method.

Hope that made some sense. tongue.gif


Wow thanks. I'm printing this out and will be reading it soon.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/20/08 04:29AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month