Newbie Help - (Managed)Setting string value

  • Thread starter Thread starter Annette Miller
  • Start date Start date
A

Annette Miller

Hi All,

I've never really touched C++ and thought that seeing as I'm familiar with
C# & the Framework, I may as well have a go at managed C++.

I'm trying to write a Class Library to work with my C# program. I want to do
this in C++ for two reasons, 1 because PInvoking is a pain and 2 for the
learning curve.

At the moment I'm trying to work out how to set a string value by passing
the string to a function - heres my code:

....
public ref struct MyStruct
{
public:
String ^myString;
};

public ref class MyClass
{
public:
static MyStruct^ SetMyStructValue()
{
MyStruct ^ms = gcnew MyStruct();
SetStringValue(ms->myString);

return ms;
}

private:
static void SetStringValue(String^ string)
{
string = gcnew String("hello, world"); // this doesn't
work?
}
};

Now I know I can set the string value in SetMyStructValue() but I am
expirementing here... help is much appreciated! Thanks in advance guys...
 
Annette said:
static void SetStringValue(String^ string)
{
string = gcnew String("hello, world"); // this doesn't
work?
}

"string" is an input argument, any modifications to it are lost when the
function returns. I think you want

static void SetStringValue(String^ % string)

where the % is kind of like the "out" or "ref" keyword in C#. You can
also try:

static String^ SetStringValue()

Tom
 
Thanks Nishant!

Nishant Sivakumar said:
See http://blog.voidnish.com/?p=43

--
Regards,
Nish [VC++ MVP]


Annette Miller said:
Hi All,

I've never really touched C++ and thought that seeing as I'm familiar
with C# & the Framework, I may as well have a go at managed C++.

I'm trying to write a Class Library to work with my C# program. I want to
do this in C++ for two reasons, 1 because PInvoking is a pain and 2 for
the learning curve.

At the moment I'm trying to work out how to set a string value by passing
the string to a function - heres my code:

...
public ref struct MyStruct
{
public:
String ^myString;
};

public ref class MyClass
{
public:
static MyStruct^ SetMyStructValue()
{
MyStruct ^ms = gcnew MyStruct();
SetStringValue(ms->myString);

return ms;
}

private:
static void SetStringValue(String^ string)
{
string = gcnew String("hello, world"); // this doesn't
work?
}
};

Now I know I can set the string value in SetMyStructValue() but I am
expirementing here... help is much appreciated! Thanks in advance guys...
 
Back
Top