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

Ref paramater made ugly

I have a method that takes a ref string parameter. This is because it
will be called in loops and most of the time it will not need to modify
the paramater at all, so I preferred not to return a newly allocated
string object.

I need to pass items of an array list (can't use specialized collection
in compact framework) to this method. I can't pass the array item
directly because I get a compiler error (a ref or out argument must be
an lvalue). So this is what I'm doing:
string temp = (string)array[i];
obj.refMethod ( ref temp );
array[i] = temp;

I didn't find a performance problem with this, but is there a better way
to write this?
Nov 16 '05 #1
2 1271

"Brad Wood" <bradley_.wood_@ndsu_.nodak_.edu> wrote in message
news:%2******************@TK2MSFTNGP12.phx.gbl...
I have a method that takes a ref string parameter. This is because it will
be called in loops and most of the time it will not need to modify the
paramater at all, so I preferred not to return a newly allocated string
object.

I need to pass items of an array list (can't use specialized collection in
compact framework) to this method. I can't pass the array item directly
because I get a compiler error (a ref or out argument must be an lvalue).
So this is what I'm doing:
string temp = (string)array[i];
obj.refMethod ( ref temp );
array[i] = temp;

I didn't find a performance problem with this, but is there a better way
to write this?


No. ArrayList only looks like an array because of C# indexers.

string temp = (string)arrayList[i];

is just syntactic sugar for

string temp = (string)ArrayList::get_Item(i);

and
arrayList[i] = temp
is
ArrayList::set_Item(i, temp);

There's no better way to write this.

Even if the C# compiler somehow allowed you to write

obj.refMethod ( ref arrayList[i]);

It would still have to call get_Item/set_Item, and would also need a
temporary variable to hold the result.

David
Nov 16 '05 #2
Not sure its better. But you could propably get rid of the ref method with
something like:

string temp = (string)array[i];
string result = obj.RefMethod( temp ); // Returns null if no change needed.
if ( result != null )
array[i] = result;

--
William Stacey, MVP
http://mvp.support.microsoft.com

"Brad Wood" <bradley_.wood_@ndsu_.nodak_.edu> wrote in message
news:#i**************@TK2MSFTNGP12.phx.gbl...
I have a method that takes a ref string parameter. This is because it
will be called in loops and most of the time it will not need to modify
the paramater at all, so I preferred not to return a newly allocated
string object.

I need to pass items of an array list (can't use specialized collection
in compact framework) to this method. I can't pass the array item
directly because I get a compiler error (a ref or out argument must be
an lvalue). So this is what I'm doing:
string temp = (string)array[i];
obj.refMethod ( ref temp );
array[i] = temp;

I didn't find a performance problem with this, but is there a better way
to write this?


Nov 16 '05 #3

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

Similar topics

9
by: Pedro Graca | last post by:
<?php function ugly_array() { return array(1, 2, 3, 4, 5); } $arr = ugly_array(); echo $arr; ?> not so ugly :) now ... I want to get rid of the $arr temporary variable.
6
by: Quick Function | last post by:
Hi, I developed a site and used css. I put almost all style information in the css and used a lot of "id=my_css_class" in the html. They is little style specification in the html. I found that on...
6
by: WertmanTheMad | last post by:
Ok, This sounds dangerous (and yes I know it is) But its in a controlled enviroment and I really need to know how to do this. How can I pass a Subquery for an Exist or In clause as a...
2
by: Max Adams | last post by:
Question about using delegates with a string paramater to return a ListViewItem object... All, I have a thread and I want this thread to post messages to the main GUI thread using a delegate....
0
by: Matt Swift | last post by:
Anyone have experience of doing this? My aim is thus. I'd like to supply my SP with datetime related information, but possibly break entries down into **/**/**** and have the interface actually...
5
by: yxq | last post by:
Hello I am using VS.Net 2002, my icons of XP type look ugly, and i have saw the article. i have added the manifest file. Sometime the icons show very good, but sometime the icons look very...
7
by: teo | last post by:
I have to validate the user input to prvent HTML injection. I use validateRequest=True and when a potentially malicious input occurs AspNet immediately sends its ugly page about the...
5
by: Frank Jovi | last post by:
I am working on a website with ASP.NET 2.0 I started with a SQL database .mdf. After further discussions with my client, we decided to switch to an Access database. I created a Select query...
2
by: Jim Langston | last post by:
In my function/method paramater list I try to keep constant correctness. I.E. I would use: int Foo( const char* a, int b ) instead of int Foo( char* a, int b ) My question reguards int b. ...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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,...

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.