Confused with properties declaration

  • Thread starter Thread starter Bogdan
  • Start date Start date
B

Bogdan

Hi,

I mainly develop in C++ and Java but I think I have a basic understanding of
C# which I use occasionally only. I came across the following declaration
of properties in on line docs:

DbParameterCollection.Item(Int32)
DbParameterCollection.Item(String)

Since this is not a 'typical' declaration of properties (e.g. not
<accessmod><return><name> + accessors) I don't really know how to interpret
it. How do I access items? Assuming that the collection is 'params',
'name' is a valid string, and 'i' is a valid index,can I simply do the
following?
params.Item[index] = param; param = params.Item[index]; params.Item[name] =
param;

Could someone please provide me with a quick explanation? Any pointers to
on-line docs will also be very helpful.

Thanks,
Bogdan
 
Thanks for the reply. I was initially thinking of indexers but I had doubts
because of the fact that there were no square brackets in the declaration
and docs clearly stated that they were properties. The link that I was
referring to is at:
http://msdn2.microsoft.com/en-us/library/system.data.common.dbparametercollection.item.aspx

So, how shoud I interpret the following syntax: Item(Int32)? Is this an
indexer that can be accessed by an integer index? If yes, do C# specs have
this syntax defined somewhere?

Thanks,
Bogdan

Nicholas Paldino said:
Bogdan,

It would be helpful if you show the link where you saw the declaration,
as it would help provide context.

What you want to look at is "indexers":

http://msdn2.microsoft.com/en-us/library/6x16t2tx.aspx

As there is no need to call the Item property directly in C#.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Bogdan said:
Hi,

I mainly develop in C++ and Java but I think I have a basic understanding
of C# which I use occasionally only. I came across the following
declaration of properties in on line docs:

DbParameterCollection.Item(Int32)
DbParameterCollection.Item(String)

Since this is not a 'typical' declaration of properties (e.g. not
<accessmod><return><name> + accessors) I don't really know how to
interpret it. How do I access items? Assuming that the collection is
'params', 'name' is a valid string, and 'i' is a valid index,can I simply
do the following?
params.Item[index] = param; param = params.Item[index]; params.Item[name]
= param;

Could someone please provide me with a quick explanation? Any pointers
to on-line docs will also be very helpful.

Thanks,
Bogdan
 
Thanks for the reply.  I was initially thinking of indexers but I had doubts
because of the fact that there were no square brackets in the declaration
and docs clearly stated that they were properties.  The link that I was
referring to is at:http://msdn2.microsoft.com/en-us/library/system.data.common.dbparamet...

So, how shoud I interpret the following syntax: Item(Int32)?  Is this an
indexer that can be accessed by an integer index?  If yes, do C# specs have
this syntax defined somewhere?

Thanks,
Bogdan

message

   It would be helpful if you show the link where you saw the declaration,
as it would help provide context.
   What you want to look at is "indexers":

   As there is no need to call the Item property directly in C#.
Bogdan said:
Hi,
I mainly develop in C++ and Java but I think I have a basic understanding
of C# which I use occasionally only.  I came across the following
declaration of properties in on line docs:
DbParameterCollection.Item(Int32)
DbParameterCollection.Item(String)
Since this is not a 'typical' declaration of properties (e.g. not
<accessmod><return><name> + accessors) I don't really know how to
interpret it.  How do I access items?  Assuming that the collectionis
'params', 'name' is a valid string, and 'i' is a valid index,can I simply
do the following?
params.Item[index] = param; param = params.Item[index]; params.Item[name]
= param;
Could someone please provide me with a quick explanation?  Any pointers
to on-line docs will also be very helpful.
Thanks,
Bogdan- Hide quoted text -

- Show quoted text -

Make sure you are looking at the C# example and not the VB example.
Under the hood, C# convert all properties to methods:

<value_type> get_PropertyName
void set_PropertyName(<value_type> value)

When working with Indexers, those methods look like this:

<value_type> get_Item(<list of indices>);
void set_Item(<list of indices>, <value_type> value);

Some times when looking at a property either through a COM Interop or
through VB you will just see Item. Here you just pass the indices as
arguments. It is just like saying:

myClass[<list of indices>]

In C++, overloading the [] operator is no different, except .NET
converts it to a method call under the hood.

Am I answering your question?
 
Bogdan,

Well, an indexer is usually just shorthand for an indexed property. You
could call it directly if you wanted, but I don't see the point.

The syntax means that if you were going to access it directly by its
property, then the property you would access is the Item property. The
Int32 means that you can index it on an integer (although other collections
might be indexed on other things, in which case, that type will be where
Int32 is).

You could look in the C# programmers guide as well as the C# language
specification if you are looking for the specific definition of an indexer.
A google search will turn up both easily.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Bogdan said:
Thanks for the reply. I was initially thinking of indexers but I had
doubts because of the fact that there were no square brackets in the
declaration and docs clearly stated that they were properties. The link
that I was referring to is at:
http://msdn2.microsoft.com/en-us/library/system.data.common.dbparametercollection.item.aspx

So, how shoud I interpret the following syntax: Item(Int32)? Is this an
indexer that can be accessed by an integer index? If yes, do C# specs
have this syntax defined somewhere?

Thanks,
Bogdan

Nicholas Paldino said:
Bogdan,

It would be helpful if you show the link where you saw the
declaration, as it would help provide context.

What you want to look at is "indexers":

http://msdn2.microsoft.com/en-us/library/6x16t2tx.aspx

As there is no need to call the Item property directly in C#.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Bogdan said:
Hi,

I mainly develop in C++ and Java but I think I have a basic
understanding of C# which I use occasionally only. I came across the
following declaration of properties in on line docs:

DbParameterCollection.Item(Int32)
DbParameterCollection.Item(String)

Since this is not a 'typical' declaration of properties (e.g. not
<accessmod><return><name> + accessors) I don't really know how to
interpret it. How do I access items? Assuming that the collection is
'params', 'name' is a valid string, and 'i' is a valid index,can I
simply do the following?
params.Item[index] = param; param = params.Item[index];
params.Item[name] = param;

Could someone please provide me with a quick explanation? Any pointers
to on-line docs will also be very helpful.

Thanks,
Bogdan
 
[...]
Make sure you are looking at the C# example and not the VB example.
Under the hood, C# convert all properties to methods:

<value_type> get_PropertyName
void set_PropertyName(<value_type> value)

When working with Indexers, those methods look like this:

<value_type> get_Item(<list of indices>);
void set_Item(<list of indices>, <value_type> value);

Some times when looking at a property either through a COM Interop or
through VB you will just see Item. Here you just pass the indices as
arguments. It is just like saying:

myClass[<list of indices>]

In C++, overloading the [] operator is no different, except .NET
converts it to a method call under the hood.

Am I answering your question?

Yes. Thanks. The COM Interop was a good clue.
 
Thanks again for the reply and your patience.

I get it know. I was confusing C#'s indexers and properties with .NET
properties. I wrongly assummed that having language filter set to C# will
cause all declarations to appear in C#. So when I saw
"DbParameterCollection.Item Property" I thought that it referred to C#
property of a class.


Nicholas Paldino said:
Bogdan,

Well, an indexer is usually just shorthand for an indexed property.
You could call it directly if you wanted, but I don't see the point.

The syntax means that if you were going to access it directly by its
property, then the property you would access is the Item property. The
Int32 means that you can index it on an integer (although other
collections might be indexed on other things, in which case, that type
will be where Int32 is).

You could look in the C# programmers guide as well as the C# language
specification if you are looking for the specific definition of an
indexer. A google search will turn up both easily.
[...]
 

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

Back
Top