Click here to Skip to main content
Rate this: bad
good
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,
 
thanx
Posted 1-Apr-12 7:45am

1 solution

Rate this: bad
good
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:
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
  Permalink  
Comments
SAKryukov at 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
SAKryukov at 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)

  Print Answers RSS
0 OriginalGriff 390
1 Sergey Alexandrovich Kryukov 329
2 Afzaal Ahmad Zeeshan 244
3 BillWoodruff 210
4 Maciej Los 192
0 OriginalGriff 5,560
1 DamithSL 4,476
2 Maciej Los 3,942
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,175


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 1 Apr 2012
Copyright © CodeProject, 1999-2014
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