Click here to Skip to main content
15,748,749 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Im using SlimDx to pick up joystick feedback in my c# app. The code I implemented works fine for one stick but I cant seem to get two identical sticks to work. Apologies for my ignorance ahead of time. Here is the code:

public SimpleJoystick()

DirectInput dinput = new DirectInput();

foreach (DeviceInstance device in dinput.GetDevices(DeviceClass.GameController,                                                  DeviceEnumerationFlags.AttachedOnly))

                // Create device
                     stickOne = new Joystick(dinput, device.InstanceGuid);


                catch (DirectInputException)
                   //catch stuff here

Ive made a Joystick[] named sticks prior to this. How do I place each device into the Joystick array within the foreach loop? I cant figure out what variable can be used in the sticks[x] so that it corresponds to the correct InstanceGuid of the stick. Any help would be most appreciated.

1 solution

Unless you have to use an Array for some reason, you would be better using one of the generic collections.

A List<Joystick>, for example. You would then be able to replace:
stickOne = new Joystick(dinput, device.InstanceGuid);

with (assume you called the list sticks as in your array)
sticks.Add(new Joystick(dinput, device.InstanceGuid));

Or even a dictionary
Dictionary<GUID, Joystick> sticks = new Dictionary<GUID, Joystick>();

// then in the try block
sticks.Add(device.InstanceGuid, new Joystick(dinput, device.InstanceGuid));

You would then be able to look them up by their GUID, if that is required.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900