473,395 Members | 1,616 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

passing & filling string class variables

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
Dec 6 '05 #1
6 1479
brian_harris 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


Brian:

In the CLI object model zoo, System::String is that special beast: the
immutable reference class.

I do not fully understand why a supposedly simpler language needs these
different constructs. C++ is so clean in this regard: every type
(intrinsic or user-defined) can be created on the stack or on the heap,
and every object can be passed by value, pointer or reference. No
exceptions.

David Wilkinson
David Wilkinson
Dec 6 '05 #2
As I understand it when you do an assignment it is suppose to delete old
string and create new string. The value for the empcode variable is correct
in func2. So I think it is more an issue of how to pass parameter by
reference correctly. Or maybe it is due to the auto managing and it is just
freeing the value I want to keep when it exits func2.

"David Wilkinson" wrote:
brian_harris 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


Brian:

In the CLI object model zoo, System::String is that special beast: the
immutable reference class.

I do not fully understand why a supposedly simpler language needs these
different constructs. C++ is so clean in this regard: every type
(intrinsic or user-defined) can be created on the stack or on the heap,
and every object can be passed by value, pointer or reference. No
exceptions.

David Wilkinson
David Wilkinson

Dec 6 '05 #3
brian_harris wrote:
As I understand it when you do an assignment it is suppose to delete old
string and create new string. The value for the empcode variable is correct
in func2. So I think it is more an issue of how to pass parameter by
reference correctly. Or maybe it is due to the auto managing and it is just
freeing the value I want to keep when it exits func2.


Brian:

I'm only just starting on managed code (and I'm learning C++/CLI) but I
think you have to do

func2(String*& empcode)
{
empcode = "empcode value";
}

David Wilkinson
Dec 6 '05 #4
JAL
Brian.... I only know C# and I am a newbie to C++/cli. So I can only answer
in C++/cli. In SomeMethod(SomeObject^ 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= someInstanceHandle->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(String^ s1, String^ s2) {
String^ temp= s1;
s1= s2;
s2= temp;
}
static void SwapByRef(interior_ptr<String^> s1, interior_ptr<String^> s2) {
String^ temp= *s1;
*s1= *s2;
*s2= temp;
}
static String^ GetString() {
return L"New Value";
}
};

int main(array<System::String ^> ^args)
{
String^ s1= L"One";
String^ s2= L"Two";
Swap::SwapByValue(s1,s2);
Console::WriteLine(s1); // -> one
Swap::SwapByRef(&s1,&s2);
Console::WriteLine(s1); // --> two
Console::WriteLine(s2); // --> one
String^ someString= Swap::GetString();
Console::WriteLine(someString);
Console::ReadLine();
return 0;
}

"brian_harris" 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

Dec 7 '05 #5
JAL
David... Yup.. I got that to compile as SomeFunction(String^ & s1) which I
believe is the equivalent to C# SomeFunction(String ref s1) or pass by
reference.
"David Wilkinson" wrote:
brian_harris wrote:
As I understand it when you do an assignment it is suppose to delete old
string and create new string. The value for the empcode variable is correct
in func2. So I think it is more an issue of how to pass parameter by
reference correctly. Or maybe it is due to the auto managing and it is just
freeing the value I want to keep when it exits func2.


Brian:

I'm only just starting on managed code (and I'm learning C++/CLI) but I
think you have to do

func2(String*& empcode)
{
empcode = "empcode value";
}

David Wilkinson

Dec 7 '05 #6
Thanks, that worked for me in C++. I don't recognize that syntax as valid,
since I just changed my declartion to use what you specified and did not need
to change anything comming into function or inthe use of that variable inside
function. So do you have some book that is good at explaning what is leagal
in managed C++ and list of classes. I have been using visual C++ .net step
by step from microsoft for version 2003. While it has given me enogh
information to get started it leaves out alot of stuff.
thanks

"David Wilkinson" wrote:
brian_harris wrote:
As I understand it when you do an assignment it is suppose to delete old
string and create new string. The value for the empcode variable is correct
in func2. So I think it is more an issue of how to pass parameter by
reference correctly. Or maybe it is due to the auto managing and it is just
freeing the value I want to keep when it exits func2.


Brian:

I'm only just starting on managed code (and I'm learning C++/CLI) but I
think you have to do

func2(String*& empcode)
{
empcode = "empcode value";
}

David Wilkinson

Dec 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
1
by: Reinier Beeckman | last post by:
Hi, In a program i'm working on i got several classes. 3 of them have relation to my problem. Let's name them classes class A, class B and class C. classA { public static ClassA A = new...
8
by: Johnny | last post by:
I'm a rookie at C# and OO so please don't laugh! I have a form (fclsTaxCalculator) that contains a text box (tboxZipCode) containing a zip code. The user can enter a zip code in the text box and...
4
by: Doruk | last post by:
The problem that we are experiencing is simple: We want to pass certain parameters from a page with several server controls to another page. We want to do this in a dotnet compliant manner,...
8
by: JJ | last post by:
Hi, What's the preferred way to pass variables around to different pages now? Or if my reading servers me right they are retained in memory for the life of the app, correct? How do I access...
12
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
0
by: bharathreddy | last post by:
Here I will given an example on how to access the session, application and querystring variables in an .cs class file. Using System.Web.HttpContext class. 1) For accesing session variables :...
6
BezerkRogue
by: BezerkRogue | last post by:
This is the most fundamental action I am sure, but I can't seem to make it happen. I am familiar with passing variables in ASP. But that doesn't seem to be the preferred method in .NET. I have...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.