Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / February 2008

Tip: Looking for answers? Try searching our database.

Confused with properties declaration

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bogdan - 23 Feb 2008 12:35 GMT
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
Nicholas Paldino [.NET/C# MVP] - 23 Feb 2008 14:55 GMT
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#.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Hi,
>
[quoted text clipped - 18 lines]
> Thanks,
> Bogdan
Bogdan - 23 Feb 2008 20:00 GMT
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.dbparametercollectio
n.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

> Bogdan,
>
[quoted text clipped - 29 lines]
>> Thanks,
>> Bogdan
jehugaleahsa@gmail.com - 23 Feb 2008 20:27 GMT
> 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
[quoted text clipped - 47 lines]
>
> - 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 - 25 Feb 2008 03:43 GMT
[...]

>Make sure you are looking at the C# example and not the VB example.
>Under the hood, C# convert all properties to methods:
[quoted text clipped - 17 lines]
>
>Am I answering your question?

Yes. Thanks. The COM Interop was a good clue.
Nicholas Paldino [.NET/C# MVP] - 23 Feb 2008 20:36 GMT
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.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> 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
[quoted text clipped - 42 lines]
>>> Thanks,
>>> Bogdan
Bogdan - 25 Feb 2008 03:45 GMT
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.

> Bogdan,
>
[quoted text clipped - 10 lines]
> specification if you are looking for the specific definition of an
> indexer. A google search will turn up both easily.

[...]

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.