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 / .NET Framework / New Users / January 2005

Tip: Looking for answers? Try searching our database.

Overload Indexer

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dieter Pelz - 05 Jan 2005 10:57 GMT
Hallo,

could anyone explain why the double indexer ist called and not the
overloaded int indexer.
The Test Object is called with an int Value!

Output:
CALL Test this[double dbl] ??????????????
CALL Test this[int idx]

using System;
public class Test : TestBase
{
   public override object this [int idx]
   {
       get {
         Console.WriteLine("CALL Test this[int idx]");
         return "Test[int]";
       }
   }
   public object this[double dbl]
   {
       get {
         Console.WriteLine("CALL Test this[double dbl]");
         return "Test[double]";
       }
   }
   public static void Main()
   {
     object obj;
     Test tst = new Test();

     int i = 2;
     obj = tst[i];
     obj = ((TestBase)tst)[i];

     Console.ReadLine();

     // Output
     // CALL Test this[double dbl] ??????????????
     // CALL Test this[int idx]
   }

};

public class TestBase
{
   public virtual object this[int idx]
   {
       get {
         Console.WriteLine("CALL TestBase this[int idx]");
         return "TestBase[int]";
       }
   }
};

Thanks
Dieter
Steven Cheng[MSFT] - 06 Jan 2005 08:21 GMT
Hi Dieter,

Thanks for your posting. As for the Indexer problem you mentioned, I think
it is the normal behavior of the C# compiler on dealing with indexers. In
fact, when the compiler encounter a certain indexer expression, for example:

class a;

a[index] =

then, the compiler will first create a set of all of the class a's
avaliable indexers and them filter some and finally choose a most
appropriate one. And one rule of the filtering is the indexer which is
defined with  
"override" keyword will be bypassed, that's why your override indexer(int
parameter) will not work.

For detailed explanition, you can have a look at the following
specification:

7.5.6.2 Indexer access
http://msdn.microsoft.com/library/en-us/csspec/html/vclrfcsharpspec_7_5_6_2.
asp?frame=true

Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Signature

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


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.