Click here to Skip to main content
13,193,468 members (46,372 online)
Rate this:
Please Sign up or sign in to vote.
hi all,

i need to find out how i can declare a global object which can be accesible from all functions of the same application.

i created a global serialport object :

System::IO::Ports::SerialPort^ serialPort = gcnew SerialPort(L"COM4" , 300, Parity::Even , 7, StopBits::One);

then, when try to run the application i see this:

error C3145: 'serialPort' : global or static variable may not have managed type 'System::IO::Ports::SerialPort ^'

waiting your solutions,

Posted 1-Apr-12 6:45am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

.NET does not have a concept of global objects (finally, thanks goodness!). Instead, you can have a static member of some managed "ref" class or structure.

Formally, this solves your problem. But even this is not good enough, would be the sign of bad coding style.

There are two ways of putting it right. First one is the most desirable and most usually applicable: make your serialPort a local variable or an instance (non-static) member of the class declaring the method running the code you show. Usually, this is quite enough. Think about using of the reference to this object. It is very unlikely it is really needed to be used by many other parts of your application. Most likely, the situation is directly the opposite: you need to use this object in some limited scope, the smaller, the better.

In rare cases when you really need to use something globally, the right design pattern is the singleton pattern. Please see:[^],[^].

The singleton pattern itself can be implemented in a good or a bad way. This is a sample of good .NET implementation:[^].

Sorry, this is C#, but I don't think it could be a problem for you. This is still .NET, and you are using C++/CLI which is in few ways more advanced then C#.

But let me repeat: I believe in your case you only need a very local use of the object in question.

SAKryukov 1-Apr-12 13:27pm
[Adnan Merter commented (removed from "Solution"):]


i declared my global objects and variables in a reference class and it works
SAKryukov 1-Apr-12 13:31pm
No wonder it could work, but I did not advice you doing that. You rather had to use this object locally to the code working with the port. Also, keep in mind that you usually should run such code in a different thread.

Please don't post comments like that using "Add your solution here" like you did -- this is not a solution. Instead, use "Have a Question or Comment?" to add a comment, "Reply" to reply to existing comment, or "Improve question" if needed. You "solution", will be removed, no one gets e-mail notification, many would down-vote such fake solutions.

Good luck, call again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.171018.2 | Last Updated 1 Apr 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100