?
Solved

vc.net 2003, managed global containers

Posted on 2006-03-29
6
Medium Priority
?
489 Views
Last Modified: 2013-12-03
I need to create some kind of container in vc.net 2003, it shall store either Strings or byte arrays. It must be in a separate header and be accessible throughout the whole project.

it must use managed code.

I would prefer it to use const variables.

Any ideas?

This is what it looks like right now:

#pragma once

#using <mscorlib.dll>
#using <System.dll>

namespace Dgcp {

      __gc struct BasicCommands_ {
            static const System::String *cmdAcknowledgement = "ACK";
            static const System::String *cmdHandshake = "HSK";
      } *BasicCommands;
}

But it produces this error:
error C3145: 'BasicCommands' : cannot declare a global or static managed type object or a __gc pointer
0
Comment
Question by:bjqrn
  • 3
  • 2
6 Comments
 
LVL 22

Expert Comment

by:mahesh1402
ID: 16319786
You could make a class that initialises and holds  struct BasicCommands_

Remember you can only define .NET Framework objects within function scope.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/vctbsCompilerErrorC3145.asp <===

You may try using gcroot.



MAHESH
0
 
LVL 3

Author Comment

by:bjqrn
ID: 16319807
The thing is that I would like to use the variables as I would use #define's, but I would like a more "modern" way of doing that.

Please explain gcroot.
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 16319887
GCHandle and gcroot are well described in the docs see "Tips and Tricks to Bolster Your Managed C++ Code" by Tomas Restrepo in the February 2002 MSDN®Magazine :

http://msdn.microsoft.com/msdnmag/issues/02/02/managedc/ <==


MAHESH
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 48

Accepted Solution

by:
AlexFM earned 2000 total points
ID: 16319890
This is my test in VC++ 8.0 which is successfully compiled:

ref class TestClass
{
public:
    static String^ str = "test";
};


int main(array<System::String ^> ^args)
{
    Console::WriteLine(TestClass::str);
    return 0;
}

What line exactly is not compiled in your project? Maybe you are trying to create global BasicCommands_ instance?
0
 
LVL 3

Author Comment

by:bjqrn
ID: 16319899
Yes, a global BasicCommands_ instance is what I try to create.
0
 
LVL 3

Author Comment

by:bjqrn
ID: 16319914
AlexFM: Worked!

Didn't know you could get a variable from a class without instancing it!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question