473,382 Members | 1,705 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,382 software developers and data experts.

String object as function argument

I’m not sure where to post this, so I post it here. It’s more like a VC++.NET
/ Managed Code problem. Probably easy to fix for you guys.

I want to use a String object as a parameter for a function. Here is what it
should look like:

String *a = "Hallo";
this->SomeClass->FormatString(a);
this->label->Text = a;

The FormatString function looks like this:

void FormatString(String *b)
{
b = “Aloha”;
}
What comes up in the label is “Hallo” instead of “Aloha”. All class
declarations and everything works fine. The code compiles without error or
warning, but I’m not able to pass the String object to “FormatString” in a
way that it can change it. What am I doing wrong?

Nov 17 '05 #1
2 1011
cnickl wrote:
Im not sure where to post this, so I post it here. Its more like a VC++.NET
/ Managed Code problem. Probably easy to fix for you guys.

I want to use a String object as a parameter for a function. Here is what it
should look like:

String *a = "Hallo";
this->SomeClass->FormatString(a);
this->label->Text = a;

The FormatString function looks like this:

void FormatString(String *b)
{
b = Aloha;
}
What comes up in the label is Hallo instead of Aloha. All class
declarations and everything works fine. The code compiles without error or
warning, but Im not able to pass the String object to FormatString in a
way that it can change it. What am I doing wrong?


You need to pass the handle by reference, e.g. declare FormatString as
below:

void FormatString(String*& b)

Then "b" will simply be another name for the argument "a", and your
modification of "b" will actually modify "a".

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 17 '05 #2
i knew i had to pass it by reference, just couldn’t get the syntax right even
so it is that easy. Well what can i say. Thank you very much.

"Doug Harrison [MVP]" wrote:
cnickl wrote:
I’m not sure where to post this, so I post it here. It’s more like a VC++.NET
/ Managed Code problem. Probably easy to fix for you guys.

I want to use a String object as a parameter for a function. Here is what it
should look like:

String *a = "Hallo";
this->SomeClass->FormatString(a);
this->label->Text = a;

The FormatString function looks like this:

void FormatString(String *b)
{
b = “Aloha”;
}
What comes up in the label is “Hallo” instead of “Aloha”. All class
declarations and everything works fine. The code compiles without error or
warning, but I’m not able to pass the String object to “FormatString” in a
way that it can change it. What am I doing wrong?


You need to pass the handle by reference, e.g. declare FormatString as
below:

void FormatString(String*& b)

Then "b" will simply be another name for the argument "a", and your
modification of "b" will actually modify "a".

--
Doug Harrison
Microsoft MVP - Visual C++

Nov 17 '05 #3

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

Similar topics

9
by: Ronald Fischer | last post by:
Assume the following JavaScript function: function bracketize(s) { return ''; } This function which doesn't assume anything about its argument except that it must be convertible to a string.
5
by: nboutelier | last post by:
Scenario: you enter "foo bar" into a text field... Is it possible through javascript to select/highlight just "foo"? formObject.select() selects all. I need to select only part of the string....
1
by: Neil Schemenauer | last post by:
The title is perhaps a little too grandiose but it's the best I could think of. The change is really not large. Personally, I would be happy enough if only %s was changed and the built-in was...
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
11
by: nephish | last post by:
Hello there, i need a way to check to see if a certain value can be an integer. I have looked at is int(), but what is comming out is a string that may be an integer. i mean, it will be formatted...
31
by: Peter Michaux | last post by:
Hi, I want to know the name of an object's constructor function as a string. Something like this <script type="text/javascript"> function Foo(){}; var a = new Foo(); alert('"' +...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
21
by: phpCodeHead | last post by:
Code which should allow my constructor to accept arguments: <?php class Person { function __construct($name) { $this->name = $name; } function getName()
1
by: dhtmlkitchen | last post by:
Why does "foo" instanceof String return false? It just seems counterintuitive to me. I mean, sure, I can always check the constructor: var fooVar = "foo"; var isString =...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.