Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

indexing an object with a string?

Status
Not open for further replies.

prolog2345

Programmer
Jan 2, 2006
7
YU


Can someone help me with the following syntax in c#:

say i have a class MyClass

in many books in C# i have seen the following syntax: MyClass["Name"]

what does this mean?

i have read about indexers but they take an argument of type 'int' not 'String'. So what does this syntax retrieve from an abject.



 
There's no reason why your indexer parameter can't be a string. This is valid code:
Code:
        public DateTime this[string index]
        {
            get
            {
                return null;  // or something meaningful
            }
            set
            {
                // Set the value here
            }
        }
If you create a new class, then go into the class viewer, then expand the project to get to your class in the tree, you can right-click and on the context menu is Add... Indexer. In the indexer creation wizard you can specify your datatypes.

Chip H.



____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top