|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming | |
Help with initializing classes C#
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | |||||||||||||
|
New to Overclock.net
|
Forgive me I have been coding in C# for 3 or 4 weeks now.
I am working on remoting a Tektronix oscilloscope. They have a dll that has functions for the scope. I am then writing a class to talk with that dll. That part is easy but since there are 1500 commands I am creating seperate classes to make it organized and object orientied. The problem is that each class needs to be able to talk to that dll but if I open the class it initializes the variables and I can no longer talk to the scope. I tried creating another class whose sole purpose was to talk to that dll and the others classes talked to it but when if I initialize that class more than once I lose my variables and can't talk. What sort of things can I do to keep the class from reinitizing or make it so it keeps the variables? Any help would be greatly appreciated. To summerize how I initialize the dll only once and have it available to all the other classes.
__________________
Buy Games Member of the OCN Diablo 3 Club Go Towson University http://www.speedtest.net/result/318264415.png
Last edited by vgplayer : 07-15-08 at 02:59 PM. |
|||||||||||||
|
|
|
|
#2 (permalink) | ||||||||||||
|
News Fiend
|
I DREW u a picture
|
||||||||||||
|
|
|
|
|
#3 (permalink) | |||||||||||||
|
New to Overclock.net
|
Thanks, yeah that is the idea but I don't know how to implement it.
__________________
Buy Games Member of the OCN Diablo 3 Club Go Towson University http://www.speedtest.net/result/318264415.png
|
|||||||||||||
|
|
|
|
#4 (permalink) | |||||||||||||
|
New to Overclock.net
|
Ok I added another picture. The problem is I have to use funtions from the parent class. In the Data Class in order to set or read the encoding type I need to send read and write command to the scope through my Tek Class which then talks to the dll.
__________________
Buy Games Member of the OCN Diablo 3 Club Go Towson University http://www.speedtest.net/result/318264415.png
|
|||||||||||||
|
|
|
|
#5 (permalink) | |||||||||||||
|
Mmmm! Toast!
|
sounds like you've got a win32 library dll to link to?
check out the syntax for DllImport and pinvoke.net has tons of info on the windows api which serves a good example. After wrapping the dll functions, it's all depends on how the vendor intends it to be used. In some cases you can just call each function w/o any prior setup. Sometimes (in an image processing library I use), it requires initializing and saving lots of handles all over the place.
|
|||||||||||||
|
|
|
|
#6 (permalink) | |||||||||||||
|
New to Overclock.net
|
I got it working. I had to pass the parent class into the child class constructor in order to make its functions available.
__________________
Buy Games Member of the OCN Diablo 3 Club Go Towson University http://www.speedtest.net/result/318264415.png
|
|||||||||||||
|
|
|
|
#7 (permalink) | ||||||||||||
|
New to Overclock.net
|
If you only want one instance of a class to be made, I would look at the Singleton design pattern. It's disgusting but it does about that. Here's one way to do it: Basically, you just have a static function and a private static variable, in the class, and a private constructor. When you call this function for the first time (i.e. the static variable is null), it creates an instance of the class and saves it to the static variable. On future calls, rather than constructing it again it just returns the static variable.
__________________This allows you to simply write something like Foo f1 = Foo.GetInstance(); Foo f2 = Foo.GetInstance(); // f1 == f2 so you can call this function from any other class in your application and be guaranteed to get the same instance (as long as 2 threads don't try to call the initial method at the same time). However, there must be a more elegant way of doing it than this.
|
||||||||||||
|
|
|
|
|
#8 (permalink) | |||||||||||||
|
Mmmm! Toast!
|
that'll work. elegance is overrated.
|
|||||||||||||
|
|
|
|
#9 (permalink) | ||||||||||||
|
New to Overclock.net
|
Is it possible to have more than one device connected at once? If so, you may want to avoid the singleton design pattern and use one instance of the object for each device.
__________________Would hooking 2 up at the same time not work at all, as one would overwrite the other's variables? I only really have experience with human interface devices (HID).
|
||||||||||||
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|