Skip to main content
GameDev.net gamedev.net
🔒 Locked

[.net] Static indexers in C#, is there a way?

Started by remigius Feb 4, 2006 at 10:37 AM 9 replies 10.9k views
Original Post
remigius
remigius
Hello, I'm working on a little utility lib to use the XInput library in .NET and I've run into a little problem. The XInput library provides support for gamepad controllers (like the XBox360 controller for Windows), so I thought it would be a nice design to expose these like: XInput.Controller[ index ] But unfortunately C# (or rather .NET?) doesn't seem to support static indexers. Is there any way I can trick C# into allowing the above syntax? Overloading [] perhaps, as it it supposedly done in C++? I know I could simply use a static method to access the controllers, but the indexers seem to be the right way to do this and I think it just looks nicer :) Thanks in advance for any help!
CodeImp
CodeImp
Make a collection class and call it something like XInputControllers. This will hold the collection of your controllers and will have an indexer. Then make a static property Controllers which returns a XInputControllers object.

// This will hold you collectionpublic class XInputControllers{   // stuff here...      // Indexer   public Controller this[int index]   {      get       {         // Return an item (controller) here      }   }}// In your XInput class...private static XInputControllers controllers;// Property that returns the controllers collectionpublic static XInputControllers Controllers{   get   {      return controllers;   }}// You may now access it like thisXInput.Controllers[index]...


A good example can be found in the .NET architecture itsself. Look at the Items property of the ListView control, for example :)
Kind regards,
Pascal van der Heiden

CodeImp - My trademark and website
abnormal
abnormal
no you can't since c# doesn't support that. but why not write a normal indexer and access it with a singleton or any other property.

in your example: XInput.Controller.Default[index]
(Default being your public static property to get the default controller instance)
maybe not as convenient, but it works.
Microsoft DirectX MVP. My Blog: abi.exdream.com
remigius
remigius
Quote:
Original post by CodeImp
Make a collection class and call it something like XInputControllers. This will hold the collection of your controllers and will have an indexer. Then make a static property Controllers which returns a XInputControllers object.


I just figured out something similar, but thanks for the quick response. When I look at this solution, I can see it's a much better way to implement this kind of indexers than allowing static ones, keeping the 'collection code' in a sperate class. One last question though, I always forget about the CLR/.NET code guidelines. Would the correct name of this static property be Controller or Controllers?

Anyway, I haven't yet tried the XInput wrapper that comes with the Managed DirectX 2.0 beta release, so I don't know if I'm making anything useful here. Still, it should be a nice lib to have when you're not working with DirectX, as MDX is now completely packaged in one DLL. I'll publish it on mdxinfo.com when it's done, so stay tuned [smile]

CodeImp
CodeImp
Quote:
Original post by remigius
I always forget about the CLR/.NET code guidelines. Would the correct name of this static property be Controller or Controllers?

I'd say Controllers since the property returns the entire collection and not just one, but I'm not sure what the guidelines say about this either.
Kind regards,
Pascal van der Heiden

CodeImp - My trademark and website
Rob Loach
Rob Loach
Quote:
Original post by remigius
One last question though, I always forget about the CLR/.NET code guidelines. Would the correct name of this static property be Controller or Controllers?
The class should be named "ControllerCollection" and the property name should be whatever you think makes the most sense to you (Controllers):

// In your XInput class:private static ControllerCollection m_Controllers = new ControlCollection();public static ControllerCollection Controllers{    get    {        return m_Controllers;    }    // Collections shouldn't have a setter.}


This will give you the ability to have the syntax XInput.Controllers[ index ].DoSomething().
Rob Loach [Website] [Projects] [
Arild Fines
Arild Fines
Quote:
Original post by CodeImp
I always forget about the CLR/.NET code guidelines. Would the correct name of this static property be Controller or Controllers?


I'd say Controllers given that MS uses that. IE, you have Form.Controls, ListBox.Items etc.

--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Rob Loach
Rob Loach
Quote:
Original post by remigius
And Controllers it is, thanks for the help [smile] In case anyone's interested, here's the current beta of the wrapper with a little demo app.
It seems xinput.php isn't up... I'm very interested in taking a look. [wink]
Rob Loach [Website] [Projects] [
Rob Loach
Rob Loach
Quote:
Original post by remigius
Hmmm, strange... I uploaded it yesterday and it seems to work for me. Here's the direct download link, maybe that works better for you. What error are you getting anyway?
It works today, I'll try out the XInput wrapper once I get the time. Great work on the site, by the way. Keep it up!
Rob Loach [Website] [Projects] [

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.