473,624 Members | 2,185 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1496
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(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

Dec 7 '05 #5
JAL
David... Yup.. I got that to compile as SomeFunction(St ring^ & s1) which I
believe is the equivalent to C# SomeFunction(St ring 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
14927
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) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
1
1224
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 ClassA( ); public static void Main()
8
4407
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 click a button to determine whether the zip code is unique. If the zip code is not unique, another form/dialog is displayed (fclsLookup) - lookup form/dialog. The zip code is passed to the lookup form/dialog by reference. I then load a...
4
1957
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, but we can't seem to find a good and clean solution anywhere. The options we looked into are as follows. Comments following the options are why we did not want to go with them:
8
2102
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 these variables if in a different page than the one variable was created in? Thanks, JJ
12
2676
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
5557
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 ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
0
5008
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 : System.Web.HttpContext.Current.Session 2) For accesing Application variables : System.Web.HttpContext.Current.Application 3) For accesing QueryString variables : System.Web.HttpContext.Current.Request.QueryString Here is a simple example where...
6
2190
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 some scripts that run at the server for the form so I can't disable that statement. I followed Microsoft's instructions(I know..not the best thing to do) but still can't get the variables to pass. I created a class and set up an @reference...
0
8234
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8172
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8677
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8620
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8335
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8474
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7158
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4079
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2605
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.