how to instantiate a global class visible in whole application

P

Peted

I have an application with mdi parent form, and mutliple child forms.

In the mdi parent form i want to instantiate a device class for
comunicating to a peice of hardware via serial port.

its constructor would look something like

Device kvm = new Device(Serialport);


There may be more than 1 device instatiated, if the pc has more than 1
serial port.


I want to instantiate instance of device class's and have that
instance visible and accessable to all the childforms in this
application.

How is the best way to do this ?


thanks for any help

Peted
 
M

Moty Michaely

I have an application with mdi parent form, and mutliple child forms.

In the mdi parent form i want to instantiate a device class for
comunicating to a peice of hardware via serial port.

its constructor would look something like

Device kvm = new Device(Serialport);

There may be more than 1 device instatiated, if the pc has more than 1
serial port.

I want to instantiate instance of device class's and have that
instance visible and accessable to all the childforms in this
application.

How is the best way to do this ?

thanks for any help

Peted

Dear Peted,

How would you distinguish between the instances?
Can you please clarify the way you are going to use the device class?

Basically, c# inherently restricts the use of any global variables
kind (as an Object Oriented language).

One object oriented way to solve your problem is to create a singleton
(or static) Devices class that will contain the installed devices. The
devices list can be generated on-demand (via a method such as
GetDevice(serialPort)). For example to get the device on com port:

Device d = Devices.Instance.GetDevice("com");

Feel free to ask any further questions.
Hope this helps.
Cheers,
Moty
 
P

Peted

How would you distinguish between the instances?
Can you please clarify the way you are going to use the device class?

Well the devicetype is represented by the class Device


if i have more than 1 serial port on the pc i can connect more than 1
device. i would instantiate it more than once to different variables

eg

Device kvm1 = new Device();
Device kvm2 = new Device();
Device kvm 3 = new Device();

once the class is instantiated i can pass the different Serialport to
each

eg

Serialport sp1 = new Serialport("com1",9600);
kvm1.sp = sp1;

etc etc

if this is all done in the MDI parent form, how can i get all the
childforms throughout the app to access the kvm1, kvm2, kvm3 instances


peted
 
P

Peter Duniho

[...]
if this is all done in the MDI parent form, how can i get all the
childforms throughout the app to access the kvm1, kvm2, kvm3 instances

It just depends on where you want to put them. To some extent it seems to
me it depends on what part of your code is responsible for ensuring that
the Device instances exist. If the main parent form does so, then I would
think the main parent form class would be a natural place to keep them.
The MDI children would then just use their parent to access the Device
instances.

I think Moty's suggestion is a good one too. For example, make a public
static class within the application's namespace that handles maintenance
of the Device instances. The class itself would be accessible by any
other class in the assembly (or any that references it for that matter,
assuming you do make it public...make it internal if you don't want
that). So each MDI child class instance would just reference the static
class to get the Device instances.

As an example of the latter:

public static DevicesManager
{
public class Device
{
// this class doesn't have to be contained by the
DevicesManager,
// but maybe it makes sense to do so, in order to keep
everything
// together
}

private List<Device> _rgdevice = new List<Device>();

public Device DeviceFromIndex(int i)
{
return _rgdevice;
}

public void AddDevice(Serialport sp)
{
Device kvm = new Device();

kvm.sp = sp;
_rgdevice.Add(kvm);
}
}

Then in code in the MDI child class, you'd get a device like this:

Device kvm = DevicesManager.DeviceFromIndex(1);

The above is just a vague bit of hand-waving to try to illustrate why a
static class is useful here. Obviously, you can write your device
initialization and access stuff however you actually think makes sense.
The main thing is just that if you have such a static class, then other
classes can call it directly to get stuff from it or to have it do things.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top