Sign in to follow this  
kustom

[.net] Global Variables in C++/CLI

Recommended Posts

Hi to all, Im learning to code in C++/CLI for giving support to my native C++ applications to .NET framework and i have a problem: i cant declare a global variable in my .cpp /clr of type .NET like: MyClass^ instance = gcnew MyClass(); i can declare it like local object inside a function but not as global :( May exist a especial form of declarate globals? or maybe it doesnt exist globals? im confused..................... Kustom

Share this post


Link to post
Share on other sites
There are no global variables or functions in C#. But you can use static methods/properties/fields inside a static class instead.


public static class MyGlobalClass
{
public static int MyGlobalField1 = 0;

//or use a static property

private static int myGlobalField2 = 0;

public static int MyGlobalField2
{
get { return this.myGlobalField2; }
set { this.myGlobalField2 = value; }
}
}

// use them like:
void SomeMethod()
{
MyGlobalClass.MyGlobalField1 = 3;
MyGlobalClass.MyGlobalField2 = 5;
}


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this