cyberconian Posted March 9, 2009 Report Posted March 9, 2009 Greetings, In C# I have a problem with the FSUIPCConnection.dll when it comes to reading an offset more than once. Class c { Offset aspd = new Offset(0x02BC); // Airspeed public c( ) { } } C c1 = new c() // works fine to this point C c2 = new c() // I get an exception in C# saying that "An item with the same key has already been added." I am developing a c# application under Visual C# Express. How do I go about preventing this from happening? Is there a limitation that a class that contains an offset can only every be used once? Thanks for your time and patience.
Paul Henty Posted March 9, 2009 Report Posted March 9, 2009 Hi cyberconin There is only one 'pool' of offsets stored in the DLL. If you try and create an offset more than once (in the same group) then you'll get this error. Each time you're creating a new instance of class 'c' you're trying to create the same offsets again. I could modify the DLL to accept these 'duplicates' and return a reference to the offset that's already there. There's an extra complication where I would have to check that all the attributes are the same. For example if one class wanted an int from 0x1234 and another wanted a short from 0x1234 then this would have to still throw an error. When I wrote the DLL I decided against this approach because I could lead to confusion if the programmer didn't realise he/she was getting a reference to the existing offset instance rather than a new instance of that offset. With the current 1.3 version you can share offsets between classes (or instances of the same class) by creating another class that just handles your offsets. This could be a static class or a proper class that you only create one instance of. You would then need to pass a reference to this single instance into all the instances of other classes that need the data (probably in the constructor). So it goes something like this (Not real code or offset values!) Class FSUIPCData { public Offset airspeed = new offset(0x1234); public Offset altitude = new offset(0x5678); } Class c { private FSUIPCData data; public c (FSUIPCData Data) { data = Data; } private void doSomething() { int currentAirspeed = data.airspeed; } } In your main program you'd do something like this: FSUIPCData data = new FSUIPCData(); c c1 = new c(data); c c2 = new c(data); c c3 = new c(data); c c4 = new c(data); You'll need to manage the Process() calls in your main program loop or timer. You don't want all your instances of class c calling Process() for example. With this method it's very clear that each instance of class 'c' is referencing the same instance of the offset. That is, not each instance of class 'c' has it's own instance of the offset. I prefer this approach for it's clarity but I could be persuaded to go the other route where it's handled the in DLL as described at the top. Which method would you prefer? I'm intending to release 1.4 in the next month or so, so I could squeeze this change in. Let me know if you need any more explanation... Paul
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now