Click to See Complete Forum and Search --> : member of a managed class cannot be converted to a native reference?


kkirtac
May 27th, 2006, 04:54 PM
i have the "int undo" variable of my __gc class and i want to pass this as a reference to a method like : void method(int& und ){ und = 1 ;} and calling the method like : method(undo);

and i receive the error "An object from the gc heap (member of a managed class) cannot be converted to a native reference"

any way to fix that problem?

darwen
May 28th, 2006, 05:23 PM
Firstly, this is VERY bad practice as it's breaking encapsulation in the class. Methods outside of a class should never change the data inside of a class without going through some sort of interface first.

You should have an accessor e.g.


__gc class MyClass
{
public:
__property int get_Value() { return m_nValue; }
__property void set_Value(int nValue) { m_nValue = nValue; }

private:
int m_nValue;
} ;


and get/set the value appropriately.

Darwen.

kkirtac
May 29th, 2006, 11:54 AM
well, all methods that i will pass the private variable as a reference are already this class' private methods...

cilu
May 29th, 2006, 12:02 PM
well, all methods that i will pass the private variable as a reference are already this class' private methods...
What do you mean, that you want to pass a class' attribute as parameter to the same class' methods? Like this?

_gc class foo
{
int undo;

void f1()
{
f2(undo);
}

void f2(int& val)
{
}
};

If that's what you want to do, then there is no point in it, because as members of the same class, those functions can directly access the variable.

kkirtac
May 30th, 2006, 04:38 AM
hımm...for example, i have several events...picture_MouseDown, MenuItem_Click, Button_Click.....and all are the same class' methods...so when i use the private property "undo" during the execution of the program will all these methods be able to change the property and say picture_MouseDown changed the undo variable and the program is still running and after that event the Button_Click will reach the undo with its changed state or will it reach the initial value of undo ? maybe a simple quest. but i just need to get it..thx

kkirtac
May 30th, 2006, 04:43 AM
i dont pass the undo variable as reference to the methods, just use it inside the methods...
void Initialize()
{
undo = 0;
}

picture_MouseDown(System::Object * sender, System::Windows::Forms::MouseEventArgs * e)
{
undo = 5;
}
....
....
...
button5_Click(System::Object * sender, System::EventArgs * e)
{
//will this method see the undo as 5 or as 0 ?
}