.ASP Properties vs Attributes

  • Thread starter Thread starter Patrick *
  • Start date Start date
P

Patrick *

Reading a book on .ASP .NET I am getting a bit mixed up as to the difference
between "property" and "attribute" as the terms don't seem to be used
consistently in the book I am reading. Can anyone give two good definitions?
Both with respect to controls.
 
Patrick said:
Reading a book on .ASP .NET I am getting a bit mixed up as to the difference
between "property" and "attribute" as the terms don't seem to be used
consistently in the book I am reading. Can anyone give two good definitions?
Both with respect to controls.

Not having the book text available it's hard to tell wether they are
mixing up the words or if you're mixing up attributes and properties.

The .NET definition of the two are:

A property is something part of a class, like Name. You access it like this:

obj.Name = "Hello";

or similar. This executes some code in order to store the new value in
the object.

For components/controls you drop on form, these are the things you get
in the Properties window when you select it.

An attribute is something different and is only relevant in source code.

for a class, defined like this:

public class YourClass {...}

an attribute could be applied to it like this:

[AttributeName(parameters to attribute here)]
public class YourClass {...}

Some attributes are used to tell the compiler to do something specific
with whatever the attribute is applied to, other attributes are simply
stored in the assembly for future reference. For instance, you can
iterate through all types in an assembly and check if each has a
specific attribute applied to it.

If your book talks about properties and calling them attributes, well,
then I don't know what they're doing.

A html tag, like <a href="xyz"> has attributes in the sense that "href"
is an attribute of the "A" tag. ASP.NET controls store some of the
property values in attributes like this. This could be what they're
talking about.
 
Patrick,

I don't know your book, however an attribute is used as they belong to HTML
controls (which the servercontrols on the client side are as well). Height,
Width on the client side is an attribute.

While when you are using them in your code in C# directly it are properties.

Maybe I can show it you with this piece of code.
\\\
private void Page_Load(object sender, System.EventArgs e) {
if (!IsPostBack)
{
this.Button1.Text = "Send Mail";
this.Button1.Attributes["onClick"] =
"window.location='mailto:[email protected]?subject=Cor demo&body=I hope this
helps?';";
}}
///
In this.Button1 is Attributes a property that sets an Attribute in the HTML
form (clientside)

I hope this helps?

Cor
 
Zach,

What is a definition when some people calls something an attribute and the
others a property.

Asked is how it is used in ASPNET and there they are mixed up when it is on
clientside or on serverside.
http://msdn.microsoft.com/library/d...iwebcontrolswebcontrolclasswidthtopic.aspWhen you want a definition than you can look in a dictionary however incomputer bussiness definitions are not always used right.http://dictionary.reference.com/sea...ctionary.reference.com/search?q=attributeJust my thoughtsCor"Zach" <[email protected]>> Hey guys, these explanations, however valuable are no definitions.>>
 
I don't know why it became all together

Zach,

What is a definition when some people calls something an attribute and the
others a property.

Asked is how it is used in ASPNET and there they are mixed up when it is on
clientside or on serverside.
http://msdn.microsoft.com/library/d...iwebcontrolswebcontrolclasswidthtopic.aspWhen you want a definition than you can look in a dictionary howeverincomputer bussiness definitions are not always used right.http://dictionary.reference.com/sea...ctionary.reference.com/search?q=attributeJust my thoughtsCor
 
It was too much work to make this message not trying it to get it sent
correct.

What is a definition when some people calls something an attribute and the
others a property.

Asked is how it is used in ASPNET and there they are mixed up when it is on
clientside or on serverside.

http://msdn.microsoft.com/library/d...author/dhtml/reference/properties/width_2.asp

---

http://msdn.microsoft.com/library/d...webuiwebcontrolswebcontrolclasswidthtopic.asp

When you want a definition than you can look in a dictionary
howeverincomputer bussiness definitions are not always used right.

http://dictionary.reference.com/search?q=property


----

http://dictionary.reference.com/search?q=attribute

Just my thoughts

Cor
 
The terms attribute and property are related in that, in the design phase of
a project, you define attributes of objects - for instance, an attribute of a
car is its color. When you create a car class, you then create a color
property to describe that attribute. It is this relationship that it sounds
like, to me, that you may be getting confused about from your book.

I say this, because there's another attribute definition which is different
enough from a property that it shouldn't be as confusing - the
System.Attribute class and objects created from that class. You can read the
values of attributes in code and respond appropriately.

In Visual Studio .Net, there is an attribute class. Attributes are placed
into your source code to describe code or control compiler behavior. You can
read the values of the attributes and act accordingly in your code.
 
The terms attribute and property are related in that, in the design phase of
a project, you define attributes of objects - for instance, an attribute of a
car is its color. When you create a car class, you then create a color
property to describe that attribute. It is this relationship that it sounds
like, to me, that you may be getting confused about from your book.

I say this, because there's another attribute definition which is different
enough from a property that it shouldn't be as confusing - the
System.Attribute class and objects created from that class. You can read the
values of attributes in code and respond appropriately.

In Visual Studio .Net, there is an attribute class. Attributes are placed
into your source code to describe code or control compiler behavior.
 
I think Lasse's post is probably closest to the answer you are looking for,
since you are speaking about ASP.Net.

Attributes belong to Html Elements, such as the table tag. They are
contained inside the opening tag and specifiy behaviors and look and feel
information, like Cellspacing.

Properties belong to controls, components, and classes.

Both serve the same purpose in the grand scheme of things. It's a matter of
convention for the type of language or technology you are dealing with.
 
Thomas,

Did you ever set the EnableViewStateen = true of a webpage control and did
than look at the source code of that HTML page.

Than you will see that what is in C# a property is presented in the Client
side as an attribute to use in JavaScript again as a property.

Cor
 
Patrick said:
Reading a book on .ASP .NET I am getting a bit mixed up as to the difference
between "property" and "attribute" as the terms don't seem to be used
consistently in the book I am reading. Can anyone give two good definitions?
Both with respect to controls.
In the .Net environment, a property is any member value of a class that
has accessors (get/set) as follows:

class SomeClass
{
private int classInt;
public int ClassInt
{
get { return classInt; }
set { classInt = value; }
}
}

WHen you do reflection on this class for the properties, you only get
information on items such as this. An attribute is a directly
accessible member variables. If classInt where public, it would also be
an attribute.
 
Hi,


C# Code Perspective

Property = A functional piece of code where an action can take place.
But where you can get confused is LOGICALLY a property is an Attribute
of the object which holds it but physically it is a functional piece of
code.

Attribute = A piece of code which describes code to the compiler. An
instance of System.Attribute is Run-Time meta-data which allows the
compiler to do certain things when you append an attribute to your code.

Attributes are most commonly used with Reflection Techniques.

Hope this helps.


Happy Coding,

Stefan
C# GURU
www.DotNETovation.com

"You always have to look beyond the horizon and can never be complacent
-- God forbid we become complacent."

Jozef Straus
 
Back
Top