Sign in to follow this  

Sharing some data between classes, c#

This topic is 3844 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I want to share some data between several classes in c#. for arguements sake lets make it an array (which it actually is). given that there is no such thing as a global variable in c#, how do i share the variable? my fallback of creating a pointer to it also fails, due to c# not being overly fond of them...

Share this post


Link to post
Share on other sites
C# arrays are first class. Just pass it around as necessary.

As for pointers, you can use them in C# -- just you need to mark the block as unsafe (and compile with /unsafe) before you can do so.

class A
{
int[] someArray = new int[50];
B someB;

public A()
{
for (int i = 0; i < 50; ++i)
someArray[i] = i;

someB = new B(someArray);
}
}

class B
{
int[] needyArray;

public A(int[] MyArray)
{
needyArray = MyArray;
}
}

Share this post


Link to post
Share on other sites
And although most people here will tell you that globals are Really Bad, there's nothing keeping you from storing the array as a static member of a class. I'd be accessible from anywhere, so it amounts to the same thing.

But yeah, passing the data between classes is better.

Share this post


Link to post
Share on other sites
Not sure if that helps , but here's what im doing in my little game :
(note : its in c++)

AA_CLASS.H

class _A
{
private:
std::string Afile;//just an example..add your private stuff here!
public:
void setFile(std::string __f){
thefile=__f;
}
std::string thefile;
};






BB_CLASS.H


#include "AA_CLASS.H"
class _B
{
public :
void PRINT_MSG_FROM_A();
private :
_A Testing;
};
void _B::PRINT_MSG_FROM_A()
{
cout<<"The old filename is :<<Testing.thefile<<endl;
Testing.setFile("blabla");
cout<<"And the new one is :<<Testing.thefile<<endl;
};





Sorry if my suggestion wasnt helpful.......!

Share this post


Link to post
Share on other sites

class _A
{
private:
std::string Afile;//just an example..add your private stuff here!
public:
void setFile(std::string __f){
thefile=__f;
}
std::string thefile;
};




Why do you have a member called "Afile" and another called "thefile"? It appears you don't even use Afile. And I'd make "thefile" private, and make it accessible through a function.

Disclaimer: my c++ is very rusty so this may not compile as written.


class DataEncapsulator
{
private:
int mSomeValue;
public:
DataEncapsulator(int someValue )
{
mSomeValue = someValue;
}

int GetSomeValue()
{
return mSomeValue;
}
};

class DataUser
{
private:
DataEncapsulator * mData;
public:
DataUser(DataEncapsulator * data)
{
mData = data;
}

OutputData()
{
cout<<mData->GetSomeValue();
}
};




PS, your naming conventions would make baby Jesus cry :) (I kid: I assume you whipped it up on the spot).

Share this post


Link to post
Share on other sites
If you are sharing data between classes and multiple classes can change that data at will, I would suggest looking a your design. You are asking for trouble. If the data goes bad you have multiple classes that could have caused the trouble, just asking for problems. Your data should be controlled by one class, and only one class. That way you only have one place to look when things go wrong, and they will go wrong.

theTroll

Share this post


Link to post
Share on other sites
You can also do this:

void Foo()
{
int[] someArray = null;

Bar(ref someArray);

// someArray now is an int[50].
}

void Bar(ref int[] blarg)
{
blarg = new int[50];
}


Without "ref", the 'Bar' function can change each element IN the array. With "ref", it can change the elements as well as the array ITSELF (such as to resize it or whatever).

Share this post


Link to post
Share on other sites

This topic is 3844 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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