473,385 Members | 1,615 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

how to instantiate a global class visible in whole application

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
Jun 6 '07 #1
3 3468
On Jun 6, 7:46 am, Peted wrote:
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

Jun 6 '07 #2
>
>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
Jun 6 '07 #3
On Tue, 05 Jun 2007 23:26:01 -0700, <Petedwrote:
[...]
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[i];
}

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
Jun 6 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have...
7
by: Michael | last post by:
Hi newsgroup, as the subject indicates I am looking for an advice using global variables. I am not if this problem is more about style then C. If its wrong in thi group, sorry. So I have a...
12
by: Sunny | last post by:
Hi All, I have a serious issue regarding classes scope and visibility. In my application, i have a class name "TextFile", and also a few other classes like "TotalWords", "TotalLines" and etc..,...
1
by: noname | last post by:
i have the following in global.asax: <%@ Application Codebehind="Global.asax.cs" Inherits="Foo.Global" Classname="AppClass" %> but wherever i use AppClass.foo() i get the compiler error: The...
4
by: BB | last post by:
Hello all, I might be missing something here, but am trying to understand the difference between using application-level variables--i.e. Application("MyVar")--and global variables--i.e. public...
4
by: Simon Harris | last post by:
Hi All, I'm new to ASP.Net, so be gentle! (Plenty of 'classic' ASP experience), just one question... - Am I correct in thinking that global functions are stored in ASCX files? Thanks! ...
22
by: fd123456 | last post by:
Hi Tom ! Sorry about the messy quoting, Google is playing tricks on me at the moment. > Global.asax is where you normally have the Global Application > and Session variables and code to...
7
by: J-T | last post by:
I can instantiate my object in my *ASP.NET* application in two ways: A) public sealed class RSSingleton { private static ReportingServiceProxy m_RsProxy=null; static RSSingleton() {...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.