Brian.... I only know C# and I am a newbie to C++/cli. So I can only answer
in C++/cli. In SomeMethod(Some Object^ obj) a handle of type SomeObject is
passed and a copy of the handle goes on the stack. On method entry both
handles, the original and the copy "point" to same string on the heap. You
can reassign another string literal to the copy of the handle on the stack
within the method, but when the method exits the copy of the handle is popped
off the stack and the new string literal may be eligible for garbage
collection. The original handle still "points" to the original string. If you
want to get a string value just do:
String^ GetString() {
return L"New Value";
}
and call it as:
String^ someString= someInstanceHan dle->GetString();
If you actually want to do a swap routine, you need to pass a handle by
reference as the following code demonstrates:
#include "stdafx.h"
using namespace System;
public ref class Swap {
public:
static void SwapByValue(Str ing^ s1, String^ s2) {
String^ temp= s1;
s1= s2;
s2= temp;
}
static void SwapByRef(inter ior_ptr<String^ > s1, interior_ptr<St ring^> s2) {
String^ temp= *s1;
*s1= *s2;
*s2= temp;
}
static String^ GetString() {
return L"New Value";
}
};
int main(array<Syst em::String ^> ^args)
{
String^ s1= L"One";
String^ s2= L"Two";
Swap::SwapByVal ue(s1,s2);
Console::WriteL ine(s1); // -> one
Swap::SwapByRef (&s1,&s2);
Console::WriteL ine(s1); // --> two
Console::WriteL ine(s2); // --> one
String^ someString= Swap::GetString ();
Console::WriteL ine(someString) ;
Console::ReadLi ne();
return 0;
}
"brian_harr is" wrote:
I have a function that passes a string class pointer to a function, this
function is then suppose to fill it and the outer function uses it. But I
believe that I am running into problem due to manged memory. Because it has
value in inner function, but does not have the assigned value in outher
function. So I need to know how to declare this variable and make
assignments correctly so that it will have value when it is back in outher
function: Below is basically how I am currently calling.
I am using .NET 2003 C++
func1 () {
String *empcode;
empcode = new String ("0");
func2(empcode)
}
func2(String *empcode) {
empcode = "empcode value";
}
I have tried it with and without the new String("0") line in func1
Thangs for your help