Click here to Skip to main content
15,895,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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,

thanx
Posted

1 solution

.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:
http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29[^],
http://en.wikipedia.org/wiki/Singleton_pattern[^].

The singleton pattern itself can be implemented in a good or a bad way. This is a sample of good .NET implementation:
http://csharpindepth.com/Articles/General/Singleton.aspx[^].

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.

—SA
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Apr-12 13:27pm    
[Adnan Merter commented (removed from "Solution"):]

thanx,

i declared my global objects and variables in a reference class and it works
Sergey Alexandrovich Kryukov 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.
--SA

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900