473,568 Members | 2,850 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(Serialpo rt);
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 3477
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(Serialpo rt);

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(seria lPort)). For example to get the device on com port:

Device d = Devices.Instanc e.GetDevice("co m");

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("com 1",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_rgd evice = new List<Device>();

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

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

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

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
3008
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 been for years. If the machine has memory to spare and windows can use it - I'm thinking "Why not?" I was wondering what some of you have to say...
7
3115
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 couple of function that all need the same information (all located in the same file). By now it looks like /* file beginns */
12
2004
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.., which are suppose to describe the structure of my main TextFile class. Also i have created some custom collection classes, which only take items of...
1
3745
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 type or namespace name 'AppClass' could not be found (are you missing a using directive or an assembly reference?) am i missing something? thanks.
4
3526
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 myVar as string, etc. It seems to me that the scope and duration are the same, as they both are there while the application is running, and both go away...
4
1763
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! Simon.
22
3750
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 manipulate them. It starts > and ends with <script></script> tags. > > Yours looks like a compiled version of it.
7
1986
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() { m_RsProxy = new ReportingServiceProxy();
1
29323
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 called it polluting the global namespace. This article explores what happens when the global namespace becomes polluted and how to avoid this...
0
7604
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8117
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7660
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6275
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5498
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.