.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